bean name
Each bean has one or more identifiers that must be unique in the container.
XML
- If neither id nor name is specified, the IOC container will automatically generate a unique identifier, that is, the full class name.
- Unique identifier if only id or name is specified.
- If specified at the same time, id is an identifier and name is an alias.
The xml configuration is as follows:
<?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 code:
@Test public void test() { ApplicationContext app = new ClassPathXmlApplicationContext("daos.xml"); String[] names = app.getBeanDefinitionNames(); for (String name : names) { } }
The results are as follows:
annotation
Annotation also has two attributes, name and value, but they cannot coexist, only one can be used. If both name and value are not specified, the method name is the unique identifier of the 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(); } /* Two designated together, error will be reported @Bean(value = "buyDao4", name = "buyDao5") public BuyDao4 buyDao4() { return new BuyDao4(); }*/ }
Test code:
@Test public void test() { ApplicationContext app = new AnnotationConfigApplicationContext(MyConfig.class); String[] names = app.getBeanDefinitionNames(); for (String name : names) { System.out.println(name); } }
The operation results are as follows:
bean alias
When a bean needs more than one identifier, it needs aliases, which are also unique.
XML
bean Tags
Multiple aliases can be separated by,; and spaces.
services.xml is configured as follows:
<?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>
The test code is as follows:
@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); } }
The operation results are as follows:
As you can see, all of them are the same object and multiple aliases are printed.
alias Tags
The alias configuration is as follows:
<?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 The corresponding is bean Unique identifier of, alias Corresponding alias--> <alias name="orderService" alias="orderService2"/> <alias name="orderService" alias="orderService3"/> </beans>
The test code is as follows:
@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); } }
The operation results are as follows:
The effect is the same as the above example.
annotation
bean name and value are arrays of string type, so you can define them directly with arrays.
MyConfig
@Configuration public class MyConfig { @Bean(name ={"orderService1","orderService2","orderService3"}) public OrderService orderService(){ return new OrderService(); } }
Test code:
@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); } }
The operation results are as follows: