liuzhijun
liuzhijun

Reputation: 4469

Spring Injection of resource fields failed

I have a BaseDAO and some subClass eg:UserDaoImpl,OrderDaoImpl,now in a BaseService

public abstract class Baservice{

    @Resource
    private BaseDao basedao;
    //something crud operate.....
}

@Service
public UserService extends BaseService{
   //....
}

how to configure Spring that I can pass a real subclass to basedao

I encount a exception

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of resource fields failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.cloudking.trafficmonitor.BaseDAO] is defined: expected single matching bean but found 3: [roleDAO, userDAO

Upvotes: 4

Views: 9201

Answers (3)

Jigar Joshi
Jigar Joshi

Reputation: 240898

It seems you have 3 classes which extends BaseDAO so all of them are BaseDao (IS A)

You need to use name attribute of @Resource

For example:

@Component
public RoleDao extends BaseDao{/* some code*/}

and

@Component
public UserDao extends BaseDao{/* some code*/}

now if you want to inject UserDao then

@Resource(name="userDao")
BaseDao baseDao

Upvotes: 1

sufyan.shoaib
sufyan.shoaib

Reputation: 1147

Your application is unable to find the bean BaseDAO. You need to annotate BaseDAO with @Component (or @ Service, not sure about this one)

Upvotes: 0

mprabhat
mprabhat

Reputation: 20323

Use @Qualifier("UserDaoImpl")

or use @Resource(name="UserDaoImpl")

Upvotes: 0

Related Questions