小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Java干貨:分享Spring框架之IOC的基本配置

 好程序員IT 2019-07-10

前言:上一章我們學(xué)習(xí)了Spring的IOC特性以及IOC的實現(xiàn)原理:注解和反射,本章我們將學(xué)習(xí)如何在Spring中使用IOC。

Spring的IOC配置

Spring最重要的特性是IOC控制反轉(zhuǎn),利于IOC我們能降低對象之間的耦合性。

IOC需要通過一定的配置實現(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、電子郵件、國際化、校驗和調(diào)度功能。它包含Spring Core組件,能實現(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正在運行....");

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)存寫入數(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+"電腦啟動了");

     cpu.run();

     memory.read();

     memory.write();

        }

49. }

在maven項目的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對象-->

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>標簽代表在容器中創(chuàng)建一個Java對象,屬性id代表對象名,class是對象的類型。

在配置文件中首先創(chuàng)建了一個cpu對象和一個memory對象,然后創(chuàng)建了一個computer對象,computer中有Cpu類型的cpu屬性和Memory類型memory屬性以及String類型的brand屬性,這里使用依賴注入的方式給屬性賦值。

<property name="cpu" ref="cpu"></property>

property 指的是對象的屬性,name是屬性名,ref是對象引用,這里引用了前面的cpu對象。

<property name="brand" value="華碩電腦"></property>

brand屬性注入的是數(shù)值而不是對象引用,這里使用value注入值。

Spring上下文對象

Spring容器可以看做是一個JavaBean的工廠BeanFactory,BeanFactory負責(zé)創(chuàng)建并保存各個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)用程序上下文對象

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代碼和注解實現(xiàn)配置,這種配置方法代碼更加簡潔。

常用注解:

@Component

配置到類上面,Spring容器會自動掃描并添加有該注解類的對象

@Autowired

配置到屬性或set方法上,容器會將容器中同類型的對象自動注入到屬性中

@Qualifier

用于給不同的組件設(shè)置標識,用于區(qū)分多個相同類型的對象

@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.         //創(chuàng)建基于注解的上下文對象

106.         AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext(MyConfig.class);

107.         //獲得Computer對象

108.         Computer computer = cxt.getBean(Computer.class);

109.         computer.start();

110.     }

111. }

總結(jié)

本章我們學(xué)習(xí)了兩種Spring的配置方法,XML配置的好處是:和代碼耦合性低,容易維護,注解配置的好處是:代碼簡潔。兩種配置方法的優(yōu)勢互補,在實際開發(fā)過程中一般會使用XML和注解混合進行配置。

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多