This article mainly describes the naming method of beans in spring. We introduce six methods to you through simple examples, as follows.
Generally speaking, when configuring a bean, you need to specify an id attribute as the name of the bean. The id must be unique in the IoC container. In addition, the naming of the id needs to meet the naming specification of the id by the xml.
In actual situations, the id naming constraint will not affect us. However, if the user does want to use some special characters to name the bean, then the name attribute of the bean can be used to name it. The name attribute has no character restrictions, and almost any character can be used.
Each bean can have one or more ids. We call the first id "identifier" and the rest ids "alias". These ids must be unique in the IoC container.
First, let’s introduce the naming rules of Beanid:
1. Follow XML naming specifications
2. Composed of letters, numbers, and underscores
3. Camel style, the letter of the first word is lowercase, and the letter of the first word is uppercase from the second word.
Next, we use specific examples to introduce different naming methods of beans
1. Configure fully qualified class names, unique
In the example, we mainly output greeting information to everyone. We need a HelloWorld interface and an implementation class named HelloWorldImpl. Next we create a configuration file and a program entry class.
First create the package definition in the project, and then create the HelloWorld interface in the package:
public interface HelloWorld { public void saysHello(); }Next we create the HelloWorldImpl implementation class:
public class HelloWorldImpl implements HelloWorld{ public void saysHello() { System.out.println("Hello World"); } }Next, we bean name for HelloWorldImpl in the configuration file:
<bean/>
We load the configuration file and run the examples in the program entrance Mian.java.
public static void saysHelloWorldByClass(){ //Use FileSystemXmlApplicationContext to load configuration file information BeanFactory beanFactory= new FileSystemXmlApplicationContext("src/conf/conf-definition.xml"); //Get bean instance HelloWorld helloWorld=beanFactory.getBean(HelloWorldImpl.class); helloWorld.sayHello(); }In the Main.java file we need:
1. Complete the loading of configuration files and the startup of SpringIoC containers
2. Obtain an instance of HelloWorldImpl implementation class from the container
3. Output greeting information
2. Specify id, unique
Configure beans in configuration file
<bean id="HelloWorldById"/>
Modify the Main program entry and create a new method to call the bean
public static void saysHelloWorldById(){ BeanFactory factory= new FileSystemXmlApplicationContext("src/conf/conf-definition.xml"); HelloWorld helloWorld=factory.getBean("HelloWorldById",HelloWorldImpl.class); helloWorld.sayHello(); } 3. Specify name, name is an identifier, unique
Configure beans in configuration file
<bean name="HelloWorldByName"/>
Modify the Main program entry and create a new method to call the bean
public static void saysHelloWorldByName(){ BeanFactory factory= new FileSystemXmlApplicationContext("src/conf/conf-definition.xml"); HelloWorld helloWorld=factory.getBean("HelloWorldByName",HelloWorldImpl.class); helloWorld.sayHello(); }4. Specify id and name, where id is an identifier, name is an alias, unique
Configure beans in configuration file
<bean id="HelloWorldById01" name="HelloWorldByName01" />
Modify the Main program entry and create a new method to call the bean
public static void saysHelloWorldByNameAndId(){ BeanFactory factory= new FileSystemXmlApplicationContext("src/conf/conf-definition.xml"); HelloWorld helloWorld01=factory.getBean("HelloWorldById01",HelloWorldImpl.class); HelloWorld helloWorld02=factory.getBean("HelloWorldByName01",HelloWorldImpl.class); helloWorld01.sayHello(); helloWorld02.sayHello(); } 5. Specify multiple names, where multiple names need to be split with semicolons. The first name is an identifier, the others are an alias, and unique.
Configure beans in configuration file
<bean name="bean1;alias01;alias02;alias03" /> <bean id="bean2" name="alias11;alias12;alias13" />
Modify the Main program entry and create a new method to call the bean
public static void saysHelloWorldByMutilName(){ BeanFactory factory= new FileSystemXmlApplicationContext("src/conf/conf-definition.xml"); HelloWorld helloWorld1=factory.getBean("bean1",HelloWorldImpl.class); HelloWorld helloWorld01=factory.getBean("alias01",HelloWorldImpl.class); HelloWorld helloWorld02=factory.getBean("alias02",HelloWorldImpl.class); HelloWorld helloWorld03=factory.getBean("alias03",HelloWorldImpl.class); helloWorld1.sayHello(); helloWorld01.sayHello(); helloWorld02.sayHello(); helloWorld03.sayHello(); HelloWorld helloWorld2=factory.getBean("bean2",HelloWorldImpl.class); HelloWorld helloWorld11=factory.getBean("alias11",HelloWorldImpl.class); HelloWorld helloWorld12=factory.getBean("alias12",HelloWorldImpl.class); HelloWorld helloWorld13=factory.getBean("alias13",HelloWorldImpl.class); helloWorld2.sayHello(); helloWorld11.sayHello(); helloWorld12.sayHello(); helloWorld13.sayHello(); } 6. Specify an alias, use the alias tag to specify it, unique
Configure beans in configuration file
<bean name="bean3"/> <alias name="bean3" alias="alias21"/> <alias name="bean3" alias="alias22"/>
Modify the Main program entry and create a new method to call the bean
public static void saysHelloWorldByAlias(){ BeanFactory factory= new FileSystemXmlApplicationContext("src/conf/conf-definition.xml"); HelloWorld helloWorld01=factory.getBean("bean3",HelloWorldImpl.class); HelloWorld helloWorld02=factory.getBean("alias21",HelloWorldImpl.class); HelloWorld helloWorld03=factory.getBean("alias22",HelloWorldImpl.class); helloWorld01.sayHello(); helloWorld02.sayHello(); helloWorld03.sayHello(); }When naming with alias, you must first have a unique name (both id and name are OK)
Summarize
The above is all the detailed explanation of the naming method code of Beans in Spring. I hope it will be helpful to everyone. Interested friends can continue to refer to this site:
" A Brief Discussion on the Scope and Life Cycle of Beans in Spring "
" Detailed explanation of the method and code of Spring instantiation bean "
" Spring Factory Method Creation (Instantiation) Bean Instance Code "
If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!