Spring reads the configuration file and gets several ways to get the bean

Source: Internet
Author: User

Spring reads the configuration file and gets several ways to get the bean


method One: Save the ApplicationContext object when initializing
Code:

ApplicationContext ac = new Filesystemxmlapplicationcontext ("Applicationcontext.xml");
Ac.getbean ("Beanid");


Description
This approach applies to stand-alone applications that use the spring framework, and requires programs to manually initialize spring with a configuration file.


Method Two: Get the ApplicationContext object from the tool class provided by spring
Code:

Import Org.springframework.web.context.support.WebApplicationContextUtils;
ApplicationContext AC1 = webapplicationcontextutils.getrequiredwebapplicationcontext (ServletContext SC)
ApplicationContext AC2 = webapplicationcontextutils.getwebapplicationcontext (ServletContext SC)
Ac1.getBean (" Beanid ");
Ac2.getbean ("Beanid");


Description
This approach is suitable for the B/s system using the spring framework, obtaining the ApplicationContext object through the ServletContext object, and then obtaining the required class instances through it.
The difference between the above two tools is that the former throws an exception when a failure is obtained, and the latter returns null.


Method Three: Inherit from abstract class Applicationobjectsupport
Description
Abstract class Applicationobjectsupport provides a Getapplicationcontext () method that can be easily obtained from ApplicationContext. When spring initializes, the ApplicationContext object is injected through the Setapplicationcontext (ApplicationContext context) method of the abstract class.


method Four: Inherit from abstract class Webapplicationobjectsupport
Description
Similar to the above method, call Getwebapplicationcontext () to get Webapplicationcontext


method Five: Implement Interface Applicationcontextaware
Description
Implements the Setapplicationcontext (ApplicationContext context) method of the interface and saves the ApplicationContext object. When spring initializes, the ApplicationContext object is injected by this method.

The above methods are suitable for different situations, please choose the appropriate method according to the specific circumstances.

What is worth mentioning here is that the classes used in these methods in the system are actually tightly coupled to the spring framework, because these classes are known to run on the spring framework, so in the system, you should minimize such applications so that the system is as independent as possible from the current running environment. Try to get the service providers you need through DI.


Reference article: http://blog.knowsky.com/185540.htm


In a spring Web project, if you want to get a bean manually, you can use the second method to test it.



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.