我无法在服务层(ServiceContext.getBean(“beanName”))中获取spring bean。不过,我能够在servlet中获得bean。在接下来的课程中,我做错了什么?
package com.ekaplus.presentation.common;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class ServiceContext implements ApplicationContextAware{
private static ApplicationContext applicationContext;
@SuppressWarnings("static-access")
public void setApplicationContext(ApplicationContext ctx)throws BeansException {
this.applicationContext=ctx;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static Object getBean(String beanName)
{
return applicationContext.getBean(beanName);
}
}发布于 2010-11-09 14:09:20
在ear和web-inf库中有一些库问题。它现在起作用了。
发布于 2010-11-01 15:16:55
试着在没有静态访问的情况下这样做。像这样的Smt (仅用于测试)
class ServiceContext {
public static Object getBean(final String beanName){
return new ApplicationContextAware(){
Object res;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
res = applicationContext.getBean(beanName);
}
Object getBean(){
return res;
}
}.getBean();
}
}发布于 2010-11-06 16:27:58
是由Spring管理的Servicecontext。看起来不是。如果它不是由Spring管理的,它就不能在你的Servicecontext对象中注入ApplicationContext。
如果你能说出你正在努力实现的目标,那就更容易提出建议了。尤其是ServiceContext的作用是什么?beans不能自动连接吗?
https://stackoverflow.com/questions/4066953
复制相似问题