Spring Bean naming code details

  • 2020-12-18 01:49:12
  • OfStack

This paper mainly describes the naming mode of bean in spring, and introduces 6 ways through simple examples, which are as follows.

1 Generally, when configuring an Bean, you need to specify an id attribute as the name of bean. id must be unique in the IoC container, and the naming of id needs to meet the naming conventions of xml for id.

In the real world, the id naming constraint doesn't bother us. But if the user really wants to use 1 special character to name bean, then the name attribute of bean can be used. The name attribute has no character restrictions and can use almost any character.

Each Bean can have one or more id, the first id we call "identifier", the rest of id we call "alias", these id must have only one in the IoC container.

First, let's introduce the naming rules of Beanid:

1. Follow XML naming conventions
2. It consists of letters, numbers and underscores
3. Hump, lowercase first word, uppercase second word.

Let's use concrete examples to illustrate the different naming conventions of Bean

1. Configure fully qualified class name, only 1

In this example, mainly to print greetings to you, we need an HelloWorld interface and an implementation class named HelloWorldImpl. Next we create a configuration file and an entry class for the program.

First, create the package definition in the project. Next, create the HelloWorld interface in the package:


public interface HelloWorld { 
  public void sayHello(); 
} 

Next, let's create the HelloWorldImpl implementation class:


public class HelloWorldImpl implements HelloWorld{ 
  public void sayHello() { 
    System.out.println("Hello World"); 
  } 
} 

Next, we name Bean in the configuration file:


<bean class="cn.lovepi.chapter03.definition.HelloWorldImpl"/> 

We load the configuration file and run the sample at the application entry Mian.java.


public static void sayHelloWorldByClass(){ 
  // use FileSystemXmlApplicationContext Load configuration file information  
  BeanFactory beanFactory= 
     new FileSystemXmlApplicationContext("src/conf/conf-definition.xml"); 
  // To obtain bean The instance  
  HelloWorld helloWorld=beanFactory.getBean(HelloWorldImpl.class); 
  helloWorld.sayHello(); 
} 

In the ES66en.java file we need:

1. Load the configuration file and start the SpringIoC container
2. Get an instance of the HelloWorldImpl implementation class from the container
3. Output greetings

2. Specify id, only 1

Configure bean in the configuration file


<bean id="HelloWorldById" class="cn.lovepi.chapter03.definition.HelloWorldImpl"/> 

Modify the Main program entry to create a new method to call 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, only 1

Configure bean in the configuration file


<bean name="HelloWorldByName" class="cn.lovepi.chapter03.definition.HelloWorldImpl"/> 

Modify the Main program entry to create a new method to call 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 id and name, where id is the identifier, name is the alias, and only 1

Configure bean in the configuration file


<bean id="HelloWorldById01" name="HelloWorldByName01" 
   class="cn.lovepi.chapter03.definition.HelloWorldImpl"/> 

Modify the Main program entry to create a new method to call 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 name, where multiple name need to be separated by a semicolon, the first name is the identifier, the others are aliases, and only 1

Configure bean in the configuration file


public class HelloWorldImpl implements HelloWorld{ 
  public void sayHello() { 
    System.out.println("Hello World"); 
  } 
} 
0

Modify the Main program entry and create a new method to call bean


public class HelloWorldImpl implements HelloWorld{ 
  public void sayHello() { 
    System.out.println("Hello World"); 
  } 
} 
1

6. Specify the alias, using the alias tag, only 1

Configure bean in the configuration file


public class HelloWorldImpl implements HelloWorld{ 
  public void sayHello() { 
    System.out.println("Hello World"); 
  } 
} 
2

Modify the Main program entry to create a new method to call 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(); 
 
} 

When using aliases, you must first have a 1-only name (id and name are ok)

conclusion

That is the end of this article on Spring Bean naming code details, I hope to help you. Those who are interested can continue to see this site:

On the Scope and Life Cycle of Bean

Details of the Way Spring Instantiates bean

Spring Factory Method Creation (Instantiation) bean Instance Code

If there is any deficiency, please let me know. Thank you for your support!


Related articles: