我试图在我的代码中使用ApplicationContextAware并提供以下帮助: https://jerlinworld.wordpress.com/2009/07/20/making-your-spring-application-context-aware/
但正在返回异常:
org.springframework.beans.factory.NoSuchBeanDefinitionException:未定义[com.example.test.service.RoleService]类型的限定bean
的web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/business-config.xml, classpath:spring/web-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
业务-config.xml中:
<bean id="applicationContextProvider" class="com.example.test.extension.spring.ServiceLocator" />
<beans profile="allcomponents">
<context:component-scan base-package="com.example.test" />
</beans>
我的WebBean(例外):
try {
ServiceLocator.getBean(RoleService.class).save(role);
} catch (Exception e) {
e.printStackTrace();
}
ServiceLocator.java:
public class ServiceLocator implements ApplicationContextAware {
private static ApplicationContext ctx = null;
public static <T> T getBean(Class<T> clazz) {
return ctx.getBean(clazz);
}
public static <T> T getBean(String beanName, Class<T> clazz) {
return ctx.getBean(beanName, clazz);
}
@SuppressWarnings("static-access")
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
// Assign the ApplicationContext into a static method
this.ctx = ctx;
}
public static String getProperty(String key) {
return ctx.getEnvironment().getProperty(key);
}
}
RoleService:
public interface RoleService {
Role save(Role role);
}
RoleServiceImpl:
@Service
public class RoleServiceImpl implements RoleService {
@Autowired
private RoleRepository roleRepository;
@Override
@Transactional
public Role save(Role role) {
return this.roleRepository.save(role);
}
}
编辑:
完整堆栈跟踪:
org.springframework.beans.factory.NoSuchBeanDefinitionException:未定义[com.examle.test.service.RoleService]类型的限定bean at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:371) 在org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:331) 在org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:968) 在com.example.test.extension.spring.ServiceLocator.getBean(ServiceLocator.java:11) 在com.example.test.web.IndexWebBean.getTest(IndexWebBean.java:30) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:497) 在javax.el.BeanELResolver.getValue(BeanELResolver.java:97) at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176) at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203) 在org.apache.el.parser.AstValue.getValue(AstValue.java:169) at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184) at com.sun.faces.facelets.el.ELText $ ELTextVariable.writeText(ELText.java:238) at com.sun.faces.facelets.compiler.TextInstruction.write(TextInstruction.java:85) at com.sun.faces.facelets.compiler.UIInstructions.encodeBegin(UIInstructions.java:82) 在com.sun.faces.facelets.compiler.UILeaf.encodeAll(UILeaf.java:183) 在javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859) 在javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859) 在com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:456) at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:133) at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120) 在com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 在com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219) 在javax.faces.webapp.FacesServlet.service(FacesServlet.java:647) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) 在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) 在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) 在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) 在org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617) 在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) 在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518) 在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091) at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:668) 在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1527) 在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1484) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61) 在java.lang.Thread.run(Thread.java:745)
在我的business-config.xml中:
<beans profile="allcomponents">
<context:component-scan base-package="com.make3.mymed" />
</beans>
答案 0 :(得分:0)
我发现了问题!
我在尝试:
<beans profile="allcomponents">
<context:component-scan base-package="com.make3.mymed" />
</beans>
但是当我搬到:
<context:component-scan base-package="com.make3.mymed" />
工作!
为什么呢?我不能用这个吗?
<context:component-scan base-package="com.make3.mymed" />