我想试试<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:没有为依赖项找到类型为 [com.project.personal.admin.manager.Manager] 的匹配 bean:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
大多数时候,我在每个类的应用程序上下文中创建条目,并且它一直在工作,这一次我想尝试包扫描。那是我做得不好的事情吗?我试过 Autowired 和后来的资源。所以我没有想法
感谢您阅读本文。