Spring依賴注入之Resource和Component

Resource是j2ee的內容,若是要使用註解@Resource,必須引入jar文件common-annotations.jarjava

@Resource註解在set方法上,默認是經過ByType形式進行注入spring

class代碼以下:dom

1 @Resource2     public void setBookDAO(BookDAO bookDAO) {
3         System.out.println("******************setBookDAO");
4         this.bookDAO = bookDAO;
5         System.out.println(this.bookDAO.getClass().toString());        
6     }

xml文件以下:this

1 <bean id="BookDAOImpl" class="com.test.dao.impl.BookDAOImpl">
2     
3   </bean>

@Resource註解在set方法上,也能夠經過ByName指定beanspa

class代碼以下:code

1 @Resource(name="u")
2     public void setBookDAO(BookDAO bookDAO) {
3         System.out.println("******************setBookDAO");
4         this.bookDAO = bookDAO;
5         System.out.println(this.bookDAO.getClass().toString());        
6     }


xml文件以下:component

1 <bean id="u" class="com.test.dao.impl.BookDAOImpl">   
2   </bean>

@Component是Spring自己包含的功能,無需引入其它額外的jar文件
使用註解@Component以前,xml的頭必須加上以下信息:xml

1 <!-- 掃描「com.test」包下面的全部class文件,若是含有註解@Componeent的class類建立實例 -->
2        <context:component-scan base-package="com.test" />

@Component是標記是class類上面,class代碼以下:blog

 

 1 package com.test.service.impl;
 2 
 3 import java.util.ArrayList;
 4 
 5 import javax.annotation.Resource;
 6 
 7 import org.springframework.beans.factory.annotation.Autowired;
 8 import org.springframework.beans.factory.annotation.Qualifier;
 9 import org.springframework.stereotype.Component;
10 
11 
12 import com.test.dao.BookDAO;
13 import com.test.domain.Book;
14 import com.test.service.BookService;
15 
16 @Component("bookService")
17 public class BookServiceImpl implements BookService{
18     
19     private BookDAO bookDAO;    
20     public BookServiceImpl(){
21         System.out.println("BookServiceImpl init!");
22     }
23 
24     public BookDAO getBookDAO() {
25         return bookDAO;
26     }
27     
28     @Resource(name="BookDAOImpl")
29     public void setBookDAO(BookDAO bookDAO) {
30         System.out.println("******************setBookDAO");
31         this.bookDAO = bookDAO;
32         System.out.println(this.bookDAO.getClass().toString());        
33     }
34 
35     public ArrayList<Book> getBooks() {
36         // TODO Auto-generated method stub
37         return null;
38     }
39 
40     public void add(Book book) {
41         System.out.println(bookDAO.getClass().toString());
42         bookDAO.saveBook(book);
43 
44         
45     }            
46 
47 }

@Component("bookService")是指定名字"bookService",也就等同於在xml配置一個bean,如:ci

1 <bean id="bookService" class="com.test.service.impl.BookServiceImpl">   
2 </bean>

能夠經過context.getBean("bookService")獲取到該實例
也能夠經過@Resource(name="bookSercie")註解在set方法上,注入到變量中

下面代碼是將BookDAOImpl類實例化,key變量爲bookDAO

 1 package com.test.dao.impl;
 2 
 3 import java.util.List;
 4 import org.springframework.stereotype.Component;
 5 import com.test.dao.BookDAO;
 6 import com.test.domain.Book;
 7 
 8 @Component("bookDAO")
 9 public class BookDAOImpl implements BookDAO {
10     
11     public void saveBook(Book book) {        
12         System.out.println("save book success!");        
13     }
14     
15     public BookDAOImpl(){
16         System.out.println("BookDAOImpl inti!");
17     }
18     
19     public List<Book> getBookList() {        
20         return null;
21     }
22 }


下面的代碼是通@Resource,指定name爲bookDAO的實例類經過set方法注入到bookDAO變量中  

 1 package com.test.service.impl;
 2 
 3 import java.util.ArrayList;
 4 import javax.annotation.Resource;
 5 import org.springframework.stereotype.Component;
 6 import com.test.dao.BookDAO;
 7 import com.test.domain.Book;
 8 import com.test.service.BookService;
 9 
10 @Component("bookService")
11 public class BookServiceImpl implements BookService{    
12     private BookDAO bookDAO;    
13     public BookServiceImpl(){
14         System.out.println("BookServiceImpl init!");
15     }
16 
17     public BookDAO getBookDAO() {
18         return bookDAO;
19     }
20     
21     @Resource(name="bookDAO")
22     public void setBookDAO(BookDAO bookDAO) {
23         System.out.println("******************setBookDAO");
24         this.bookDAO = bookDAO;
25         System.out.println(this.bookDAO.getClass().toString());        
26     }
27 
28     public ArrayList<Book> getBooks() {
29         return null;
30     }
31 
32     public void add(Book book) {
33         System.out.println(bookDAO.getClass().toString());
34         bookDAO.saveBook(book);        
35     }            
36 }
相關文章
相關標籤/搜索