- import org.springframework.beans.BeansException;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.ApplicationContextAware;
- /**
- * 获取spring信息
- *
- * Created by zyb on 2016年3月14日.
- */
- public class SpringTool implements ApplicationContextAware {
- private static ApplicationContext applicationContext = null;
- @Override
- public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
- // TODO Auto-generated method stub
- if (SpringTool.applicationContext == null) {
- SpringTool.applicationContext = applicationContext;
- }
- }
- public static ApplicationContext getApplicationContext() {
- return applicationContext;
- }
- public static Object getBean(String name) {
- return getApplicationContext().getBean(name);
- }
- }
spring xml配置
- <!-- 通过SpringTool.getBean(beanid)获取spring管理的bean -->
- <bean class="com.task.util.SpringTool" lazy-init="false"></bean>
应用一些不能注入的地方
【本文是51CTO专栏作者张勇波的原创文章,转载请通过51CTO获取作者授权】