每个bean都有一个或者多个标识符,这些标识符在容器中必须是唯一的。
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>
测试代码:
@Test public void test() { ApplicationContext app = new ClassPathXmlApplicationContext("daos.xml"); String[] names = app.getBeanDefinitionNames(); for (String name : names) { } }
运行结果如下:
注解也有两个属性,name和value,但是这两个不能共存,只能用一个。如果都不指定name和value,那么方法名作为bean的唯一标识符。
MyConfig
@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(); }*/ }
测试代码:
@Test public void test() { ApplicationContext app = new AnnotationConfigApplicationContext(MyConfig.class); String[] names = app.getBeanDefinitionNames(); for (String name : names) { System.out.println(name); } }
运行结果如下:
当一个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>
测试代码如下:
@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); } }
运行结果如下: