私のプロジェクトでは、Spring 3.2 と Struts2 を使用しています (どちらも注釈を使用しています)。Java を使用して Spring を構成しています。サービスをアクションに挿入しようとすると、Struts2 から次のエラーが発生します。
依存関係のタイプ [com.ceiwc.mvc.service.IUserService] の適格な Bean が見つかりません: この依存関係のオートワイヤー候補として適格な Bean が少なくとも 1 つ必要です。依存関係アノテーション: {@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: No qualing bean of type [com.ceiwc.mvc.service.IUserService] が依存関係に見つかりません: この依存関係のオートワイヤー候補として適格な少なくとも 1 つの Bean が必要です。依存関係アノテーション: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
「com.ceiwc.mvc.action.LoginAction」という名前の Bean の作成中にエラーが発生しました: 自動配線された依存関係の注入に失敗しました。ネストされた例外は 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: No qualing bean of type [com.ceiwc.mvc.service.IUserService] が依存関係に見つかりません: この依存関係のオートワイヤー候補として適格な少なくとも 1 つの Bean が必要です。依存関係アノテーション: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
アクション com.ceiwc.mvc.action.LoginAction をインスタンス化できません。名前空間 '/Login' の 'login' に定義されています'com.ceiwc.mvc.action.LoginAction' という名前の Bean を作成中にエラーが発生しました: 自動配線された依存関係の注入に失敗しました。ネストされた例外は 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: No qualing bean of type [com.ceiwc.mvc.service.IUserService] が依存関係に見つかりません: この依存関係のオートワイヤー候補として適格な少なくとも 1 つの Bean が必要です。依存関係アノテーション: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
スタックトレースは次のとおりです。
アクション com.ceiwc.mvc.action.LoginAction をインスタンス化できません。名前空間 '/Login' の 'login' に定義されています'com.ceiwc.mvc.action.LoginAction' という名前の Bean を作成中にエラーが発生しました: 自動配線された依存関係の注入に失敗しました。ネストされた例外は 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: No qualing bean of type [com.ceiwc.mvc.service.IUserService] が依存関係に見つかりません: この依存関係のオートワイヤー候補として適格な少なくとも 1 つの 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: Could not autowire field: private com.ceiwc.mvc.service.IUserService com.ceiwc.mvc.action.LoginAction.userService; です。ネストされた例外は org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualing bean of type [com.ceiwc.mvc.service.IUserService] が依存関係に見つかりません: この依存関係のオートワイヤー候補として適格な少なくとも 1 つの 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: No qualing bean of type [com.ceiwc.mvc.service.IUserService] が依存関係に見つかりません: この依存関係のオートワイヤー候補として適格な少なくとも 1 つの 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
依存関係の場合: この依存関係のオートワイヤー候補として適格な Bean が少なくとも 1 つ必要です。依存関係アノテーション: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
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)
Spring の Java 構成は次のとおりです。
@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();
}
}
これが私のDAOです:
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 がインジェクション用の Bean を見つけられない理由がわかりません。