Spring Road (12) - arranged in the assembled bean annotations

background

Previous talked about how to assemble bean xml configuration, in fact, annotation configuration and assembly of the bean xml exactly the same principle, but also to achieve the effect is the same, but it uses a different approach.

So this article, we also do not replace instance, or in the stage injecting singers, dancers, for example, are assembled. Since the case of annotations, are added directly on the classes, properties annotation, displayed no need to specify the bean class name + path package, the assembly is automatic.

Automated assembly by name

We name the bean, bean and specify the name of the assembly during assembly.

1, the class definition of the singer, and for generating a corresponding annotation @component the bean.
package org.maoge.annotationassemble;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("liujia") // 通过注解指定Dancer类生成的bean名称为liujia
public class Dancer {
	@Value("刘迦") // 通过注解注入姓名
	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") // 通过注解指定Singer类生成的bean名称为daolang
public class Singer {
	@Value("刀郎") // 通过注解注入姓名
	private String name;

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

Here again explain @Component("xxx")the role of the annotation, Spring container at startup, will scan the label annotation class, and generating a named object into a bean xxx container. If you use annotation @Componentlabel, then turn generates a bean named for the first letter lowercase class.

2, the class definition of the stage, and the name of its properties assembly

By @Autowiredmaking the injection by specifying rules come bean, while using @Qualifier("xxx")the assembly named xxx the bean, the following code:

package org.maoge.annotationassemble;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
//舞台类,注意舞台也要生成一个bean
@Component("stage")
public class Stage {
	@Autowired//表示自动注入
	@Qualifier("daolang")//指定要装配名称为daolang的bean
	private Singer singer;
	@Autowired
	@Qualifier("liujia")//指定要装配名称为liujia的bean
	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, the configuration xml file, scan to open the bean

Do not forget to use annotation configuration bean, or need a xml file that you want to open the scan where the bean bag, so spring.xml file 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, the test

Start the main class, get arena from the container, you will find the stage singer and dancer assembly have been successful.

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:

刀郎
刘迦

By type automatic assembly

The more simple ah, because the use of annotations, a class typically generates a bean (applied to a note on a class corresponding to generate a bean).

So according to the type of match is basically very stable ah, nothing issue, but also very processes. Currently this way should be very, very popular, whether SSM project or SpringBoot project, we are using a lot this way.

Look at an example:

1, the definition of classes and dancer singer Class
package org.maoge.annotationassemble;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component // 未指定命名,所以默认命名应为singer
public class Singer {
	@Value("刀郎") // 通过注解注入姓名
	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 // 未指定命名,所以默认命名应为dancer
public class Dancer {
	@Value("刘迦") // 通过注解注入姓名
	private String name;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}
2, the bean type automatic assembly stage

Do not increase @Qualifier, the default is automatically according to the type of assembly, because too common.

package org.maoge.annotationassemble;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Stage {
	@Autowired // 按类型自动装配
	private Singer singer;
	@Autowired // 按类型自动装配
	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, the test run

Still the same xml and Main.java, directly run Main test, still no problem.

to sum up

When using annotations, highly automated assembly and very sharp.

Assemble this thing, is not complicated, it is to inject a bean property in other bean.

Automatic assembly, is to specify the type of bean to be mounted or the name, and then specify the rule (name / type) automatic assembly, and then automatically injected into the appropriate bean bean property is assembled by the container.

Published 325 original articles · won praise 238 · views 520 000 +

Guess you like

Origin blog.csdn.net/woshisangsang/article/details/104081113