Annotation-based container configuration
-
5.9.1. @Required
5.9.2. @Autowired
5.9.3. Fine-tuning annotation-based autowiring with qualifiers
5.9.4. Using generics as autowiring qualifiers
5.9.5. CustomAutowireConfigurer
5.9.6. @Resource
5.9.7. @PostConstruct and @PreDestroy
利用spring一步一步搭建起一个例子(本人采用eclipse)
/**
* Dao主要做数据库的交互工作
* Modle 是模型 存放你的实体类
*Service 做相应的业务逻辑处理
*Action是一个控制器
* @author syw
*
*/
modle:实体类
package com.model;
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Dao 数据库
package com.dao;
import com.model.User;
public interface UserDAO {
public void saveMySql(User user);
public void saveOracle(User user);
}
DaoImpl 实现(数据不一样,不一样的实现)
package com.dao.impl;
import com.dao.UserDAO;
import com.model.User;
public class UserDaoImpl implements UserDAO{
@Override
public void saveMySql(User user) {
System.out.println("Mysql: " + user.getUsername());
}
@Override
public void saveOracle(User user) {
System.out.println("Oracle: " + user.getPassword());
}
}
service 业务逻辑处理
package com.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import com.dao.UserDAO;
import com.model.User;
public class UserService {
private UserDAO userDAO;
public void add(User user) {
userDAO.saveMySql(user);
}
public UserDAO getUserDAO() {
return userDAO;
}
@Autowired
//@Qualifier("user2")
public void setUserDAO(@Qualifier("user1")UserDAO userDAO) {
this.userDAO = userDAO;
}
}
service 测试类
package com.service;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.model.User;
/**
* Dao主要做数据库的交互工作
* Modle 是模型 存放你的实体类
*Service 做相应的业务逻辑处理
*Action是一个控制器
* @author tao.zeng
*
*/
public class UserServiceTest {
@Test
public void testAdd() throws Exception {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
UserService service = (UserService)ctx.getBean("userService");
User u = new User();
u.setUsername("syw");
u.setPassword("syw");
service.add(u);
}
}
代码运行:Mysql: syw
参考:
Because autowiring by type may lead to multiple candidates,
it is often necessary to have more control over the selection process. One way to accomplish this is with Spring’s @Qualifier annotation. You can associate qualifier values with specific arguments, narrowing the set of type matches so that a specific bean is chosen for each argument. In the simplest case, this can be a plain descriptive value:
public class MovieRecommender {
@Autowired
@Qualifier("main")
private MovieCatalog movieCatalog;
// ...
}
The @Qualifier annotation can also be specified on individual constructor arguments or method parameters:
public class MovieRecommender {
private MovieCatalog movieCatalog;
private CustomerPreferenceDao customerPreferenceDao;
@Autowired
public void prepare(@Qualifier("main")MovieCatalog movieCatalog,
CustomerPreferenceDao customerPreferenceDao) {
this.movieCatalog = movieCatalog;
this.customerPreferenceDao = customerPreferenceDao;
}
// ...
}