基本概念
IOC (Inversion of Control:控制反转 )将创建UserService对象的控制权交给Spring管理
DI(Dependency Injection)依赖注入,在Spring创建Bean对象时动态将以来对象注入到Bean对象中的方法。
两者区别:Ioc是指对象的创建权反转给Spring,DI是创建对象的时候将对象依赖的属性以配置方式注入。
例子:
- User.class:
public class User {
private String username;
private String password;
public User(String username, String password) {
super();
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [username is: " + username + ", password is: " + password + "]";
}
}
- UserDAO.class:
public interface UserDAO {
public void save(User user);
public void delete(User user);
}
- UserDAOImpl.class:
public class UserDAOImpl implements UserDAO {
public void save(User user){
// todo
};
public void delete(User user){
// todo ;
}
}
- UserFactory.class:
public class UserFactory {
public User CreatUser () {
User user=new User();
user.setUsername("Toto");
user.setPassword("123456");
return user;
}
}
- UserService:
public class UserService {
private UserDAO userDAO;
public void init() {
//todo
System.out.println("init");
}
public void save(User user) {
userDAO.save(user);
}
public UserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
public UserService(UserDAO userDAO) {
super();
this.userDAO = userDAO;
}
public void destroy() {
//todo
System.out.println("destroy");
}
}
- 测试:
- UserServiceTest.class
public class UserServiceTest {
@Test
public void test() {
//Ioc
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");
UserService userService=(UserService) applicationContext.getBean("userService");
//DI构造方法注入
User userFac=(User) applicationContext.getBean("user");
//DI属性注入
User user1=(User) applicationContext.getBean("user1");
//DI工厂注入
User user2=(User) applicationContext.getBean("user2");
System.out.println(userFac);
System.out.println(user1);
System.out.println(user2);
userService.save(userFac);
userService.destroy();
}
- beans.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- 构造方法DI -->
<bean id="user" class="com.bjsxt.model.User" >
<constructor-arg index="0" type="String" value="userCon"></constructor-arg>
<constructor-arg index="1" type="String" value="123"></constructor-arg>
</bean>
<!-- 属性DI -->
<bean id="user1" class="com.bjsxt.model.User" >
<property name="username" value="u1"></property>
<property name="password" value="000000"></property>
</bean>
<!-- 工厂DI -->
<bean id= "userFactory" class="com.bjsxt.factory.UserFactory" > </bean>
<bean id="user2" factory-bean="userFactory" factory-method="CreatUser"></bean>
<bean id="userDao" class="com.bjsxt.dao.impl.UserDAOImpl"></bean>
<bean id="userService" class="com.bjsxt.service.UserService" init-method="init" destroy-method="destroy" scope="prototype">
<property name="userDAO" ref="userDao" />
</beans>