Spring Direct fetch Bean definition

Source: Internet
Author: User

Ext.: http://blog.csdn.net/sdandan/article/details/7911241

————————————————————————————————————————

Beanfactory is a factory that manages beans (that is, a container for spring), and it can manage either a bean or a factorybean (which would call Factorybean's GetObject () to get a real bean). Factorybean is a factory bean, managed by beanfactory. Let's take a look at Beanfactory:
Publicinterface beanfactory {String factory_bean_prefix = "&"; Object Getbean (String name); Object Getbean (String name, Class requiredtype); Boolean Containsbean (String name); Boolean Issingleton (String name); Boolean Isprototype (String name); Boolean istypematch (String name, Class targetType); Class GetType (String name); string[] getaliases (String name); } 
The Beanfactory caller only needs to call the Getbean method to get a reference to the specified bean.ApplicationContextinherit frombeanfactory, it hasbeanfactoryProvides all the configuration framework and basic functionality, andApplicationContextAdded a stronger function to it. Therefore, in the application of the Java EE Environment, It is best to use subclasses under ApplicationContext. Web apps now have two options through the configuration loader in XML:Contextloaderlistener and Contextloaderservlet.
The two are functionally identical, but one is based on the newly introduced listener interface implementation in the Servlet2.3 version , and the other based on the servlet interface. The development can be selected according to the actual situation of the target Web container. In

The configuration is very simple and is added in Web. xml:
<listener> <listener-class> Org.springframework.web.context.ContextLoaderListener </listener-class> </Listener>

Or:
<servlet> <servlet-name>context</servlet-name> <servlet-class> Org.springframework.web.context.ContextLoaderServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet>

With the above configuration, theWEB container will automatically load /web-inf/applicationcontext.xml initialization.
ApplicationContext instances, which can be specified by Context-param If you need to specify a configuration file location:
<< Span lang= "en-US" >context-param>    <param-name> contextconfiglocation</param-name >    <param-value >/web-inf/myapplicationcontext.xml</ param-value> </context-param>

Once the configuration is complete, you can
The Webapplicationcontextutils.getwebapplicationcontext method gets the ApplicationContext reference in the Web App . Such as:
ApplicationContext Ctx=webapplicationcontextutils.getwebapplicationcontext (); loginaction action= (loginaction) Ctx.getbean ("action");
The ApplicationContext instance is loaded automatically at startup. In a Web application, although almost all user code managed by Beanfactory does not need to know beanfactory, Beanfactory is instantiated in some way in the internal implementation. For standalone referencing programs or some servlet,action,jsp you can instantiate beanfactory using the following methods:
Resource Resource = new filesystemresource ("Beans.xml"); beanfactoryFactory = new xmlbeanfactory (Resource);
Classpathresource resource = new classpathresource ("Beans.xml"); beanfactoryFactory = new xmlbeanfactory (Resource);

ApplicationContext context = new classpathxmlapplicationcontext (new string[] {" Applicationcontext.xml "," Applicationcontext-part2.xml "}); beanfactory Factory = (beanfactory) context;

Spring Direct fetch Bean definition

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.