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的實例。java
<?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()
方法,運行結果以下:spring
控制反轉(DI)本質上是一種建立實例化對象的方式,經過控制反轉,程序員不須要經過new
關鍵字來建立對象,而是直接經過對象的名字從BeanFactory
的子接口ApplicationContext
中取出實例化好的對象,BeanFactory
本質上是Spring中容器。app
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
對象注入就叫作依賴注入。框架