这是我的情景。我的项目使用Spring(3.2.3.RELEASE),Struts2(2.3.14.3)和JPA(2.0)。我们有一个项目(我们称之为项目A),它包含各种实体和公共类。我们使用项目A生成.jar文件,以便其他项目可以使用这些类。在项目A中,我们使用了Spring构造型:@Component。我们希望Spring注入的那些类的@Service或@Repository。每当我们尝试从jar中注入bean时,都会出现类似于:
的错误
没有类型的限定bean 找到[com.ceiwc.bc.commonsql.service.CommonSQLService] 依赖:预计至少有1个bean有资格成为autowire 这种依赖的候选人。依赖注释: {@ org.springframework.beans.factory.annotation.Autowired(所需=真)}
无法自动装配字段:private com.ceiwc.bc.commonsql.service.CommonSQLService com.ceiwc.ma.mvc.action.LoginAction.commonSQLService;嵌套异常 是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有 合格的bean类型 找到[com.ceiwc.bc.commonsql.service.CommonSQLService] 依赖:预计至少有1个bean有资格成为autowire 这种依赖的候选人。依赖注释: {@ org.springframework.beans.factory.annotation.Autowired(所需=真)}
创建名为'com.ceiwc.ma.mvc.action.LoginAction'的bean时出错:注入自动装配 依赖失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:不能 autowire字段:私有 com.ceiwc.bc.commonsql.service.CommonSQLService com.ceiwc.ma.mvc.action.LoginAction.commonSQLService;嵌套异常 是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有 合格的bean类型 找到[com.ceiwc.bc.commonsql.service.CommonSQLService] 依赖:预计至少有1个bean有资格成为autowire 这种依赖的候选人。依赖注释: {@ org.springframework.beans.factory.annotation.Autowired(所需=真)}
- 醇>
无法实例化动作,com.ceiwc.ma.mvc.action.LoginAction,为名称空间'/ Login'Error中的'doLogin'定义创建bean name'com.ceiwc.ma.mvc.action.LoginAction':注入自动装配 依赖失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:不能 autowire字段:私有 com.ceiwc.bc.commonsql.service.CommonSQLService com.ceiwc.ma.mvc.action.LoginAction.commonSQLService;嵌套异常 是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有 合格的bean类型 找到[com.ceiwc.bc.commonsql.service.CommonSQLService] 依赖:预计至少有1个bean有资格成为autowire 这种依赖的候选人。依赖注释: {@ org.springframework.beans.factory.annotation.Autowired(所需=真)}
我们正在使用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 / setters):
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文件中包含的类不能自动装配吗?
先谢谢你的帮助!
答案 0 :(得分:5)
将@Service("CommonSQLService")
更改为@Service("commonSQLService")
,或者如果您未在其他任何位置实施@Service
界面,则可以使用CommonSQLService
。