普通java类获取spring容器的方法

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/maqingbin8888/article/details/84678465

参考文章连接

实现ApplicationContextAware接口会往实现类里面启动的时候注入一个applicationContext

 工具类SpringContextUtils

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component("springContextUtils")
public class SpringContextUtils implements ApplicationContextAware {
    private static ApplicationContext applicationContext;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String beanId) {
        return (T) applicationContext.getBean(beanId);
    }
    public static <T> T getBean(Class<T> requiredType) {
        return (T) applicationContext.getBean(requiredType);
    }
}

如何使用

展开阅读全文

普通获取spring中的bean.....

09-15

rnpackage com.proxy;rnrnimport org.springframework.beans.BeansException;rnimport org.springframework.context.ApplicationContext;rnimport org.springframework.context.ApplicationContextAware;rnimport org.springframework.context.support.ApplicationObjectSupport;rnimport org.springframework.web.context.support.WebApplicationObjectSupport;rnrn/**rn * Desc:rn * Copyright: Copyright(c)Sinosoft 2012rn * Attention attention:rn * Exception:rn * VERSION: 1.0 rn * DATE: 2012-9-15 下午06:02:56 rn * Author: MaoHaiTaorn * Email:imusic.mht@gmail.com rn * URL:http://weibo.com/u/1788282477rn * CHANGE/COMMENT: create rn */rnpublic final class ToolSpring extends ApplicationObjectSupport rn public static ApplicationContext applicationContext = null; rn public static String string="";rn @Override rn protected void initApplicationContext(ApplicationContext context) throws BeansException rn // TODO Auto-generated method stub rn super.initApplicationContext(context); rn if(ToolSpring.applicationContext == null) rn ToolSpring.applicationContext = context; rn NetTest.applicationContext= context;rn System.out.println(); rn System.out.println(); rn System.out.println("---------------------------------------------------------------------"); rn System.out.println("========ApplicationContext配置成功,在普通类可以通过调用ToolSpring.getAppContext()获取applicationContext对象,applicationContext="+applicationContext+"========"); rn System.out.println("---------------------------------------------------------------------"); rn System.out.println(); rn string ="S";rn NetTest.string ="222";rn rn new NetTest(context,"12312");rn System.out.println("applicationContext="+applicationContext); rn rn rn public static ApplicationContext getAppContext() rn return applicationContext; rn rn public static Object getBean(String name) rn return getAppContext().getBean(name); rn rn rnrnrn配置文件:rn rnrn其他类的 main方法:rnrn public static void main(String[] args) rn// ServiceDownProcessor sdp = new ServiceDownProcessor(ServletActionContext.getRequest());rn rn System.out.println("applicationContext="+ToolSpring.getAppContext()+"|ToolSpring.getSpringServices()=");rn rn System.out.println("applicationContext="+getApplicationContext()+"|ToolSpring.getSpringServices()=");rn System.out.println("applicationContext="+ToolSpring.string+"|ToolSpring.getSpringServices()=");rnrn rn System.out.println("applicationContext="+com.proxy.NetTest.string+"|ToolSpring.getSpringServices()=");rnrn rn// sdp.createNodePrefixMap();rn rn rnrnrn输出:rn applicationContext=null|ToolSpring.getSpringServices()=rnapplicationContext=null|ToolSpring.getSpringServices()=rnapplicationContext=|ToolSpring.getSpringServices()=rnapplicationContext=|ToolSpring.getSpringServices()=rnrnrn为什么在 main方法里面去不到值呢。 很急。 知道的打下帮帮忙!!!rnrn rn 论坛

没有更多推荐了,返回首页