Spring Road (13) - in the assembled configuration bean JavaConfig

Outline

Notes with similar configuration, JavaConfig is also automatic assembly Bean, can also by type or by the name of automated assembly, I believe that if you are in front of xml and notes have a better understanding of the way words herein, this would not go into details too much, directly on the code here needs to explain will be given in the comments.

Examples of the type of automatic assembling press

Clean class singer, dancer class, class stage

Since the injection of the work is done in javaconfig, there is no need to add special information on the class.

package org.maoge.javaconfigssemble;
//普通歌手类
public class Singer {
	private String name;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}
package org.maoge.javaconfigssemble;
//普通舞者类
public class Dancer {
	private String name;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}
package org.maoge.javaconfigssemble;
//普通舞台类
public class Stage {
	private Singer singer;
	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;
	}
}
Bean and automatic assembly configuration
package org.maoge.javaconfigssemble;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration // 该类是一个配置类,Spring需要扫描该类中bean
public class BeanConfiguration {
	@Bean // 生成一个命名为daolang的bean
	public Singer daolang() {
		Singer singer = new Singer();
		singer.setName("刀郎");
		return singer;
	}
	@Bean // 生成一个命名为liujia的bean
	public Dancer liujia() {
		Dancer dancer = new Dancer();
		dancer.setName("刘迦");
		return dancer;
	}
	@Bean(autowire = Autowire.BY_TYPE) // 指定按类型自动装配该bean
	public Stage stage() {
		Stage stage = new Stage();
		return stage;
	}
}
test
package org.maoge.javaconfigssemble;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BeanConfiguration.class);
		Stage stage = (Stage) context.getBean("stage");
		System.out.println(stage.getSinger().getName());
		System.out.println(stage.getDancer().getName());
	}
}

The above code is successfully entered in the following, according to the type of automatic assembly proved successful.

刀郎
刘迦

Automated assembly by name

We will change the way the assembly: @Bean(autowire = Autowire.BY_NAME)by name, run the main class prompted a null pointer, it is because we generate bean names are daolang, liujia, but the property is the name of our Stage class as singer, dancer, so I can not automated assembly by name .

We can change the bean id singer, dancer, or a class of property to the State daolang, after liujia, so that the name matching, can be automatically assembled.

Here too simple, does not show the specific code.

to sum up

Assembly is indeed cumbersome, so I wrote these three chapters also more bothersome, but this is the basis for the Spring, or to grasp in understanding the basis of it.

In order for everyone to have a comprehensive understanding of the Spring, I have been also introduced xml, notes, JavaConfig three ways, because I am in the process of learning found in three ways if we do not have a comprehensive understanding, often not read understand someone else's code, I can not read his company's project.

Haha, in fact, three specific implementations, multiple write to adapt.

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

Guess you like

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