spring容器對於Bean的建立和對象屬性的依賴注入提供了註解的支持,讓咱們在開發中可以更加便捷的實現對象的建立和對象屬性的依賴注入。
一,對於Bean的建立spring容器提供瞭如下四個註解的支持:
一、@Component
二、@Repository dao層實現類的註解
三、@Service service層實現類的註解
四、@Controller controller層實現類的註解
以上四個註解在普通使用中是等效的,但在web項目中爲了區分三層架構中不一樣層之間Bean的建立,爲了不註解使用的混亂,使用後三個註解進行區分。
二,對於Bean屬性的依賴注入分爲兩類,一類是對於屬性是String類型或者基本數據類型Spring容器提供了@Value這個註解,另外一類是對於屬性是對象的提供了@Autowired和@Resource這兩個註解。
其中,@Autowired這個註解是spring框架自帶的註解,而@Resource(javax.annotation.Resource)這個註解是javax擴展包中註解規範的一種,而spring對這一註解提供了支持。
下面咱們經過實驗來講明註解對於bean建立和bean屬性依賴注入的實現。
首先要在配置文件中配置註解掃描的驅動。
java
<context:annotation-config/>
<context:component-scan base-package="com.opensource"/>
這裏提一句,若是配置了註解掃描包的範圍,也就是第二行,那麼<context:annotation-config/>能夠不用配置,由於配置掃描包的範圍後,註解的驅動也就有了。
實驗一,bean的建立,由於spring容器對於bean建立的四個註解是等效,這裏咱們使用@Component這個註解程序員
Student類:
@Component public class Student { public Student(){ System.out.println("spring容器調用Student類的無參構造器"); }
測試類:
public class MyTest { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("spring-bean.xml"); } }
實驗結果:
![](http://static.javashuo.com/static/loading.gif)
實驗二:bean屬性爲String類型及基本數據類型的的依賴注入web
student類:spring
@Component(value = "student") public class Student { @Value("張三") private String name; @Value("23") private int age; public String getName() { return name; } public int getAge() { return age; } }
在這裏 @Component(value = "student") value指定的是bean的id,另外對於註解方式實現的依賴注入,bean的屬性無需再提供setter方法。架構
測試類:框架
public class MyTest { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("spring-bean.xml"); Student student = (Student)ac.getBean("student"); System.out.println(student.getName()); System.out.println(student.getAge()); } }
實驗結果:測試
實驗三:bean屬性爲java對象的依賴注入。spa
對於bean屬性爲java對象的依賴注入,分爲兩種一種是byName類型的,一種是byType類型的。而使用的註解分別是@Autowired和@Resource,對於byType類型的二者的使用沒有區別,可是對於byName類型的@Autowired要使用聯合註解@Qualifier,因此說使用@Resource較爲簡單。這裏咱們在研究對於bean屬性爲java對象的依賴注入時就使用@Resource這個註解了。code
實驗3.1:按照byName類型對bean屬性爲java對象的依賴注入。component
上個實驗中的Student類保持不變,再提供一個Teacher類。
@Component("teacher") public class Teacher { @Resource(name = "student") private Student student; public Student getStudent() { return student; } }
測試類:
public class MyTest { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("spring-bean.xml"); Teacher teacher = (Teacher)ac.getBean("teacher"); System.out.println(teacher.getStudent().getName()); System.out.println(teacher.getStudent().getAge()); } }
實驗結果:
實驗3.2:按照byType類型對bean屬性爲java對象的依賴注入。
對於這種方式的依賴注入,只需分別修改Student類和Teacher類。
@Component public class Student { @Value("張三") private String name; @Value("23") @Qualifier private int age; public String getName() { return name; } public int getAge() { return age; } }
@Component("teacher") public class Teacher { @Resource private Student student; public Student getStudent() { return student; } }
固然在這裏Student類的id也是能夠保留的,這裏這麼作是爲了說明問題。實驗結果同上。
最後說一點,咱們做爲程序員,研究問題仍是要仔細深刻一點的。當你對原理了解的有夠透徹,開發起來也就駕輕就熟了,不少開發中的問題和疑惑也就迎刃而解了,並且在面對其餘問題的時候也可作到舉一反三。固然在開發中沒有太多的時間讓你去研究原理,開發中要以實現功能爲前提,可等項目上線的後,你有大把的時間或者空餘的時間,你大可去刨根問底,深刻的去研究一項技術,爲以爲這對一名程序員的成長是很重要的事情。