简体   繁体   中英

Little trouble with hibernate autowire in a spring project, bean not found error

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.

确保你的Manager在正确的包中(或你的base-package正确设置)(我不会提到这一点,但在模型包中有经理似乎很可疑)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM