gpt4 book ai didi

java - Spring项目中hibernate Autowiring 的小麻烦,未找到bean错误

转载 作者:行者123 更新时间:2023-12-02 08:20:13 25 4
gpt4 key购买 nike

我想尝试 <context:component-scan base-package /> spring 3.0.5 的功能。

我在 applicationContext 中有此条目:

<context:component-scan base-package="com.project.personal.admin.model"/>
<context:annotation-config />

我有一个管理器类,它知道如何创建任何 POJO 和 DAO。

@Component("manager")
public class ManagerImpl implements ApplicationContextAware, Manager {

ApplicationContext applicationContext;

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}

public User CreateUser(){
return (User) getInstance("user", User.class);
}


public UserDAO createUserDAO(){
return (UserDAO) getInstance("userDAO", UserDAO.class);
}
//....
}

像 Pojo :

@Entity
@Table(name = "user", uniqueConstraints = {
@UniqueConstraint(columnNames = {"email"})})
@Component("user")
public class User {

public User() {
this.dateCreated = new Date();
}


@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid.hex")
@Column(name = "id", length = 32)
private String id;
@Column(name = "email", length = 150)
private String email;

//setters and getters
}

我的测试课是这样的

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:META-INF/test-project-admin-config.xml"})
@TransactionConfiguration(defaultRollback=true)
@Transactional
public class UserDAOImplTest {
//@Autowired
@Resource(name="manager")
Manager manager;

@Autowired
UserDAO userDAO;

public UserDAOImplTest() {
}

@Test
public void testSave() {
User u1 = manager.CreateUser();
u1.setEmail("misterjojtoo@gmail.com");
u1.setFullname("joseph djomeda");
u1.setPassword("psaumedetdavid");
userDAO.save(u1);
User expResult = u1;

User result = (User)userDAO.getById(u1.getId());

Assert.assertEquals(expResult, result);
Assert.assertEquals(expResult.getId(), result.getId());
}

}

我遇到了这个错误:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.project.personal.admin.manager.Manager] 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)}

大多数时候,我在每个类的应用程序上下文中创建条目,并且它一直在工作,这一次我想尝试包扫描。难道是我哪里做得不好?我尝试过 Autowired,后来又尝试了 Resource。所以我没主意了

感谢您阅读本文。

最佳答案

确保您的Manager位于正确的包中(或者您的base-package设置正确)(我不会提及这一点,但似乎很可疑模型包中有管理器)

关于java - Spring项目中hibernate Autowiring 的小麻烦,未找到bean错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5623864/

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