上一章咱們學習了Spring的IOC特性以及IOC的實現原理:註解和反射,本章咱們將學習如何在Spring中使用IOC。spring
Spring最重要的特性是IOC控制反轉,利於IOC咱們能下降對象之間的耦合性。app
IOC須要經過必定的配置實現,配置方法分爲:框架
1)使用xml文件配置maven
2)使用註解配置學習
使用Spring的基本功能,必須先導入Spring的依賴:this
1. <dependency>xml
2. <groupId>org.springframework</groupId>對象
3. <artifactId>spring-context</artifactId>接口
4. <version>5.1.5.RELEASE</version>內存
5. </dependency>
Spring Context:向 Spring框架提供上下文信息。Spring 上下文包括企業服務,例如JNDI、EJB、電子郵件、國際化、校驗和調度功能。它包含Spring Core組件,能實現IOC的核心功能。
6. /**
7. * CPU接口
8. */
9. public interface Cpu {
10. void run();
11. }
12. /**
13. * AMD的CPU
14. */
15. public class AMDCpu implements Cpu {
16. public void run() {
17. System.out.println("AMD的CPU正在運行....");
18. }
19. }
20. /**
21. * 內存接口
22. */
23. public interface Memory {
24. void read();
25. void write();
26. }
27. /**
28. * DDR8G的內存
29. */
30. public class DDR8GMemory implements Memory {
31. public void read() {
32. System.out.println("使用DDR8G的內存讀取數據....");
33. }
34. public void write() {
35. System.out.println("使用DDR8G的內存寫入數據....");
36. }
37. }
38. 相似的IntelCpu和DDR16Memory類省略了代碼
39. /**
40. * 電腦類
41. */
42. public class Computer {
43.
44. private Cpu cpu;
45. private Memory memory;
46. private String brand;
47. ...省略get\set
48. public Computer() {
}
public Computer(String brand, Cpu cpu, Memory memory) {
this.brand = brand;
this.cpu = cpu;
this.memory = memory;
}
public void start(){
System.out.println(brand+"電腦啓動了");
cpu.run();
memory.read();
memory.write();
}
49. }
在maven項目的resources目錄下,添加配置文件:
applicationContext.xml
50. <?xml version="1.0" encoding="UTF-8"?>
51. <beans xmlns="http://www.springframework.org/schema/beans"
52. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
53. xmlns:context="http://www.springframework.org/schema/context"
54. xsi:schemaLocation="http://www.springframework.org/schema/beans
55. http://www.springframework.org/schema/beans/spring-beans.xsd
56. http://www.springframework.org/schema/context
57. http://www.springframework.org/schema/context/spring-context.xsd">
58. <!-- CPU對象-->
59. <bean id="cpu" class="com.qianfeng.springioc.demo3.IntelCpu"/>
60. <!--Memory對象-->
61. <bean id="memory" class="com.qianfeng.springioc.demo3.DDR16GMemory"/>
62. <!--電腦對象-->
63. <bean id="computer" class="com.qianfeng.springioc.demo3.Computer">
64. <!--屬性的注入-->
65. <property name="cpu" ref="cpu"></property>
66. <property name="memory" ref="memory"></property>
67. <property name="brand" value="小米電腦"></property>
68. </bean>
69. </beans>
配置說明:
<beans>是根標籤,表明Spring的Java對象容器
<bean>標籤表明在容器中建立一個Java對象,屬性id表明對象名,class是對象的類型。
在配置文件中首先建立了一個cpu對象和一個memory對象,而後建立了一個computer對象,computer中有Cpu類型的cpu屬性和Memory類型memory屬性以及String類型的brand屬性,這裏使用依賴注入的方式給屬性賦值。
<property name="cpu" ref="cpu"></property>
property 指的是對象的屬性,name是屬性名,ref是對象引用,這裏引用了前面的cpu對象。
<property name="brand" value="華碩電腦"></property>
brand屬性注入的是數值而不是對象引用,這裏使用value注入值。
Spring容器能夠看作是一個JavaBean的工廠BeanFactory,BeanFactory負責建立並保存各個JavaBean,BeanFactory的子類有:
1)ClassPathXMLApplicationContext
基於XML配置文件上下文
2)AnnotationConfigApplicationContext
基於註解配置的上下文
3)FileSystemApplicationContext
基於文件系統的上下文
使用ClassPathXMLApplicationContext的方法:
70. public class TestComputer {
71.
72. @Test
73. public void testComputer(){
74. //建立XML文件的應用程序上下文對象
75. ClassPathXmlApplicationContext cxt =
76. new ClassPathXmlApplicationContext("applicationContext.xml");
77. //經過類型從容器得到Java對象
78. Computer computer = cxt.getBean(Computer.class);
79. //還能夠經過對象名得到對象
80. // Computer computer = (Computer) cxt.getBean("computer");
81. computer.start();
82. }
83. }
Spring的IOC也能夠不使用配置文件,徹底經過Java代碼和註解實現配置,這種配置方法代碼更加簡潔。
經常使用註解:
@Component
配置到類上面,Spring容器會自動掃描並添加有該註解類的對象
@Autowired
配置到屬性或set方法上,容器會將容器中同類型的對象自動注入到屬性中
@Qualifier
用於給不一樣的組件設置標識,用於區分多個相同類型的對象
@Value
注入通常類型的值,如:@Value(20) 、 @Value("張三")
@Configuration
加在配置類上,該類做爲Spring啓動的入口
@ComponentScan
和@Configuration配合使用,加在配置類上,用於掃描包中全部@Component註解的類
84. 在DDR8Memory類和IntelCpu類上添加@Component註解
85. 修改Computer類:
86. @Component
87. public class Computer {
88.
89. @Value("蘋果電腦")
90. private String brand;
91.
92. @Autowired
93. private Cpu cpu;
94.
95. @Autowired
96. private Memory memory;
97. ....
98. }
99.
100. @Configuration
101. @ComponentScan("com.qianfeng.springioc.demo4")
102. public class MyConfig {
103.
104. public static void main(String[] args) {
105. //建立基於註解的上下文對象
106. AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext(MyConfig.class);
107. //得到Computer對象
108. Computer computer = cxt.getBean(Computer.class);
109. computer.start();
110. }
111. }
本章咱們學習了兩種Spring的配置方法,XML配置的好處是:和代碼耦合性低,容易維護,註解配置的好處是:代碼簡潔。兩種配置方法的優點互補,在實際開發過程當中通常會使用XML和註解混合進行配置。