Java class gets 5 ways of Bean in spring _java

Source: Internet
Author: User

There are a number of ways to get the beans in spring, and again, summarize:
First: Save the ApplicationContext object when initializing

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

Description: This approach applies to stand-alone applications that adopt the spring framework, requiring that the program manually initialize spring through a configuration file.
The second type: get the ApplicationContext object from the tool class provided by spring

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
1, these two methods are suitable to adopt the Spring frame b/s system, obtain the ApplicationContext object through the ServletContext object, then obtains the necessary class instance through it;
2. The first method throws an exception when a failure is obtained, and the second way returns NULL.
The third type: inherit from abstract class Applicationobjectsupport

Description: The Getapplicationcontext () method provided by the abstract class Applicationobjectsupport provides a convenient way to get to the ApplicationContext instance and get the bean in the spring container. When spring initializes, the ApplicationContext object is injected through the Setapplicationcontext (ApplicationContext context) method of the abstract class.
The fourth kind: inherit from abstract class Webapplicationobjectsupport

Description: Similar to the above method, obtain the Webapplicationcontext instance by calling Getwebapplicationcontext ();
The fifth kind: 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.

Although spring provides the following three ways to implement a class or interface that inherits or implements the corresponding classes or interfaces in a normal class, you can get the ApplicationContext object of spring. However, it is important to note that the normal Java class that inherits or implements these abstract classes or interfaces must be configured in the spring's configuration file (that is, the Application-context.xml file), otherwise the obtained ApplicationContext object will be null.

The following shows how to get the bean in the spring container by implementing an interface Applicationcontextaware:
First, customize a class that implements the Applicationcontextaware interface, and implement the method inside:

Package com.ghj.tool;

Import org.springframework.beans.BeansException;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.ApplicationContextAware;

public class Springconfigtool implements Applicationcontextaware {//extends applicationobjectsupport{

 private static ApplicationContext AC = null;
 private static Springconfigtool Springconfigtool = null;

 Public synchronized Static Springconfigtool init () {
 if (Springconfigtool = null) {
  Springconfigtool = new Spring Configtool ();
 }
 return springconfigtool;
 }

 public void Setapplicationcontext (ApplicationContext applicationcontext) throws beansexception {
 AC = ApplicationContext;
 }

 Public synchronized static Object Getbean (String beanname) {return
 Ac.getbean (beanname);
 }
}

Second, configure the Applicationcontext.xml file:

Copy Code code as follows:
<bean id= "Springconfigtool" class= "Com.ghj.tool.SpringConfigTool"/>

You can finally get to the appropriate bean in the spring container by using the following code:
Copy Code code as follows:
Springconfigtool.getbean ("Beanid");

Note that when the server starts the spring container initialization, you cannot get the spring container in the following ways:

Import Org.springframework.web.context.ContextLoader; 
Import Org.springframework.web.context.WebApplicationContext; 
 
Webapplicationcontext WAC = Contextloader.getcurrentwebapplicationcontext (); 
Wac.getbean (Beanid);

The above is the entire content of this article, I hope to help you learn.

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.