标签:控制反转 junit4 实例化 encoding alt 测试 out 直接 关键字
UserDao.java
接口public interface UserDao {
public void say();
}
UserDaoImpl.java
继承UserDao.java
并重写say()
方法public class UserDaoImpl implements UserDao{
@Override
public void say() {
System.out.println("i am a UserDaoImpl");
}
}
ApplicationContext.xml
这个温江,相当于花名册,Spring中的Bean全部需要在这里注册,当遇到实例化某个Bean时,只需要翻开花名册,根据它的name
来获取Bean的实例。
<?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.xsd">
<bean id = "userDao" class="UserDaoImpl"></bean>
</beans>
TestDI.java
并实例化userDao
对象ApplicationContext
类是Spring中的org.springframework.context.BeanFactory
的子接口,是Spring中的Bean实例加工厂。
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestDI {
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("ApplicationContext.xml");
UserDao userDao = (UserDao) applicationContext.getBean("userDao");
@Test
public void testDI() {
userDao.say();
}
}
testDI()
方法通过JUnit4运行testDI()
方法,运行结果如下:
控制反转(DI)本质上是一种创建实例化对象的方式,通过控制反转,程序员不需要通过new
关键字来创建对象,而是直接通过对象的名字从BeanFactory
的子接口ApplicationContext
中取出实例化好的对象,BeanFactory
本质上是Spring中容器。
UserService.java
接口public interface UserService {
public void say();
}
UserServiceImpl.java
继承UserService.java
并重写say()
方法public class UserServiceImpl implements UserService{
@Override
public void say() {
System.out.println("i am a UserServiceImpl !");
}
}
UserServiceImpl.java
中声明UserDao
对象,并生成setUserDao()
方法注入UserDao
对象public class UserServiceImpl implements UserService{
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void say() {
userDao.say();
System.out.println("i am a UserServiceImpl !");
}
}
ApplicationContext.xml
文件中配置UserServiceImpl
<?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.xsd">
<bean id = "userDao" class="UserDaoImpl"></bean>
<!-- 将id为uerDao的Bean实例注入到userService实例中 -->
<bean id = "userService" class="UserServiceImpl" >
<!-- property标签调用userService实例中的setUserDao方法实现依赖注入 -->
<property name="userDao" ref="userDao"></property>
<!-- name属性表示userService实例中的属性名称,
ref属性表示userService实例中的属性值 -->
</bean>
</beans>
TestIOC.java
并实例化userService
对象import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestIOC {
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("ApplicationContext.xml");
UserService userService = (UserService) applicationContext.getBean("userService");
@Test
public void testIOC() {
userService.say();
}
}
testIOC()
方法在UserServiceImpl.java
里实例化UserDao
对象作为它的属性,UseServiceImpl.java
对UserDaoImpl.java
产生依赖关系。通过setUserDao()
方法将userDao
对象注入就叫做依赖注入。
标签:控制反转 junit4 实例化 encoding alt 测试 out 直接 关键字
原文地址:https://www.cnblogs.com/2sheep2simple/p/10804848.html