applicaitonContext未sql注入解决方案.怎么解决

activiti,我在任务监听TaskListener的实现类里想注入spring的bean要怎么做?我@Autowired注入都是null - 开源中国社区
当前访客身份:游客 [
当前位置:
activiti,我在任务监听TaskListener的实现类里想注入spring的bean要怎么做?我@Autowired注入都是null
共有3个答案
<span class="a_vote_num" id="a_vote_num_
把实现类交给spring管理即可。
--- 共有 2 条评论 ---
: 用过,一个工作流引擎,交给spring管理不善只加那2个注解,你得把这个类在spring配置文件中配置下的。
(1年前)&nbsp&
加了@Service和@Transaction也不行,拿到的还是空。你用过activiti吗
(1年前)&nbsp&
<span class="a_vote_num" id="a_vote_num_
public class SpringContextHolder implements ApplicationContextAware, DisposableBean {
private static ApplicationContext applicationContext =
private static Logger LOGGER = Logger.getLogger(SpringContextHolder.class);
* 取得存储在静态变量中的ApplicationContext.
public static ApplicationContext getApplicationContext() {
assertContextInjected();
return applicationC
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
@SuppressWarnings("unchecked")
public static &T& T getBean(String name) {
LOGGER.debug("从SpringContextHolder中取出Bean:" + name);
assertContextInjected();
return (T) applicationContext.getBean(name);
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
public static &T& T getBean(Class&T& requiredType) {
assertContextInjected();
return applicationContext.getBean(requiredType);
* 清除SpringContextHolder中的ApplicationContext为Null.
public static void clearHolder() {
LOGGER.debug("清除SpringContextHolder中的ApplicationContext:"
+ applicationContext);
applicationContext =
* 实现ApplicationContextAware接口, 注入Context到静态变量中.
public void setApplicationContext(ApplicationContext applicationContext) {
logger.debug("注入ApplicationContext到SpringContextHolder:{}", applicationContext);
if (SpringContextHolder.applicationContext != null) {
LOGGER.warn("SpringContextHolder中的ApplicationContext被覆盖, 原有ApplicationContext为:" + SpringContextHolder.applicationContext);
SpringContextHolder.applicationContext = applicationC // NOSONAR
* 实现DisposableBean接口, 在Context关闭时清理静态变量.
public void destroy() throws Exception {
SpringContextHolder.clearHolder();
* 检查ApplicationContext不为空.
private static void assertContextInjected() {
if(applicationContext == null) {
throw new IllegalStateException("applicaitonContext属性未注入, 请在applicationContext.xml中定义SpringContextHolder.");
XxxService xxxlService = SpringContextHolder.getBean(XxxService.class);
<span class="a_vote_num" id="a_vote_num_
解决了,TaskListener的实现类里面确实是拿不到Spring的bean的,它是后面new出来的对象,我用表达式Expression就可以使用Spring的bean了
更多开发者职位上
有什么技术问题吗?
类似的话题1316人阅读
JavaSE(18)
J2EE(16)
import org.springframework.context.ApplicationC
import org.springframework.context.ApplicationContextA
* 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext.
public class SpringContextHolder implements ApplicationContextAware {
private static ApplicationContext applicationC
* 实现ApplicationContextAware接口的context注入函数, 将其存入静态变量.
public void setApplicationContext(ApplicationContext applicationContext) {
SpringContextHolder.applicationContext = applicationC // NOSONAR
* 取得存储在静态变量中的ApplicationContext.
public static ApplicationContext getApplicationContext() {
checkApplicationContext();
return applicationC
* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
@SuppressWarnings(&unchecked&)
public static &T& T getBean(String name) {
checkApplicationContext();
return (T) applicationContext.getBean(name);
* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
@SuppressWarnings(&unchecked&)
public static &T& T getBean(Class&T& clazz) {
checkApplicationContext();
return (T) applicationContext.getBeansOfType(clazz);
* 清除applicationContext静态变量.
public static void cleanApplicationContext() {
applicationContext =
private static void checkApplicationContext() {
if (applicationContext == null) {
throw new IllegalStateException(&applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder&);
用于持有ApplicationContext,可以使用SpringContextHolder.getBean('xxxx')的静态方法得到spring&bean对象
原文地址:
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:121675次
积分:2163
积分:2163
排名:第12891名
原创:96篇
转载:12篇
评论:44条
(3)(5)(3)(1)(3)(1)(4)(8)(1)(1)(4)(1)(2)(7)(2)(1)(2)(2)(1)(6)(1)(2)(1)(1)(1)(1)(2)(1)(5)(1)(1)(1)(1)(2)(1)(1)(2)(2)(3)(1)(1)(3)(2)(1)(3)(2)(1)(6)trackbacks-0
在Spring应用中创建全局获取ApplicationContext对象
1、需要创建一个类,实现接口ApplicationContextAware的setApplicationContext方法。
2、在创建的这个类中保存一个静态的ApplicationContext对象,然后通过静态的方法返回。
如下,下面是SpringSide的实现,供参考:
* Copyright (c)
* Licensed under the Apache License, Version 2.0 (the "License");
package org.springside.modules.test.
import mons.lang3.V
import org.slf4j.L
import org.slf4j.LoggerF
import org.springframework.beans.factory.DisposableB
import org.springframework.context.ApplicationC
import org.springframework.context.ApplicationContextA
* 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候取出ApplicaitonContext.
* @author calvin
public class SpringContextHolder implements ApplicationContextAware, DisposableBean {
private static ApplicationContext applicationContext = null;
private static Logger logger = LoggerFactory.getLogger(SpringContextHolder.class);
* 取得存储在静态变量中的ApplicationContext.
public static ApplicationContext getApplicationContext() {
assertContextInjected();
return applicationC
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
public static &T& T getBean(String name) {
assertContextInjected();
return (T) applicationContext.getBean(name);
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
public static &T& T getBean(Class&T& requiredType) {
assertContextInjected();
return applicationContext.getBean(requiredType);
* 清除SpringContextHolder中的ApplicationContext为Null.
public static void clearHolder() {
logger.debug("清除SpringContextHolder中的ApplicationContext:" + applicationContext);
applicationContext = null;
* 实现ApplicationContextAware接口, 注入Context到静态变量中.
public void setApplicationContext(ApplicationContext applicationContext) {
logger.debug("注入ApplicationContext到SpringContextHolder:{}", applicationContext);
if (SpringContextHolder.applicationContext != null) {
logger.warn("SpringContextHolder中的ApplicationContext被覆盖, 原有ApplicationContext为:"
+ SpringContextHolder.applicationContext);
SpringContextHolder.applicationContext = applicationC //NOSONAR
* 实现DisposableBean接口, 在Context关闭时清理静态变量.
public void destroy() throws Exception {
SpringContextHolder.clearHolder();
* 检查ApplicationContext不为空.
private static void assertContextInjected() {
Validate.validState(applicationContext != null,
"applicaitonContext属性未注入, 请在applicationContext.xml中定义SpringContextHolder.");
阅读(...) 评论()

我要回帖

更多关于 spring 解决循环注入 的文章

 

随机推荐