Spring Bean naming code details, springbean
This article mainly describes the bean naming method in spring, and introduces six methods through simple examples, as shown below.
Generally, you must specify an id attribute as the Bean name When configuring a bean. The id must be unique in the IoC container. In addition, the name of the id must meet the xml Naming rules for the id.
In practice, the id naming constraints do not affect us. However, if you really want to use some special characters to name the bean, you can use the bean name attribute for naming. The name attribute has no character restrictions and can use almost any character.
Each Bean can have one or more IDs. We call the first id as the "identifier" and the other IDS as the "alias". These IDS must be unique in the IoC container.
First, we will introduce the naming rules of Beanid:
1. Follow the XML Naming rules
2. It consists of letters, numbers, and underscores
3. camper, the first letter is in lower case, and the first letter is in upper case from the second word.
Next, we will use specific examples to introduce different Bean naming methods.
1. configure a fully qualified class name, unique
In this example, we will output greetings to you. We need a HelloWorld interface and an implementation class named HelloWorldImpl. Next we will 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 sayHello(); }
Next we will create the HelloWorldImpl implementation class:
public class HelloWorldImpl implements HelloWorld{ public void sayHello() { System.out.println("Hello World"); } }
In the configuration file, we name HelloWorldImpl Bean:
<bean class="cn.lovepi.chapter03.definition.HelloWorldImpl"/>
We load the configuration file and running example in the program entry Mian. java.
Public static void sayHelloWorldByClass () {// 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. Load the configuration file and start the SpringIoC container.
2. Get the HelloWorldImpl implementation class instance from the container
3. Output greeting Information
2. id specified, unique
Configure bean in the configuration file
<bean id="HelloWorldById" class="cn.lovepi.chapter03.definition.HelloWorldImpl"/>
Modify the Main program entry and create a method to call the bean
public static void sayHelloWorldById(){ BeanFactory factory= new FileSystemXmlApplicationContext("src/conf/conf-definition.xml"); HelloWorld helloWorld=factory.getBean("HelloWorldById",HelloWorldImpl.class); helloWorld.sayHello(); }
3. Specify name, name as the identifier, unique
Configure bean in the configuration file
<bean name="HelloWorldByName" class="cn.lovepi.chapter03.definition.HelloWorldImpl"/>
Modify the Main program entry and create a method to call the bean
public static void sayHelloWorldByName(){ BeanFactory factory= new FileSystemXmlApplicationContext("src/conf/conf-definition.xml"); HelloWorld helloWorld=factory.getBean("HelloWorldByName",HelloWorldImpl.class); helloWorld.sayHello(); }
4. Specify the id and name, where id is the identifier, name is the alias, unique
Configure bean in the configuration file
<bean id="HelloWorldById01" name="HelloWorldByName01" class="cn.lovepi.chapter03.definition.HelloWorldImpl"/>
Modify the Main program entry and create a method to call the bean
public static void sayHelloWorldByNameAndId(){ 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. Multiple names must be separated by semicolons. The first name is the identifier, the others are aliases, and they are unique.
Configure bean in the configuration file
<bean name="bean1;alias01;alias02;alias03" class="cn.lovepi.chapter03.definition.HelloWorldImpl"/> <bean id="bean2" name="alias11;alias12;alias13" class="cn.lovepi.chapter03.definition.HelloWorldImpl"/>
Modify the Main program entry and create a method to call the bean
public static void sayHelloWorldByMutilName(){ 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 the alias and use the alias tag to specify it. It is unique.
Configure bean in the configuration file
<bean name="bean3" class="cn.lovepi.chapter03.definition.HelloWorldImpl"/> <alias name="bean3" alias="alias21"/> <alias name="bean3" alias="alias22"/>
Modify the Main program entry and create a method to call the bean
public static void sayHelloWorldByAlias(){ 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(); }
You must have a unique name (both id and name can be used) before using an alias)
Summary
The above is all the details about Bean naming method code in Spring. I hope it will be helpful to you. Interested friends can continue to refer to this site:
《Introduction to Bean scope and lifecycle in Spring"
《Code explanation of Spring bean instantiation Method"
《Create (instantiate) bean instance code using Spring factory Method"
If you have any shortcomings, please leave a message. Thank you for your support!