Spring Road (12) -- assembling bean s in annotation configuration

Keywords: xml Spring encoding Java

background

The last one talked about how to assemble bean in the xml configuration. In fact, the principle of bean in the annotation configuration is exactly the same as that in xml, and the effect is the same, but it adopts different ways.

Therefore, we do not change the examples in this article, but take the example of injecting singers and dancers into the stage for assembly. In the case of annotations, annotations are added directly to classes and properties. It is unnecessary to display the package path + class name of the specified bean, so they are all automatically assembled.

Auto assemble by name

We name the beans and specify the assembled bean name at assembly time.

1. Define singer class and generate corresponding bean for it through @ component annotation.
package org.maoge.annotationassemble;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("liujia") // The annotation specifies that the bean name generated by the Dancer class is liujia
public class Dancer {
	@Value("Liu Jia") // Insert name by comment
	private String name;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}
package org.maoge.annotationassemble;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("daolang") // The bean name generated by the Singer class is specified as daolang by annotation
public class Singer {
	@Value("Dao Lang") // Insert name by comment
	private String name;

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

Here again explain the role of @ Component("xxx") annotation. When the Spring container starts, it will scan the annotated classes and generate a bean object named xxx to put into the container. If the @ Component annotation is used, the first letter of the generated bean named class is converted to lowercase.

2. Define a stage class and assign a name to assemble its properties

You can use @ Autowired to inject beans through specified rules, and use @ Qualifier("xxx") to assemble beans named xxx. The code is as follows:

package org.maoge.annotationassemble;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
//Stage class, note that stage also needs to generate a bean
@Component("stage")
public class Stage {
	@Autowired//Indicates automatic injection
	@Qualifier("daolang")//Specify the bean named daolang to assemble
	private Singer singer;
	@Autowired
	@Qualifier("liujia")//Specify the bean named liujia to assemble
	private Dancer dancer;
	public Singer getSinger() {
		return singer;
	}
	public void setSinger(Singer singer) {
		this.singer = singer;
	}
	public Dancer getDancer() {
		return dancer;
	}
	public void setDancer(Dancer dancer) {
		this.dancer = dancer;
	}
}
3. Configure the xml file to start the bean scanning

Don't forget to use annotations to configure beans. You still need an XML file, in which you need to start scanning the package where the bean is located. Therefore, the spring.xml file 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"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">
   <context:component-scan
   	base-package="org.maoge.annotaionassemble" />
</beans>
4, test

Start the main class, get the stage from the container, you will find that the singers and dancers in the stage have been assembled successfully.

package org.maoge.annotationassemble;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				"/org/maoge/annotationassemble/spring.xml");
		Stage stage = (Stage) context.getBean("stage");
		System.out.println(stage.getSinger().getName());
		System.out.println(stage.getDancer().getName());
	}
}

Output results:

Dao Lang
 Liu Jia

Automatic assembly by type

This is easier, because when using annotations, a class usually generates a bean (an annotation is added to a class to generate a corresponding bean).

So matching by type is basically very stable, no problem, and very process. At present, this method should be very popular. No matter SSM project or spring boot project, this method is used very much.

Look at the example:

1. Define singers and dancers
package org.maoge.annotationassemble;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component // No name specified, so the default name should be singer
public class Singer {
	@Value("Dao Lang") // Insert name by comment
	private String name;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}
package org.maoge.annotationassemble;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component // No name specified, so the default name should be dancer
public class Dancer {
	@Value("Liu Jia") // Insert name by comment
	private String name;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}
2. Automatically assemble stage bean s by type

Do not add @ Qualifier, the default is to automatically assemble by type, because it is too common.

package org.maoge.annotationassemble;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Stage {
	@Autowired // Automatic assembly by type
	private Singer singer;
	@Autowired // Automatic assembly by type
	private Dancer dancer;
	public Singer getSinger() {
		return singer;
	}
	public void setSinger(Singer singer) {
		this.singer = singer;
	}
	public Dancer getDancer() {
		return dancer;
	}
	public void setDancer(Dancer dancer) {
		this.dancer = dancer;
	}
}
3. Test run

It's the same xml and Main.java. It's still OK to run Main directly for testing.

summary

When using annotations, the assembly is highly automated and very sharp.

Assembling is not complicated. It is to inject other beans into the properties of one bean.

Automatic assembly is to specify the type or name of the bean to be assembled, then specify the rules (name / type) for automatic assembly, and then the container will automatically inject the corresponding bean into the properties of the assembled bean.

325 original articles published, 238 praised, 520000 visitors+
His message board follow

Posted by esport on Fri, 24 Jan 2020 02:18:11 -0800