spring學習之bean的命名

bean的命名

每一個bean都有一個或者多個標識符,這些標識符在容器中必須是惟一的。spring

XML

  1. 若是id和name都不指定,IOC容器會自動生成一個惟一標識符,即全類名。
  2. 若是僅指定id或者name,則爲惟一標識符。
  3. 若是同時指定,id爲標識符,name爲別名。

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.xsd">

    <bean id="orderDao" class="com.learn.ch1.OrderDao"/>

    <bean class="com.learn.ch1.BuyDao"/>
    <bean id="buyDao2" class="com.learn.ch1.BuyDao2"/>
    <bean name="buyDao3" class="com.learn.ch1.BuyDao3"/>
    <bean id="buyDao4" name="buyDao5" class="com.learn.ch1.BuyDao4"/>

</beans>

測試代碼:app

@Test
public void test() {
    ApplicationContext app = new ClassPathXmlApplicationContext("daos.xml");
    String[] names = app.getBeanDefinitionNames();
    for (String name : names) {

    }
}

運行結果以下:
image.png測試

註解

註解也有兩個屬性,name和value,可是這兩個不能共存,只能用一個。若是都不指定name和value,那麼方法名做爲bean的惟一標識符。
MyConfigspa

@Configuration
public class MyConfig {
    @Bean
    public BuyDao buyDao() {
        return new BuyDao();
    }

    @Bean(name = "buyDao2")
    public BuyDao2 buyDao2() {
        return new BuyDao2();
    }

    @Bean(value = "buyDao3")
    public BuyDao3 buyDao3() {
        return new BuyDao3();
    }

  /*  
    兩個一塊兒指定,會報錯
    @Bean(value = "buyDao4", name = "buyDao5")
    public BuyDao4 buyDao4() {
        return new BuyDao4();
    }*/

}

測試代碼:3d

@Test
public void test() {
    ApplicationContext app = new AnnotationConfigApplicationContext(MyConfig.class);
    String[] names = app.getBeanDefinitionNames();
    for (String name : names) {
        System.out.println(name);
    }
}

運行結果以下:
image.pngcode

bean的別名

當一個bean須要多個標識符時,就須要別名,這些別名也要是惟一的。xml

XML

bean標籤

多個別名,能夠用,;以及空格隔開。
services.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.xsd">

    <bean id="orderService" class="com.learn.ch1.OrderService" name="orderService2 orderService3,orderService4;orderService5"/>
</beans>

測試代碼以下:blog

@Test
public void test() {
    ApplicationContext app = new ClassPathXmlApplicationContext("services.xml");
    System.out.println(app.getBean("orderService2"));
    System.out.println(app.getBean("orderService3"));
    System.out.println(app.getBean("orderService4"));
    System.out.println(app.getBean("orderService5"));
    String[] aliases = app.getAliases("orderService");
    for(String alias : aliases) {
        System.out.println(alias);
    }
}

運行結果以下:
image.png
能夠看出,都是同一個對象,且打印了多個別名。

alias標籤

alias配置以下:

<?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="orderService" class="com.learn.ch1.OrderService"/>
    <!--name對應的是bean的惟一標識符,alias對應的別名-->
    <alias name="orderService" alias="orderService2"/>
    <alias name="orderService" alias="orderService3"/>
</beans>

測試代碼以下:

@Test
public void test() {
    ApplicationContext app = new ClassPathXmlApplicationContext("alias.xml");
    System.out.println(app.getBean("orderService"));
    System.out.println(app.getBean("orderService2"));
    System.out.println(app.getBean("orderService3"));
    String[] aliases = app.getAliases("orderService");
    for(String alias : aliases) {
        System.out.println(alias);
    }
}

運行結果以下:
image.png
效果跟上面例子同樣

註解

bean的name和value,都是string類型的數組,因此直接用數組定義,就能夠了。
MyConfig

@Configuration
public class MyConfig {
    @Bean(name ={"orderService1","orderService2","orderService3"})
    public OrderService orderService(){
        return new OrderService();
    }
}

測試代碼:

@Test
public void test() {
    ApplicationContext app = new AnnotationConfigApplicationContext(MyConfig.class);
    System.out.println(app.getBean("orderService1"));
    System.out.println(app.getBean("orderService2"));
    System.out.println(app.getBean("orderService3"));
    String[] aliases = app.getAliases("orderService1");
    for(String alias : aliases) {
        System.out.println(alias);
    }
}

運行結果以下:
image.png

相關文章
相關標籤/搜索