我想尝试一下spring 3.0.5的<context:component-scan base-package />
功能。
我在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);
}
//....
}
像Pjo一样的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:找不到类型为[com.project.personal.admin.manager.Manager]的匹配bean依赖:期望至少有一个bean符合此依赖关系的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
大部分时间我在applicationcontext中为每个类创建条目,并且它一直在工作,这次我想尝试包扫描。 这是我做得不好的事吗?我已经尝试了Autowired以及后来的资源。所以我没有想法
感谢您阅读本文。
答案 0 :(得分:2)
确保您的Manager
位于正确的包中(或者您的base-package
设置正确)(我不会提到这一点,但在模型包中有经理似乎很可疑)