Obtain springbean from a web project -- Obtain springbean from a non-spring component

Source: Internet
Author: User

Recently, when I was working on a project, I found a problem: the Spring IoC container cannot be referenced on the Web (or arbitrarily referenced ). We made spring automatically assemble in the configuration file, but did not retain the applicationcontext instance. If we want to get the same applicationcontext in any location of our project to obtain the resources in the IOC container, we need spring to fill the Context Environment with what we can get, for example, the following method (from network resources ):

You can customize a tool class to implement the custom applicationcontextaware interface. The interface is implemented by setapplicationcontext and let it serve us, Because spring will fill in the context environment when loading itself. All we need to do is save the obtained applicationcontext and use it.

1 package Org. coderecord. CCMS. web. action. util; 2 3 Import Org. springframework. beans. beansexception; 4 Import Org. springframework. context. applicationcontext; 5 import Org. springframework. context. applicationcontextaware; 6 7/** 8 * Spring IoC context tool Class 9*10 * @ author Ryan shaw11 * 12 */13 public class springutil implements applicationcontextaware {14 15/** 16 * Current ioc17 * /18 Private Static applicationcontext; 19 20/** 21 * set the current context. This method is automatically assembled by spring 22 */23 @ override24 public void setapplicationcontext (applicationcontext arg0) 25 throws beansexception {26 applicationcontext = arg0; 27} 28 29/** 30 * Get bean31 * 32 * @ Param id33 * bean id34 * @ return35 */36 public static object GetObject (string ID) from current IOC) {37 object = NULL; 38 object = applicationcontext. getbean (ID); 39 return object; 40} 41 42}

The above class is what we need, and the setapplicationcontext in it is implemented in the interface, spring will automatically fill in. Let's register it in the spring configuration file:

1 <bean id="springUtil" class="org.coderecord.ccms.web.action.util.SpringUtil" />

In this way, spring gives us what we need.

We can do this as needed:

1 YouClass obj = (YouClass)SpringUtil.getObject("beanid");

Of course, the premise is that you need to let spring automatically assemble.

  

In method 2, annotations and static methods are used to generate the springfactory object.

From network resources, modified on 15:25:57

The above method has a problem: configuration is required. Spring2.5 and later versions actually add annotations to inject dependencies. The following code may be better:

1 package Org. ahe. util; 2 3 Import Org. springframework. beans. factory. beanfactory; 4 Import Org. springframework. beans. factory. annotation. autowired; 5 import Org. springframework. web. context. support. springbeanautowiringsupport; 6 7 public class springwiredbean extends springbeanautowiringsupport {8 9/** 10 * Automatic Assembly annotation will allow spring to inject sample 11 */12 @ autowired13 private beanfactory by type matching; 14 15 private springwiredbean () {16} 17 18 Private Static springwiredbean instance; 19 20 static {21 // static block, initialize instance 22 instance = new springwiredbean (); 23} 24 25/** 26 * instance method 27 * Get instance 28*29 * @ Param beanid30 * @ return31 */32 public object getbeanbyid (string beanid) {33 return beanfactory. getbean (beanid); 34} 35 36 public static springwiredbean getinstance () {37 return instance; 38} 39}

If the @ autowired annotation is used for automatic assembly, the springbeanautowiringsupport class can be inherited. Of course, you can also use annotations such as @ component. If annotations are used, the configuration needs to be changed. However, we can use multiple configurations to support spring annotations, so it is better. As follows:

1 <context:component-scan base-package="org.ahe"></context:component-scan>

This configuration enables annotations in all packages under org. ahe (you can still perform more settings by matching the Regular Expression of sub-tags.

 

 

You are welcome to move to our chat group and pass the time together when you are bored:

Or contact me via QQ:

 

(Last editing time: 16:59:45)

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.