我的项目使用的是Spring3.2和Struts2(都使用了注释)。我正在通过Java配置Spring。当我尝试将一个服务注入到一个动作中时,我从Struts2得到以下错误:
>
找不到依赖项的[com.ceiwc.mvc.service.IUserService]类型的合格bean:至少需要一个合格的bean作为此依赖项的自动候选项。依赖项注释:{@org.SpringFramework.Beans.Factory.Annotation.AutoWired(required=true)}
无法自动连接字段:private com.ceiwc.mvc.service.IUserService com.ceiwc.mvc.action.loginaction.userService;嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:未找到依赖项得[com.ceiwc.mvc.service.IUserService]类型得合格bean:需要至少一个符合此依赖项自动候选条件得bean.依赖项注释:{@org.SpringFramework.Beans.Factory.Annotation.AutoWired(required=true)}
创建名为“com.ceiwc.mvc.action.loginAction”的bean时出错:注入autowired依赖项失败;嵌套异常为org.springframework.beans.factory.beanCreationException:无法自动连接字段:private com.ceiwc.mvc.service.iuserservice com.ceiwc.mvc.action.loginaction.userservice;嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:未找到依赖项得[com.ceiwc.mvc.service.IUserService]类型得合格bean:需要至少一个符合此依赖项自动候选条件得bean.依赖项注释:{@org.SpringFramework.Beans.Factory.Annotation.AutoWired(required=true)}
无法实例化命名空间“/login”中为“login”定义的操作com.ceiwc.mvc.Action.loginAction错误创建名为“com.ceiwc.mvc.Action.loginAction”的bean:注入autowired依赖项失败;嵌套异常为org.springframework.beans.factory.beanCreationException:无法自动连接字段:private com.ceiwc.mvc.service.iuserservice com.ceiwc.mvc.action.loginaction.userservice;嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:未找到依赖项得[com.ceiwc.mvc.service.IUserService]类型得合格bean:需要至少一个符合此依赖项自动候选条件得bean.依赖项注释:{@org.SpringFramework.Beans.Factory.Annotation.AutoWired(required=true)}
下面是StackTrace:
无法实例化命名空间“/login”中为“login”定义的操作com.ceiwc.mvc.Action.loginAction错误创建名为“com.ceiwc.mvc.Action.loginAction”的bean:注入autowired依赖项失败;嵌套异常为org.springframework.beans.factory.beanCreationException:无法自动连接字段:private com.ceiwc.mvc.service.iuserservice com.ceiwc.mvc.action.loginaction.userservice;嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:未找到依赖项得[com.ceiwc.mvc.service.IUserService]类型得合格bean:需要至少一个符合此依赖项自动候选条件得bean.依赖项注释:{@org.SpringFramework.Beans.Factory.Annotation.AutoWired(required=true)}
com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:316)
com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:397)
com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:194)
org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:536)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3715)
weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3681)
weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2277)
weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2183)
weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1454)
weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)
weblogic.work.ExecuteThread.run(ExecuteThread.java:178)
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.ceiwc.mvc.action.LoginAction': Injection
自动连线依赖项的%失败;嵌套异常为org.springframework.beans.factory.beanCreationException:无法自动连接字段:private com.ceiwc.mvc.service.iuserservice com.ceiwc.mvc.action.loginaction.userservice;嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:未找到依赖项得[com.ceiwc.mvc.service.IUserService]类型得合格bean:需要至少一个符合此依赖项自动候选条件得bean.依赖项注释:{@org.SpringFramework.Beans.Factory.Annotation.AutoWired(required=true)}
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1116)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:376)
com.opensymphony.xwork2.spring.SpringObjectFactory.autoWireBean(SpringObjectFactory.java:203)
com.opensymphony.xwork2.spring.SpringObjectFactory.buildBean(SpringObjectFactory.java:183)
com.opensymphony.xwork2.spring.SpringObjectFactory.buildBean(SpringObjectFactory.java:154)
com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:151)
com.opensymphony.xwork2.ObjectFactory.buildAction(ObjectFactory.java:121)
com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:297)
com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:397)
com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:194)
org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:536)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3715)
weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3681)
weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2277)
weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2183)
weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1454)
weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)
weblogic.work.ExecuteThread.run(ExecuteThread.java:178)
org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.ceiwc.mvc.service.IUserService
com.ceiwc.mvc.action.loginaction.userservice;嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:未找到依赖项得[com.ceiwc.mvc.service.IUserService]类型得合格bean:需要至少一个符合此依赖项自动候选条件得bean.依赖项注释:{@org.SpringFramework.Beans.Factory.Annotation.AutoWired(required=true)}
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)
org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1116)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:376)
com.opensymphony.xwork2.spring.SpringObjectFactory.autoWireBean(SpringObjectFactory.java:203)
com.opensymphony.xwork2.spring.SpringObjectFactory.buildBean(SpringObjectFactory.java:183)
com.opensymphony.xwork2.spring.SpringObjectFactory.buildBean(SpringObjectFactory.java:154)
com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:151)
com.opensymphony.xwork2.ObjectFactory.buildAction(ObjectFactory.java:121)
com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:297)
com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:397)
com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:194)
org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:536)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3715)
weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3681)
weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2277)
weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2183)
weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1454)
weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)
weblogic.work.ExecuteThread.run(ExecuteThread.java:178)
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.ceiwc.mvc.service.IUserService] found
org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:986)
org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:856)
org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:768)
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1116)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:376)
com.opensymphony.xwork2.spring.SpringObjectFactory.autoWireBean(SpringObjectFactory.java:203)
com.opensymphony.xwork2.spring.SpringObjectFactory.buildBean(SpringObjectFactory.java:183)
com.opensymphony.xwork2.spring.SpringObjectFactory.buildBean(SpringObjectFactory.java:154)
com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:151)
com.opensymphony.xwork2.ObjectFactory.buildAction(ObjectFactory.java:121)
com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:297)
com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:397)
com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:194)
org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:536)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3715)
weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3681)
weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2277)
weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2183)
weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1454)
weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)
weblogic.work.ExecuteThread.run(ExecuteThread.java:178)
@Configuration
@ImportResource({ "/WEB-INF/spring/spring-config.xml" })
@ComponentScan(basePackages = "com.ceiwc")
public class ApplicationConfig {
}
@Configuration
@EnableTransactionManagement
@Import(ApplicationConfig.class)
@PropertySource({ "classpath:db.properties" })
public class DataConfig {
@Autowired
Environment environment;
@Bean
public DriverManagerDataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getProperty("db.driver"));
dataSource.setUrl(environment.getProperty("db.url"));
dataSource.setUsername(environment.getProperty("db.username"));
dataSource.setPassword(environment.getProperty("db.password"));
return dataSource;
}
@Bean
public JpaVendorAdapter vendorAdapter() {
EclipseLinkJpaVendorAdapter vendorAdapter = new EclipseLinkJpaVendorAdapter();
vendorAdapter.setDatabase(Database.ORACLE);
vendorAdapter.setShowSql(true);
return vendorAdapter;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource());
entityManagerFactoryBean.setPackagesToScan(new String[] { environment
.getProperty("db.packagesToScan") });
entityManagerFactoryBean.setJpaVendorAdapter(vendorAdapter());
return entityManagerFactoryBean;
}
@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactoryBean()
.getObject());
return transactionManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
}
public interface IUserDao extends IGenericDao<User> {
public User findUserByUsernameAndPassword(String username, String password);
}
@Repository("userDAO")
@Transactional(readOnly = true)
public class UserDaoImpl extends GenericDaoImpl<User> implements IUserDao {
@Override
public User findUserByUsernameAndPassword(String username, String password) {
Query query = getEntityManager()
.createNativeQuery(
"Select u From User Where username = :username And password = :password");
query.setParameter("username", username);
query.setParameter("password", password);
return (User) query.getSingleResult();
}
}
public interface IUserService {
public User validateUser(String username, String password);
}
@Service("userService")
public class UserServiceImpl implements IUserService {
@Autowired
private IUserDao userDao;
@Override
public User validateUser(String username, String password) {
return userDao.findUserByUsernameAndPassword(username, password);
}
}
@SuppressWarnings("serial")
@Namespace("/Login")
@Component
public class LoginAction extends ActionSupport {
private User user;
private String updateFlag;
@Autowired
private IUserService userService;
@Action(value = "login", results = { @Result(name = "success", location = "/pages/login/login.jsp") })
public String login() {
updateFlag = "Y";
return SUCCESS;
}
@Action(value = "validate", results = {
@Result(name = "success", location = "/pages/menu/index.jsp"),
@Result(name = "login", location = "/pages/login/login.jsp") })
public String validateUser() {
if (updateFlag == null) {
return LOGIN;
}
if (userService.validateUser(user.getUsername(), user.getPassword()) == null) {
addActionError("You are an invalid user");
return LOGIN;
}
// TODO: Add session aware info
return SUCCESS;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getUpdateFlag() {
return updateFlag;
}
public void setUpdateFlag(String updateFlag) {
this.updateFlag = updateFlag;
}
public IUserService getUserService() {
return userService;
}
public void setUserService(IUserService userService) {
this.userService = userService;
}
}
下面是我的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>menuAdministration</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Helps with wiring the application together -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Needed to manage the Java based @Configuration classes for Spring -->
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.ceic.config</param-value>
</context-param>
<!-- Handles Strut2 URL requests -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
我错过了什么?我有@component
、@service
和@repository
原型。所以,我不明白为什么Spring找不到我的豆子注射。
我找到了解决这个问题的两种方法。
ContextConfigLocation
设置正确。这两个步骤解决了这个问题。
我正在运行一个JUnit测试,嗯,我的Spring启动项目,我是这样写的: 找到依赖项[com.br.suppcomm.ocp.dao.logindao]得[com.br.suppcomm.ocp.dao.logindao]:需要至少1个具有此依赖项自动候选资格得bean.依赖项注释:{@org.SpringFramework.Beans.Factory.Annotation.AutoWired(
我正在使用spring开发一个程序,我面临着这个问题,它说: 根据我的观察,问题出在我的服务/服务实施中 服务: 和我的ServiceImpl 请帮助我解决问题,并提供您的解决方案,thx提前。
我有测试Spring Data JPA的示例测试程序,但看起来存储库没有生成。 我的配置: 用户实体:
我有这样的项目结构: persistence.xml: xml(如您所见,我使用的是上下文类,在这里我将使用JSON): WebAppConfig.class: HolidayController.Class HolidayRepository.Class: 请帮帮我。
当我试图在SpringMVC中使用依赖注入时,我遇到了这个错误。 没有匹配的类型[com.sachin.dao.斯托克道]的bean找到依赖项:预计至少有1 bean有资格作为该依赖项的自动连接候选。依赖注释:{};嵌套异常是org.springframework.beans.factory.NoSuchBean定义异常:没有找到类型[com.sachin.dao.Stock道]的匹配bean的依
我用SpringMVC4开发了一个web应用程序。对于DAO层,我添加了一个类GenericDAO:`public class GenericDAO{ ### 如下所示声明我的DAO会在启动我的应用程序时产生错误: 错误是: 在org。springframework。豆。工厂支持DefaultListableBeanFactory。raiseNoMatchingBeanFound(DefaultL