# @Qualifier
| @SuppressWarnings("all") |
| @Repository("bookDao1") |
| public class BookDaoImpl implements BookDao { |
| public void save(){ |
| System.out.println("BookDaoImpl save rning..."); |
| } |
| } |
| @SuppressWarnings("all") |
| @Repository("bookDao") |
| public class BookDaoImpl1 implements BookDao { |
| public void save(){ |
| System.out.println("BookDaoImpl save rning... 1"); |
| } |
| } |
| @SuppressWarnings("all") |
| @Service("bookService") |
| public class BookServiceImpl implements BookService { |
| @Autowired |
| @Qualifier("bookDao") |
| private BookDao bookDao ; |
| |
| public void save(){ |
| System.out.println("BookServiceImpl save rning..."); |
| bookDao.save(); |
| } |
| } |
<font style="color:red"> 注意:</font> 需要使用 @Autowired 不然就会报错
| @SuppressWarnings("all") |
| @Service("bookService") |
| public class BookServiceImpl implements BookService { |
| @Qualifier("bookDao") |
| private BookDao bookDao; |
| |
| public void save(){ |
| System.out.println("BookServiceImpl save rning..."); |
| bookDao.save(); |
| } |
| } |
Run Result
在 @Qualifier 参数列表中指定两个 Dao 其中一个名称则 Run 结果为其该 bean 的 save 方法内容
| @SuppressWarnings("all") |
| @Service("bookService") |
| public class BookServiceImpl implements BookService { |
| @Autowired |
| @Qualifier("bookDao") |
| private BookDao bookDao; |
| public void save(){ |
| System.out.println("BookServiceImpl save rning..."); |
| bookDao.save(); |
| } |
| } |
Run Result
| @SuppressWarnings("all") |
| @Service("bookService") |
| public class BookServiceImpl implements BookService { |
| @Autowired |
| @Qualifier("bookDao1") |
| private BookDao bookDao; |
| public void save(){ |
| System.out.println("BookServiceImpl save rning..."); |
| bookDao.save(); |
| } |
| } |
Run Result