i wanted to try the <context:component-scan base-package />
feature of spring 3.0.5.
i have this entry in applicationContext :
<context:component-scan base-package="com.project.personal.admin.model"/>
<context:annotation-config />
i have a manager class which knows how to create any POJO and 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);
}
//....
}
a Pojo开发者_如何学运维 like :
@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
}
my test class is like so
@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());
}
}
i'm having this error :
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)}
most of the time i create the entry in the applicationcontext for each class, and it's been working, this time along i wanted to try the package scanning. Is that something that i'm not doing well? i've tried the Autowired and later the Resource. So i'm out of ideas
thanks for reading this.
Make sure your Manager
is in the right package (or that your base-package
is set properly) (I wouldn't mention this, but it seems suspicious to have the manager in the model package)
精彩评论