![](/img/trans.png)
[英]How to access beans from the applicationContext.xml in my service layer.
[英]How to access spring managed beans from service layer
我無法在服務層中獲取spring bean(ServiceContext.getBean(“ beanName”))。 我可以在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);
}
}
嘗試在沒有靜態訪問權限的情況下進行操作。 像這樣的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();
}
}
是您的Servicecontext由Spring管理。 看起來不是。 而且,如果它不是由Spring管理的,則無法將ApplicationContext注入Servicecontext對象中。
如果您能說出您要實現的目標,那么將比您更容易提出建議。 特別是ServiceContext的作用是什么? 豆子不能自動接線嗎?
ear和web-inf庫中存在一些庫問題。 現在正在工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.