您好,登錄后才能下訂單哦!
這篇文章主要介紹了Java中Spring框架之IOC如何配置,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
Spring最重要的特性是IOC控制反轉(zhuǎn),利于IOC我們能降低對(duì)象之間的耦合性。
IOC需要通過(guò)一定的配置實(shí)現(xiàn),配置方法分為:
1)使用xml文件配置
2)使用注解配置
使用Spring的基本功能,必須先導(dǎo)入Spring的依賴:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
Spring Context:向 Spring框架提供上下文信息。Spring 上下文包括企業(yè)服務(wù),例如JNDI、EJB、電子郵件、國(guó)際化、校驗(yàn)和調(diào)度功能。它包含Spring Core組件,能實(shí)現(xiàn)IOC的核心功能。
/**
* CPU接口
*/
public interface Cpu {
void run();
}
/**
* AMD的CPU
*/
public class AMDCpu implements Cpu {
public void run() {
System.out.println("AMD的CPU正在運(yùn)行....");
}
}
/**
* 內(nèi)存接口
*/
public interface Memory {
void read();
void write();
}
/**
* DDR8G的內(nèi)存
*/
public class DDR8GMemory implements Memory {
public void read() {
System.out.println("使用DDR8G的內(nèi)存讀取數(shù)據(jù)....");
}
public void write() {
System.out.println("使用DDR8G的內(nèi)存寫入數(shù)據(jù)....");
}
}
類似的IntelCpu和DDR16Memory類省略了代碼
/**
* 電腦類
*/
public class Computer {
private Cpu cpu;
private Memory memory;
private String brand;
...省略get\set
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();
}
}
在maven項(xiàng)目的resources目錄下,添加配置文件:
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- CPU對(duì)象-->
<bean id="cpu" class="com.qianfeng.springioc.demo3.IntelCpu"/>
<!--Memory對(duì)象-->
<bean id="memory" class="com.qianfeng.springioc.demo3.DDR16GMemory"/>
<!--電腦對(duì)象-->
<bean id="computer" class="com.qianfeng.springioc.demo3.Computer">
<!--屬性的注入-->
<property name="cpu" ref="cpu"></property>
<property name="memory" ref="memory"></property>
<property name="brand" value="小米電腦"></property>
</bean>
</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容器可以看做是一個(gè)JavaBean的工廠BeanFactory,BeanFactory負(fù)責(zé)創(chuàng)建并保存各個(gè)JavaBean,BeanFactory的子類有:
1)ClassPathXMLApplicationContext
基于XML配置文件上下文
2)AnnotationConfigApplicationContext
基于注解配置的上下文
3)FileSystemApplicationContext
基于文件系統(tǒng)的上下文
使用ClassPathXMLApplicationContext的方法:
public class TestComputer {
@Test
public void testComputer(){
//創(chuàng)建XML文件的應(yīng)用程序上下文對(duì)象
ClassPathXmlApplicationContext cxt =
new ClassPathXmlApplicationContext("applicationContext.xml");
//通過(guò)類型從容器獲得Java對(duì)象
Computer computer = cxt.getBean(Computer.class);
//還可以通過(guò)對(duì)象名獲得對(duì)象
// Computer computer = (Computer) cxt.getBean("computer");
computer.start();
}
}
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注解的類
在DDR8Memory類和IntelCpu類上添加@Component注解
修改Computer類:
@Component
public class Computer {
@Value("蘋果電腦")
private String brand;
@Autowired
private Cpu cpu;
@Autowired
private Memory memory;
....
}
@Configuration
@ComponentScan("com.qianfeng.springioc.demo4")
public class MyConfig {
public static void main(String[] args) {
//創(chuàng)建基于注解的上下文對(duì)象
AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext(MyConfig.class);
//獲得Computer對(duì)象
Computer computer = cxt.getBean(Computer.class);
computer.start();
}
}
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Java中Spring框架之IOC如何配置”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
免責(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)容。