Parsing the process of Bean Creation by AnnotationConfigWebApplicationContext

Posted Dream_it_possible!

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Parsing the process of Bean Creation by AnnotationConfigWebApplicationContext相关的知识,希望对你有一定的参考价值。

        You know Bean is a Definition in Spring Framework, It is  everywhere in our work and our projects, how does it work?  I will  write a blog to deepen impression by studying the source code of Spring Framework.

AnnotationConfigWebApplicationContext

1.  Register Configuration Class

        Let me show you a couple of examples below.

        First of all, we can define a  configuration class, it means  that Spring will scan the similar classes for future use.

        Anyway we must register our class with the Spring container.

public class AnnotationConfigWebApplicationContextTests{

@Configuration("myConfig")
	static class Config {

		@Bean
		public TestBean myTestBean() {
			return new TestBean();
		}
	}


	static class TestBean {
	}
}

        I believe you know AnnotationConfigApplicationContext, similarly, it can help us to scan all satisfied bean , you only provide a param  as a base-package. 

       There are two methods to register my definition bean ,it means that we can put our definition bean into BeanFactory.

        Let's look at a method on AnnotationConfigWebApplicationContext as follows.

	public void register(Class<?>... annotatedClasses) {
		Assert.notEmpty(annotatedClasses, "At least one annotated class must be specified");
		this.annotatedClasses.addAll(Arrays.asList(annotatedClasses));
	}

         So we can use the register method to get our definition bean.

@Test
public void registerSingleClass{

 AnnotationConfigWebApplicationContext ctx=new AnnotationConfigWebApplicationContext();
 ctx.register(Config.class);
  //  refresh BeanFactory, init or recreated.
 ctx.refresh();
 TestBean testBean=ctx.getBean(TestBean.class);
 assertNotNull(testBean);

}

        Tip:  we must execute  the ctx.refresh() before geting bean, otherwise, it will generate a exception of BeanFactory ! 

        We can easily get correct result !          Another way , we can use the setConfigLOcation()  method to register our definition bean.

	@Test
	@SuppressWarnings("resource")
	public void configLocationWithSingleClass() {
		AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
		ctx.setConfigLocation(Config.class.getName());
		ctx.refresh();

		TestBean bean = ctx.getBean(TestBean.class);
		assertNotNull(bean);
	}

        It is not hard to see how the method can be implemented as well.  

以上是关于Parsing the process of Bean Creation by AnnotationConfigWebApplicationContext的主要内容,如果未能解决你的问题,请参考以下文章

Error parsing HTTP request header Note: further occurrences of HTTP header parsing errors will be l

TOMCAT Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level.

Tomcat服务器重启失败:The server may already be running in another process, or a system process may be using

return three values that can be the lengths of the sides of a triangle,

The process of container handles the servlet request

The Concept of a Process