溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java中Spring框架之IOC如何配置

發(fā)布時(shí)間:2021-12-07 11:41:51 來(lái)源:億速云 閱讀:155 作者:小新 欄目:編程語(yǔ)言

這篇文章主要介紹了Java中Spring框架之IOC如何配置,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

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文件配置

  1. /**

  2. * CPU接口

  3. */

  4. public interface Cpu {

  5. void run();

  6. }

  7. /**

  8. * AMD的CPU

  9. */

  10. public class AMDCpu implements Cpu {

  11. public void run() {

  12. System.out.println("AMD的CPU正在運(yùn)行....");

  13. }

  14. }

  15. /**

  16. * 內(nèi)存接口

  17. */

  18. public interface Memory {

  19. void read();

  20. void write();

  21. }

  22. /**

  23. * DDR8G的內(nèi)存

  24. */

  25. public class DDR8GMemory implements Memory {

  26. public void read() {

  27. System.out.println("使用DDR8G的內(nèi)存讀取數(shù)據(jù)....");

  28. }

  29. public void write() {

  30. System.out.println("使用DDR8G的內(nèi)存寫入數(shù)據(jù)....");

  31. }

  32. }

  33. 類似的IntelCpu和DDR16Memory類省略了代碼

  34. /**

  35. * 電腦類

  36. */

  37. public class Computer {

  38. private Cpu cpu;

  39. private Memory memory;

  40. private String brand;

  41. ...省略get\set

  42. 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();
            }

  43. }

在maven項(xiàng)目的resources目錄下,添加配置文件:

applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <beans xmlns="http://www.springframework.org/schema/beans"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  4. xmlns:context="http://www.springframework.org/schema/context"

  5. xsi:schemaLocation="http://www.springframework.org/schema/beans

  6. http://www.springframework.org/schema/beans/spring-beans.xsd

  7. http://www.springframework.org/schema/context

  8. http://www.springframework.org/schema/context/spring-context.xsd">

  9. <!-- CPU對(duì)象-->

  10. <bean id="cpu" class="com.qianfeng.springioc.demo3.IntelCpu"/>

  11. <!--Memory對(duì)象-->

  12. <bean id="memory" class="com.qianfeng.springioc.demo3.DDR16GMemory"/>

  13. <!--電腦對(duì)象-->

  14. <bean id="computer" class="com.qianfeng.springioc.demo3.Computer">

  15. <!--屬性的注入-->

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

  17. <property name="memory" ref="memory"></property>

  18. <property name="brand" value="小米電腦"></property>

  19. </bean>

  20. </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的方法:

  1. public class TestComputer {

  2. @Test

  3. public void testComputer(){

  4. //創(chuàng)建XML文件的應(yīng)用程序上下文對(duì)象

  5. ClassPathXmlApplicationContext cxt =

  6. new ClassPathXmlApplicationContext("applicationContext.xml");

  7. //通過(guò)類型從容器獲得Java對(duì)象

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

  9. //還可以通過(guò)對(duì)象名獲得對(duì)象

  10. //        Computer computer = (Computer) cxt.getBean("computer");

  11. computer.start();

  12. }

  13. }

Java中Spring框架之IOC如何配置

使用注解配置

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注解的類

  1. 在DDR8Memory類和IntelCpu類上添加@Component注解

  2. 修改Computer類:

  3. @Component

  4. public class Computer {

  5. @Value("蘋果電腦")

  6. private String brand;

  7. @Autowired

  8. private Cpu cpu;

  9. @Autowired

  10. private Memory memory;

  11. ....

  12. }

  13. @Configuration

  14. @ComponentScan("com.qianfeng.springioc.demo4")

  15. public class MyConfig {

  16. public static void main(String[] args) {

  17. //創(chuàng)建基于注解的上下文對(duì)象

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

  19. //獲得Computer對(duì)象

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

  21. computer.start();

  22. }

  23. }

Java中Spring框架之IOC如何配置

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Java中Spring框架之IOC如何配置”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI