这是我的场景。我的项目正在使用 Spring (3.2.3.RELEASE)、Struts2 (2.3.14.3) 和 JPA (2.0)。我们有一个包含各种实体和公共类的项目(我们称之为项目 A)。我们使用项目 A 生成一个 .jar 文件,以便其他项目可以使用这些类。在项目 A 中,我们使用了 Spring 原型:@Component。@Service 或 @Repository 用于我们希望 Spring 注入的那些类。每当我们尝试从 jar 中注入 bean 时,都会收到类似于以下内容的错误:
没有为依赖找到类型为 [com.ceiwc.bc.commonsql.service.CommonSQLService] 的合格 bean:预计至少有 1 个 bean 有资格作为此依赖的自动装配候选者。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
无法自动装配字段:私有 com.ceiwc.bc.commonsql.service.CommonSQLService com.ceiwc.ma.mvc.action.LoginAction.commonSQLService;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type of [com.ceiwc.bc.commonsql.service.CommonSQLService] found for dependency: 预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
创建名为“com.ceiwc.ma.mvc.action.LoginAction”的 bean 时出错:自动装配依赖项的注入失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有 com.ceiwc.bc.commonsql.service.CommonSQLService com.ceiwc.ma.mvc.action.LoginAction.commonSQLService;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type of [com.ceiwc.bc.commonsql.service.CommonSQLService] found for dependency: 预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
无法实例化操作,com.ceiwc.ma.mvc.action.LoginAction,为命名空间“/Login”中的“doLogin”定义创建名为“com.ceiwc.ma.mvc.action.LoginAction”的 bean 时出错:注入 autowired依赖失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有 com.ceiwc.bc.commonsql.service.CommonSQLService com.ceiwc.ma.mvc.action.LoginAction.commonSQLService;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type of [com.ceiwc.bc.commonsql.service.CommonSQLService] found for dependency: 预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
我们正在为 Spring 使用 Java 配置。这是我们的配置类:
package com.zzz.bc.config;
@Configuration
@ImportResource({"/WEB-INF/spring/spring-config.xml"})
@ComponentScan(basePackages = "com.zzz")
public class ApplicationConfig {
@Bean
public JavaMailSender mailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(${app.mail.host});
return mailSender;
}
}
package com.zzz.bc.config;
@Configuration
@EnableTransactionManagement
@Import(ApplicationConfig.class)
@PropertySource({"classpath:db.properties"})
public class DataConfig {
@Autowired
Environment environment;
@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactoryBean().getObject());
return transactionManager;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
String[] packagesToScan =
{environment.getProperty("db.packagesToScan1"), environment.getProperty("db.packagesToScan2")};
Map<String, Object> jpaProperties = new HashMap<String, Object>();
jpaProperties.put("eclipselink.weaving", "false");
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource());
entityManagerFactoryBean.setPackagesToScan(packagesToScan);
entityManagerFactoryBean.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
entityManagerFactoryBean.setJpaVendorAdapter(vendorAdapter());
entityManagerFactoryBean.setJpaPropertyMap(jpaProperties);
return entityManagerFactoryBean;
}
@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 PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
}
我们将@ComponentScan 设置为检查com.zzz 包中的bean。在项目 B 中,我们有一个 Struts2 Action,我们想从项目 A 中注入一个 bean。下面是项目 B 中的类的片段:
@SuppressWarnings("rawtypes")
@Service("CommonSQLService")
public class CommonSQLServiceImpl implements CommonSQLService {
@Autowired
CommonSQLDao cdao;
@Override
public Policy getPolicyById(String policyNo) {
return cdao.getPolicyById(policyNo);
}
@Override
public Policy getPolicy(Policy parmRec) {
return cdao.getPolicy(parmRec);
}
@Override
public ArrayList<Policy> getPolicies(String policyNo) {
return cdao.getPolicies(policyNo);
}
@Override
public List<Policy> getPolicies(Policy parmRec){
return cdao.getPolicies(parmRec);
}
}
这是我们的操作(删除了 getter/setter):
package com.zzz.ma.mvc.action;
@SuppressWarnings("serial")
@Component
@Scope("prototype")
@Namespace("/Login")
public class LoginAction extends ActionParent {
@Autowired
private IUserService userService; //Contained in project B
@Autowired
private CommonSQLService commonSQLService; //What's autowired from project A
private User user;
private String updateFlag;
private Integer parentMenuId;
private IwifWebNavmenuItem record;
public static final String LOGIN_STR = "login";
@Action(value = "doLogin", results = { @Result(name = "success", location = "/pages/login.jsp") })
public String login() {
updateFlag = "Y";
return SUCCESS;
}
@Action(value = "validate", results = {
@Result(name = "success", location = "/BrowseOptions/showOptions", type = "redirect"),
@Result(name = "login", location = "/pages/login.jsp") })
public String validateUser() {
if (updateFlag == null) {
session.remove(LOGIN_STR);
return LOGIN;
}
if (userService.validateUser(user.getUsername(), user.getPassword()) == null) {
addActionError(getText("invalid.user"));
return LOGIN;
}
session.put(LOGIN_STR, user.getUsername());
return SUCCESS;
}
@Action(value = "logout", results = { @Result(name = "success", location = "/pages/login.jsp") })
public String logout() {
updateFlag = StringUtils.EMPTY;
session.invalidate();
return SUCCESS;
}
}
每当我们尝试执行此自动装配时,都会收到类似于上面列出的错误。我们缺少什么?不能自动装配带有 Spring Stereotypes 的 jar 文件中包含的类吗?
提前感谢您的帮助!