gpt4 book ai didi

Spring Autowire 失败,找不到类型的合格 bean 依赖错误

转载 作者:行者123 更新时间:2023-12-05 01:07:32 26 4
gpt4 key购买 nike

这是我的场景。我的项目正在使用 Spring (3.2.3.RELEASE)、Struts2 (2.3.14.3) 和 JPA (2.0)。我们有一个包含各种实体和公共(public)类的项目(我们称之为项目 A)。我们使用项目 A 生成一个 .jar 文件,以便其他项目可以使用这些类。在项目 A 中,我们使用了 Spring 原型(prototype):@Component。 @Service 或 @Repository 用于我们希望 Spring 注入(inject)的那些类。每当我们尝试从 jar 中注入(inject) bean 时,都会收到类似于以下内容的错误:

  1. No qualifying bean of type [com.ceiwc.bc.commonsql.service.CommonSQLService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

  2. Could not autowire field: private com.ceiwc.bc.commonsql.service.CommonSQLService com.ceiwc.ma.mvc.action.LoginAction.commonSQLService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.ceiwc.bc.commonsql.service.CommonSQLService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

  3. Error creating bean with name 'com.ceiwc.ma.mvc.action.LoginAction': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.ceiwc.bc.commonsql.service.CommonSQLService com.ceiwc.ma.mvc.action.LoginAction.commonSQLService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.ceiwc.bc.commonsql.service.CommonSQLService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

  4. Unable to instantiate Action, com.ceiwc.ma.mvc.action.LoginAction, defined for 'doLogin' in namespace '/Login'Error creating bean with name 'com.ceiwc.ma.mvc.action.LoginAction': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.ceiwc.bc.commonsql.service.CommonSQLService com.ceiwc.ma.mvc.action.LoginAction.commonSQLService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.ceiwc.bc.commonsql.service.CommonSQLService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@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 中注入(inject)一个 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;
}
}

每当我们尝试执行此 Autowiring 时,都会收到类似于上面列出的错误。我们缺少什么?不能 Autowiring 带有 Spring Stereotypes 的 jar 文件中包含的类吗?

提前感谢您的帮助!

最佳答案

更改@Service("CommonSQLService")@Service("commonSQLService")或者您可以使用 @Service如果您没有实现 CommonSQLService其他任何地方的接口(interface)。

关于Spring Autowire 失败,找不到类型的合格 bean 依赖错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18469641/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com