文章目录
package com.sxt.dao;
public interface IUserDao {
public String add();
}
package com.sxt.dao.impl;
import com.sxt.dao.IUserDao;
public class UserDaoImpl implements IUserDao{
@Override
public String add() {
return "hello";
}
}
package com.sxt.service;
public interface IUserService {
public String add();
}
package com.sxt.service.impl;
import com.sxt.dao.IUserDao;
import com.sxt.service.IUserService;
public class UserServiceImpl implements IUserService{
private IUserDao dao;
public void setDao(IUserDao dao) {
this.dao = dao;
}
@Override
public String add() {
return dao.add();
}
}
package com.sxt.controller;
import com.sxt.service.IUserService;
public class UserContreller {
private IUserService service;
public void setService(IUserService service) {
this.service = service;
}
public String add(){
return service.add();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置dao -->
<bean class="com.sxt.dao.impl.UserDaoImpl" id="userDaoImpl"/>
<!-- 配置 service -->
<bean class="com.sxt.service.impl.UserServiceImpl" id="userServiceImpl">
<!-- 设值注入dao对象 -->
<property name="dao" ref="userDaoImpl"></property>
</bean>
<!-- 配置controller -->
<bean class="com.sxt.controller.UserContreller">
<!-- 设值注入service对象 -->
<property name="service" ref="userServiceImpl"></property>
</bean>
</beans>
package com.sxt.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.sxt.controller.UserContreller;
public class TestDemo {
@Test
public void test(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserContreller bean = ac.getBean(UserContreller.class);
System.out.println(bean.add());
}
}
