前言:上一章我們學(xué)習(xí)了Spring的IOC特性以及IOC的實(shí)現(xiàn)原理:注解和反射,本章我們將學(xué)習(xí)如何在Spring中使用IOC。 Spring的IOC配置 Spring最重要的特性是IOC控制反轉(zhuǎn),利于IOC我們能降低對(duì)象之間的耦合性。 IOC需要通過(guò)一定的配置實(shí)現(xiàn),配置方法分為: 1)使用xml文件配置 2)使用注解配置 使用Spring的基本功能,必須先導(dǎo)入Spring的依賴: 1. <dependency> 2. <groupId>org.springframework</groupId> 3. <artifactId>spring-context</artifactId> 4. <version>5.1.5.RELEASE</version> 5. </dependency> Spring Context:向 Spring框架提供上下文信息。Spring 上下文包括企業(yè)服務(wù),例如JNDI、EJB、電子郵件、國(guó)際化、校驗(yàn)和調(diào)度功能。它包含Spring Core組件,能實(shí)現(xiàn)IOC的核心功能。 使用xml文件配置 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正在運(yùn)行...."); 18. } 19. } 20. /** 21. * 內(nèi)存接口 22. */ 23. public interface Memory { 24. void read(); 25. void write(); 26. } 27. /** 28. * DDR8G的內(nèi)存 29. */ 30. public class DDR8GMemory implements Memory { 31. public void read() { 32. System.out.println("使用DDR8G的內(nèi)存讀取數(shù)據(jù)...."); 33. } 34. public void write() { 35. System.out.println("使用DDR8G的內(nèi)存寫(xiě)入數(shù)據(jù)...."); 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+"電腦啟動(dòng)了"); cpu.run(); memory.read(); memory.write(); } 49. } 在maven項(xiàng)目的resources目錄下,添加配置文件: applicationContext.xml 50. <?xml version="1.0" encoding="UTF-8"?> 51. <beans xmlns="http://www./schema/beans" 52. xmlns:xsi="http://www./2001/XMLSchema-instance" 53. xmlns:context="http://www./schema/context" 54. xsi:schemaLocation="http://www./schema/beans 55. http://www./schema/beans/spring-beans.xsd 56. http://www./schema/context 57. http://www./schema/context/spring-context.xsd"> 58. <!-- CPU對(duì)象--> 59. <bean id="cpu" class="com.qianfeng.springioc.demo3.IntelCpu"/> 60. <!--Memory對(duì)象--> 61. <bean id="memory" class="com.qianfeng.springioc.demo3.DDR16GMemory"/> 62. <!--電腦對(duì)象--> 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> 配置說(shuō)明: <beans>是根標(biāo)簽,代表Spring的Java對(duì)象容器 <bean>標(biāo)簽代表在容器中創(chuàng)建一個(gè)Java對(duì)象,屬性id代表對(duì)象名,class是對(duì)象的類型。 在配置文件中首先創(chuàng)建了一個(gè)cpu對(duì)象和一個(gè)memory對(duì)象,然后創(chuàng)建了一個(gè)computer對(duì)象,computer中有Cpu類型的cpu屬性和Memory類型memory屬性以及String類型的brand屬性,這里使用依賴注入的方式給屬性賦值。 <property name="cpu" ref="cpu"></property> property 指的是對(duì)象的屬性,name是屬性名,ref是對(duì)象引用,這里引用了前面的cpu對(duì)象。 <property name="brand" value="華碩電腦"></property> brand屬性注入的是數(shù)值而不是對(duì)象引用,這里使用value注入值。 Spring上下文對(duì)象 Spring容器可以看做是一個(gè)JavaBean的工廠BeanFactory,BeanFactory負(fù)責(zé)創(chuàng)建并保存各個(gè)JavaBean,BeanFactory的子類有: 1)ClassPathXMLApplicationContext 基于XML配置文件上下文 2)AnnotationConfigApplicationContext 基于注解配置的上下文 3)FileSystemApplicationContext 基于文件系統(tǒng)的上下文 使用ClassPathXMLApplicationContext的方法: 70. public class TestComputer { 71. 72. @Test 73. public void testComputer(){ 74. //創(chuàng)建XML文件的應(yīng)用程序上下文對(duì)象 75. ClassPathXmlApplicationContext cxt = 76. new ClassPathXmlApplicationContext("applicationContext.xml"); 77. //通過(guò)類型從容器獲得Java對(duì)象 78. Computer computer = cxt.getBean(Computer.class); 79. //還可以通過(guò)對(duì)象名獲得對(duì)象 80. // Computer computer = (Computer) cxt.getBean("computer"); 81. computer.start(); 82. } 83. } 使用注解配置 Spring的IOC也可以不使用配置文件,完全通過(guò)Java代碼和注解實(shí)現(xiàn)配置,這種配置方法代碼更加簡(jiǎn)潔。 常用注解: @Component 配置到類上面,Spring容器會(huì)自動(dòng)掃描并添加有該注解類的對(duì)象 @Autowired 配置到屬性或set方法上,容器會(huì)將容器中同類型的對(duì)象自動(dòng)注入到屬性中 @Qualifier 用于給不同的組件設(shè)置標(biāo)識(shí),用于區(qū)分多個(gè)相同類型的對(duì)象 @Value 注入一般類型的值,如:@Value(20) 、 @Value("張三") @Configuration 加在配置類上,該類作為Spring啟動(dòng)的入口 @ComponentScan 和@Configuration配合使用,加在配置類上,用于掃描包中所有@Component注解的類 84. 在DDR8Memory類和IntelCpu類上添加@Component注解 85. 修改Computer類: 86. @Component 87. public class Computer { 88. 89. @Value("蘋(píng)果電腦") 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. //創(chuàng)建基于注解的上下文對(duì)象 106. AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext(MyConfig.class); 107. //獲得Computer對(duì)象 108. Computer computer = cxt.getBean(Computer.class); 109. computer.start(); 110. } 111. } 總結(jié) 本章我們學(xué)習(xí)了兩種Spring的配置方法,XML配置的好處是:和代碼耦合性低,容易維護(hù),注解配置的好處是:代碼簡(jiǎn)潔。兩種配置方法的優(yōu)勢(shì)互補(bǔ),在實(shí)際開(kāi)發(fā)過(guò)程中一般會(huì)使用XML和注解混合進(jìn)行配置。 |
|
來(lái)自: 好程序員IT > 《Java培訓(xùn)教程》