I have two classes CustomerDAOImpl and UserDAOImpl, both annotated with @Repository annotation. I have @Bean defined and autowired in each class.
@Repository
public class CustomerDAOImpl implements CustomerDAO {
private static final String CUSTOMER_LOCK_INSERT = "INSERT INTO CUSTOMER_LOCKS (customerid, userid, session) VALUES (?, ?, ?)";
@Bean
@Lazy(true)
public PreparedStatement customerLockAddStmt () {
return cassandraTemplate.getSession().prepare(CUSTOMER_LOCK_INSERT);
}
@Autowired
@Lazy
PreparedStatement customerLockAddStmt;
@Autowired
CassandraOperations cassandraTemplate;
public void create(CustomerLock lock) {
...
Statement statement = customerLockAddStmt.bind(lock.customerId,lock.userId, lock.sessionId);
cassandraTemplate.execute(statement);
}
}
Exactly the same way, I have defined, autowired and used following beans in UserDAOImpl class methods(just showing the bean definitions and autowiring code to keep it clean and short here):
@Bean
@Lazy(true)
public PreparedStatement userAddStmt () {
return cassandraTemplate.getSession().prepare(USER_INSERT);
}
@Bean
@Lazy(true)
public PreparedStatement userUpdateStmt () {
return cassandraTemplate.getSession().prepare(USER_UPDATE);
}
@Autowired
@Lazy
PreparedStatement userAddStmt;
@Autowired
@Lazy
PreparedStatement userUpdateStmt;
@Autowired
CassandraOperations cassandraTemplate;
public void update(User user){
//Beans userAddStmt and userUpdateStmt defined and autowired in this class are being used here
....
}
Now both these DAO Beans are being autowired in my service class OrderServiceImpl (annotated with @Service); here is the snippet:
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
UserDAO userDAO;
@Autowired
CustomerDAO customerDAO;
public void createOrder(Order order) {
....
customerDAO.create(CustomerLock); // Getting the exception on this line
....
userDAO.update(user);
....
}
}
When OrderService code executes "customerDAO.create(CustomerLock);" , I'm getting this exception.
No qualifying bean of type [com.datastax.driver.core.PreparedStatement] is defined: expected single matching bean but found 2: userAddStmt,userUpdateStmt".
After getting this error, I added the attribute name="customerLockAddStmt" in "customerLockAddStmt" bean definition and used @Qualifier("customerLockAddStmt") while autowiring this bean, it worked but now it fails on following line due to the same error for the beans wired in userDAOImpl:
userDAO.update(user);
No qualifying bean of type [com.datastax.driver.core.PreparedStatement] is defined: expected single matching bean but found 1: customerLockAddStmt".
Could someone please help?