Spring IOC容器的創建

IOC

  • 控制反轉:主要是改變了對象的創建方式,由手工創建改爲容器管理。
  • DI:依賴注入。由於對象被集中進行創建和初始化,在使用的時候,就需要從容器中進行獲取,並裝配到對應的類屬性中。

IOC容器配置

  1. 基於xml配置的方式:在xml中通過
    標籤進行對象的聲明。可以採用屬性賦值或者使用構造器的方式進行bean聲明。
<!--  id(name) 用來標識bean的名稱,如果不寫,則用類的全類名作爲名稱cn.jaylen.beans.Teacher#0  -->
    <bean id="teacher" class="cn.jaylen.beans.Teacher">
        <property name="name" value="張三"></property>
        <property name="age" value="22"></property>
    </bean>
    
    <bean id="student" class="cn.jaylen.beans.Student">
        <constructor-arg index="0" value="李四"></constructor-arg>
        <constructor-arg index="1" value="22"></constructor-arg>
        <constructor-arg index="2" value="男"></constructor-arg>
    </bean>
  1. 基於java配置類的方式:使用@Configuration、@Bean完成。
@Configuration
public class IocConfiger {
    /**
     * 類似於<bean/>標籤
     *      1、id默認爲方法名
     *      2、也可以給@Bean傳入value,作爲bean名稱
     * @return
     */
    @Bean("student-1")
    public Student student(){
        return new Student("張三", 30, "男");
    }

    @Bean
    public Teacher teacher(){
        return new Teacher("李四", 25);
    }
}
  1. 使用包掃描結合註解的方式。可以識別指定路徑下帶有@Controller、@Service、@Component、@Repository等等,並將其加入到IOC容器中。
  • 在xml配置中使用如下:
 <!--  使用包掃描的方式進行bean的創建  -->
    <context:component-scan base-package="cn.jaylen.beans"></context:component-scan>
  • 在java配置類中使用如下:
@Configuration
/**
 * 使用包掃描的方式進行bean的創建,可以識別配置路徑下的@Controller、@Service、@Component、@Repository......
 */
@ComponentScan(value = "cn.jaylen.beans",
        excludeFilters = {
            @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class})
        },
        useDefaultFilters = false,
        includeFilters = {
        
        }
)
public class IocConfiger {

    /**
     * 類似於<xml/>標籤
     *      1、id默認爲方法名
     *      2、也可以給@Bean傳入value,作爲bean名稱
     * @return
     */
    @Bean("student-1")
    public Student student(){
        return new Student("張三", 30, "男");
    }

    @Bean
    public Teacher teacher(){
        return new Teacher("李四", 25);
    }
}
  • @ComponentScan可以配置一些列的過濾規則
    • excludeFilters:排除對應bean的加載
    • includeFilters:包含指定bean的加載:由於默認掃描該路徑下所有bean,如果只想包含部分,需要將默認配置關閉。useDefaultFilters = false

IOC容器的創建

  1. 基於xml配置方式:
    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-beans.xml");
        Teacher teacher = applicationContext.getBean(Teacher.class);
        Student student = (Student) applicationContext.getBean("student");
        System.out.println(teacher);
        System.out.println(student);
    }
  1. 基於java配置類的方式
  public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(IocConfiger.class);
        Teacher teacher = applicationContext.getBean(Teacher.class);
        Student student = (Student) applicationContext.getBean("student");
        System.out.println(teacher);
        System.out.println(student);
    }

applicationContext: 是用來操作ioc容器的主要入口,可以從中獲取bean的相關信息。

注意

  • 在實際項目中,通常我們都沒有手動的進行IOC容器的創建。而是將容器的創建交給了tomcat等web服務器。參見:web.xml加載spring配置文件
  • 由於springboot中,tomcat已經內置在了項目環境中,所以IOC容器的創建便被隱藏了。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章