Spring learning notes -- 02 Bean naming and instantiation, learning notes bean
I. Bean naming
In the previous article, IoC is a container for Bean management. In most cases, beans are configured through XML files. The Bean naming methods are as follows.
1. No id is specified. Only the class name is configured.
<bean class="com.erving.HelloImpl"></bean>
To call this Bean, use the following statement:
HelloApi helloApi = context.getBean(HelloApi.class);
2. Specify the id, which must be unique in the IoC container
<bean id="hello" class="com.erving.HelloImpl"></bean>
To call the API, use the following statement:
HelloApi helloApi = context.getBean("hello", HelloApi.class);
3. specify the name, which must be unique in the IoC container.
<bean name="hello" class="com.erving.HelloImpl"></bean>
To call the API, use the following statement:
HelloApi helloApi = context.getBean("hello", HelloApi.class);
4. If both id and name are specified, id is used as the identifier and name is used as the alias, both of which are unique in the container.
<bean id="hello" name="alias" class="com.erving.HelloImpl"></bean>
In this case, Bean can be instantiated by id or by name:
HelloApi helloApi1 = context.getBean("hello", HelloApi.class);HelloApi helloApi2 = context.getBean("alias", HelloApi.class);
5. When a Bean has multiple names, the first one is the identifier and the other is the alias. Multiple names can be separated by semicolons, semicolons, or spaces.
6. You can also use the <alias> label to specify an alias.
<bean name="hello" class="com.erving.HelloImpl"></bean><alias name="hello" alias="alias"/>
This is exactly the same as the following statement.
<bean name="hello;alias" class="com.erving.HelloImpl"></bean>
Ii. Bean instantiation
Bean instantiation has three forms: constructor, static factory, and instance factory.
1. constructor.
Generally, you can call the constructor of a specified class to create a bean through reflection. In this case, the bean type is not necessarily set to JavaBean, and any Java class can be used. The constructor is also divided into the non-parameter constructor and the parameter constructor.
All of the above are non-parameter constructors. The configuration is as follows:
<bean id="hello" class="com.erving.HelloImpl"></bean>
The constructor with parameters is configured as follows:
<bean name="hello" class="com.erving.HelloImpl2"> <constructor-arg index="0" value="erving..."></constructor-arg></bean>
2. Static factory.
First, define the static factory class:
public class HelloApiStaticFactory { public static HelloApi newInstance(String message) { return new HelloImpl2(message); }}
Then edit the configuration file:
<bean id="bean2" class="com.erving.HelloApiStaticFactory" factory-method="newInstance"> <constructor-arg index="0" value="tian..."></constructor-arg></bean>
The class attribute is a static factory class, and the method for instantiating bean needs to be specified.
The actual instantiation is exactly the same as that of the first method:
public void testStaticFactory() { ApplicationContext context = new ClassPathXmlApplicationContext("helloworld.xml"); HelloApi helloApi = context.getBean("bean2", HelloApi.class); helloApi.sayHello();}
3. instance factory.
First, define the instance factory class:
public class HelloApiInstanceFactory { public HelloApi newInstance(String message) { return new HelloImpl2(message); }}
Then modify the configuration file. The configuration file is divided into two parts: one is the bean of the Instance factory, and the other is the bean created through the factory bean.
<! -- 1. Define the instance factory Bean --> <bean id = "beanInstanceFactory" class = "com. erving. HelloApiInstanceFactory"/> <! -- 2. Use an instance factory Bean to create a Bean --> <bean id = "bean4" factory-bean = "beanInstanceFactory" factory-method = "newInstance"> <constructor-arg index =" 0 "value =" Hello Spring! "> </Constructor-arg> </bean>