I looked at some of docs for Contextloaderlistener
and application context and based on that what I understand is Application Context is the container initialized by a ContextLoaderListener
and load all the spring config files. What I am not clear about is which one is responsible for the bean creation based on the meta data that is provided. Is it the ContextLoaderListener
or the Application Context.
The ApplicationContext creates the beans utilising a BeanFactory, which is the actual component that turns xml/annotations into classes and manages the lifecycle of each.
A ContextLoaderListener is used when spring is running inside another container (eg a servlet engine like tomcat) to detect the startup of the application and initialise the ApplicationContext. When the spring is used in another context -eg a java program run through a main method - the application can create the AppContext directly without needing the ContextLoaderListener like this:
public class Main {
public static void main(String[] args) throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"context.xml");
Foo foo = (Foo) ctx.getBean("fooBean");
foo.doSomethingCool();
} }
The idea behind the ApplicationContext in Spring is that in order to properly inject objects where they are needed, some thing needs to be aware of the configuration the user specifies and inject dependencies based on this configuration. The ApplicationContext is the thing that understands the user's wishes in terms of where and what should be injected (as well as other things such as AOP pointcuts and such) based on the configuration a user provides, either through an xml file or annotations. The context named after your servlet ( [servlet-name]-context.xml ) is a WebApplicationContext and is a child context of the main ApplicationContext , which is created from the files listed in the contextConfigLocation and loaded by the ContextLoaderListener. The child can access any beans defined in the parent, but the parent has no access to beans defined in the child ContextLoaderListener is a ServletListener. So in the JSP/Servlet spec a Servlet Listener gets called by the container when certain
events occur. In this case it gets called right after when the
ServletContext is created for the web application. When it gets
called, inside the ContextLoaderListener it will create/instantiate
an ApplicationContext and load in the xml file you set with the
context-param name and value tags. The contextConfigLocation param is used in conjunction with this Spring listener org.springframework.web.context.ContextLoaderListener ContextLoaderListener executes the bootstrap of the listener to start up Spring's root WebApplicationContext ContextLoaderListener is a class that starts Spring container. Basically every Spring application consists of several beans and wiring (declarative description of which beans depend on each other). This description was historically written in XML (these days we have annotations, Java configuration, CLASSPATH scanning, etc.) ContextLoaderListener reads that file, finds your classes, instantiates them and wires. All your beans are then placed inside a container.
链接地址:
http://www.djcxy.com/p/82040.html
上一篇:
春天的范围差异
下一篇:
在bean创建中使用Spring contextloaderlistener角色