gpt4 book ai didi

java - Spring 无法 Autowiring 字段,没有类型匹配的 bean

转载 作者:行者123 更新时间:2023-12-04 06:03:38 25 4
gpt4 key购买 nike

我尝试使用 Spring 3.1.0.RELEASE + Hibernate 来实现 RESTful web 服务遵循这个人的项目 https://github.com/eugenp/REST

当我运行我的项目时包含这样的错误。

SEVERE: Context initialization failed
*.BeanCreationException: Error creating bean with name 'userServiceController': Injection of autowired dependencies failed;
*.BeanCreationException: Error creating bean with name 'defaultUserService': Injection of autowired dependencies failed;
*.BeanCreationException: Could not autowire field: private com.cloudlb.dao.UserDAO com.cloudlb.service.DefaultUserService.userDao;
*.NoSuchBeanDefinitionException: No matching bean of type [com.cloudlb.dao.UserDAO]

这是我的配置:(persistence.properties)

应用配置:
@Configuration
@ComponentScan(basePackages = "com.cloudlb", excludeFilters = { @ComponentScan.Filter(Configuration.class)})
public class ApplicationConfig {

@Bean
public static PropertyPlaceholderConfigurer properties() {
final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
final Resource[] resources = new ClassPathResource[]{new ClassPathResource("persistence.properties"), new ClassPathResource("restful.properties")};
ppc.setLocations(resources);
ppc.setIgnoreUnresolvablePlaceholders(true);
return ppc;
}

在 PersistenceHibernateConfig 中包含如下:
@Profile("hibernate")
@EnableTransactionManagement
public class PersistenceHibernateConfig { ...

@Bean
public LocalSessionFactoryBean alertsSessionFactoryBean() { ...

@Bean
public DataSource restDataSource() { ...

@Bean
public HibernateTransactionManager transactionManager() { ...

网络配置:
@Configuration
@EnableWebMvc
public class WebConfig { ... }

因此,它从这里开始在 Autowiring 中出错:

用户服务 Controller :
@Controller
public class UserServiceController {

@Autowired
private UserService userService;

由 DefaultUserService 实现的 UserService:
@Service
@Transactional(propagation = Propagation.REQUIRED)
public class DefaultUserService implements UserService {

@Autowired
private UserDAO userDao;

用户DAO:
public interface UserDAO extends GenericDAO<User> {  ... }

用户HibernateDAO:
@Profile("hibernate")
public class UserHibernateDAO extends GenericHibernateDAO<User> implements UserDAO{ ... }

通用HibernateDAO:
@Profile("hibernate")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class GenericHibernateDAO<T extends Serializable> extends AbstractHibernateDAO<T> implements GenericDAO<T> { ... }

最后一个 AbstractHibernateDAO:
@Transactional( propagation = Propagation.SUPPORTS )
public abstract class AbstractHibernateDAO<T extends Serializable> implements DAO<T> {

private Class<T> clazz;

@Autowired
private SessionFactory sessionFactory;

public AbstractHibernateDAO() {
super();
}

public final void setClazz(final Class<T> clazz) {
this.clazz = clazz;
}

@Override
@Transactional( readOnly = true )
public T findById(String id) {
return (T) this.getCurrentSession().get(this.clazz, id);
}

protected Session getCurrentSession() {
return this.sessionFactory.getCurrentSession();
}
}

我以为它可以找到 UserHibernateDAO。可能是我在这里遗漏了什么。所以,希望有人知道如何解决它。我在这个问题上遇到了 2 天。

我想 SessionFactory 也可能有问题。因为我开始修复那个东西,这出现了。

先感谢您。

最佳答案

除了@Profile("hibernate") UserHibernateDAO需要一个注释声明它是一个 Spring bean,如 @Named@Component .

关于java - Spring 无法 Autowiring 字段,没有类型匹配的 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8630544/

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