Spring-DI控制反轉和IOC依賴注入

Spring-DI控制反轉和IOC依賴注入

DI控制反轉實例

IDEAJ自動導入Spring框架

IDEAJ自動生成Spring目錄

建立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");
    }
}
建立SpringBean配置文件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>
建立DI的測試類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

運行結果.png

總結

控制反轉(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>
建立IOC的測試類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()方法

運行結果.png

總結

UserServiceImpl.java裏實例化UserDao對象做爲它的屬性,UseServiceImpl.javaUserDaoImpl.java產生依賴關係。經過setUserDao()方法將userDao對象注入就叫作依賴注入。框架

相關文章
相關標籤/搜索