每一個bean都有一個或者多個標識符,這些標識符在容器中必須是惟一的。spring
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) { } }
運行結果以下:
測試
註解也有兩個屬性,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); } }
運行結果以下:
code
當一個bean須要多個標識符時,就須要別名,這些別名也要是惟一的。xml
多個別名,能夠用,;以及空格隔開。
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); } }
運行結果以下:
能夠看出,都是同一個對象,且打印了多個別名。
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); } }
運行結果以下:
效果跟上面例子同樣
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); } }
運行結果以下: