Resource是j2ee的内容,如果要使用注解@Resource,必须引入jar文件common-annotations.jar
@Resource注解在set方法上,默认是通过ByType形式进行注入
class代码如下:
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文件如下:
12 3
@Resource注解在set方法上,也可以通过ByName指定bean
class代码如下:
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 }
12
@Component是Spring本身包含的功能,无需引入其它额外的jar文件
使用注解@Component之前,xml的头必须加上如下信息:1 2
@Component是标记是class类上面,class代码如下:
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 ArrayListgetBooks() {36 // TODO Auto-generated method stub37 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,如:
12
可以通过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 ListgetBookList() { 20 return null;21 }22 }
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 ArrayListgetBooks() {29 return null;30 }31 32 public void add(Book book) {33 System.out.println(bookDAO.getClass().toString());34 bookDAO.saveBook(book); 35 } 36 }