Spring?IOC注入的兩種方式詳解以及代碼示例
Ioc是Spring全家桶各個(gè)功能模塊的基礎(chǔ),創(chuàng)建對(duì)象的容器。
AOP也是以IoC為基礎(chǔ),AOP是面向切面編程,抽象化的面向?qū)ο?/p>
AOP功能:打印日志,事務(wù),權(quán)限處理
IoC
翻譯為控制反轉(zhuǎn),即將對(duì)象的創(chuàng)建進(jìn)行反轉(zhuǎn)。常規(guī)情況下,對(duì)象都是開(kāi)發(fā)者手動(dòng)創(chuàng)建的,使用IoC開(kāi)發(fā)者不再需要?jiǎng)?chuàng)建對(duì)象,而是由IoC容器根據(jù)需求自動(dòng)創(chuàng)建項(xiàng)目所需要的對(duì)象
- 不用IoC:所有對(duì)象開(kāi)發(fā)者自己創(chuàng)建;
- 使用IoC:對(duì)象不用開(kāi)發(fā)者創(chuàng)建,而是交給spring框架完成
下面我們使用代碼來(lái)演示:
1.1、首先我們需要引入依賴(lài)
pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.20</version>
</dependency>1.2 有兩種方法創(chuàng)建
1.2.1 基于XML:
開(kāi)發(fā)者把需要的對(duì)象在XML中進(jìn)行配置,Spring框架讀取這個(gè)配置文件,根據(jù)配置文件的內(nèi)容來(lái)創(chuàng)建對(duì)象
(1)創(chuàng)建DataConfig類(lèi),同時(shí)加上@Data注解
/**
* @author 王凱欣
*/
@Data
public class DataConfig {
private String url;
private String driverName;
private String username;
private String password;
}(2)新建spring.xml文件,并配置如下內(nèi)容
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean class="com.wkx.ioc.DataConfig" id="config">
<property name="driverName" value="Driver"></property>
<property name="url" value="localhost:8080"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
</beans>
這樣可以通過(guò)反射獲取到DataConfig類(lèi),存入id為config的對(duì)象中,并使用<property>標(biāo)簽來(lái)為變量賦值。
(3)新建Test類(lèi)
package com.wkx.ioc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
System.out.println(context.getBean("config"));
}
}通過(guò)上面的方式來(lái)獲取到DataConfig
運(yùn)行后輸出

1.2.2 基于注解(常用)
基于注解有兩種方式,
- 配置類(lèi)
- 掃包+注解
第一種:配置類(lèi)
用一個(gè)Java來(lái)替代XML文件,把在XML中配置的內(nèi)容放到配置類(lèi)
(1)添加BeanConfiguration配置類(lèi)
@Configuration
public class BeanConfiguration {
@Bean(name = "config")
public DataConfig dataConfig(){
DataConfig dataConfig = new DataConfig();
dataConfig.setDriverName("Driver");
dataConfig.setUrl("localhost:3306/dbname");
dataConfig.setUsername("root");
dataConfig.setPassword("root");
return dataConfig;
}
}@Configration注解表示這是個(gè)配置類(lèi),啟動(dòng)時(shí)加載
@Bean表示加載時(shí)去調(diào)用dataConfig,然后將返回的對(duì)象DataConfig放入到IoC容器中,供開(kāi)發(fā)者使用。后面的name相當(dāng)于id,也可以用value替代
(2)編寫(xiě)Test類(lèi)
public class Test {
public static void main(String[] args) {
//參數(shù)可以是類(lèi)名.class,也可使用包名
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.wkx.configuration");
//這里傳入的時(shí)前面我們?cè)O(shè)置的Bean的name/value,如果不設(shè)置的話(huà),可以傳方法名,或接口名
System.out.println(context.getBean("config"));
}
}第二種:掃包+注解
更簡(jiǎn)單的方式,不再需要依賴(lài)于XML或者配置類(lèi),而是直接將bean的創(chuàng)建交給目標(biāo)類(lèi),在目標(biāo)類(lèi)添加注解來(lái)創(chuàng)建
(1)給DataConfig添加@Component注解,目的是告訴spring框架,現(xiàn)在這個(gè)類(lèi)需要被注入到IoC。然后spring讀到這個(gè)類(lèi)的時(shí)候,就會(huì)將這個(gè)類(lèi)創(chuàng)建對(duì)象,注入到IoC,@Value注解用來(lái)給類(lèi)中的變量賦值
@Data
@Component
public class DataConfig {
@Value("localhost:3306")
private String url;
@Value("Driver")
private String driverName;
@Value("root")
private String username;
@Value("root")
private String password;
}(2)掃包,掃描DataConfi,獲取到我們注入的值
public class Test {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.wkx.ioc");
System.out.println(context.getBean(DataConfig.class));
}
}IoC除了自動(dòng)創(chuàng)建對(duì)象。還有能夠依賴(lài)注入
那什么是依賴(lài)注入呢?
比如A中有B的對(duì)象,然后我們創(chuàng)建A和B兩個(gè)對(duì)象,對(duì)會(huì)自動(dòng)的把B裝入A
下面我舉個(gè)例子
(1)聲明一個(gè)全局變量類(lèi)GlobalConfig,里面包含DataConfig對(duì)象,因此要完成依賴(lài)注入。此時(shí)可以使用@Autowired注解,表示自動(dòng)裝載,他就會(huì)自己去IoC里查找
@Data
@Component
public class GlobalConfig {
@Value("8080")
private String prot;
@Value("/")
private String path;
@Autowired
private DataConfig dataConfig;
}其中,@Autowired注解表示通過(guò)類(lèi)型byType注入,如果要通過(guò)名字注入,給他取名字,可以加上@Qualifier("name")來(lái)完成名稱(chēng)的映射,同時(shí),DataConfig中的@Component("name"),兩個(gè)name要保持一致
(2)再去掃描包
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.wkx.ioc");
System.out.println(context.getBean(GlobalConfig.class));這樣就可以獲取到了

到此這篇關(guān)于Spring IOC注入的兩種方式詳解以及代碼示例的文章就介紹到這了,更多相關(guān)Spring IOC注入方式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IDEA如何加載resources文件夾下文件相對(duì)路徑
這篇文章主要介紹了IDEA如何加載resources文件夾下文件相對(duì)路徑問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
MyBatis批量插入數(shù)據(jù)過(guò)程解析
這篇文章主要介紹了MyBatis批量插入數(shù)據(jù)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
Jmeter多用戶(hù)并發(fā)壓力測(cè)試過(guò)程圖解
這篇文章主要介紹了Jmeter多用戶(hù)并發(fā)壓力測(cè)試過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
Java使用JSqlParser解析復(fù)雜的SQL語(yǔ)句的詳細(xì)步驟
這篇文章介紹了在Java代碼中使用 JSqlParser解析復(fù)雜SQL語(yǔ)句的相關(guān)內(nèi)容,包括JSqlParser是什么及安裝步驟,其使用場(chǎng)景如分析、轉(zhuǎn)換、生成和驗(yàn)證SQL語(yǔ)句,處理SQL注入攻擊的方法,以及解析復(fù)雜SQL語(yǔ)句的步驟、示例代碼和對(duì)不同類(lèi)型語(yǔ)句的處理,需要的朋友可以參考下2025-01-01
Spring Boot FeignClient 如何捕獲業(yè)務(wù)異常信息
這篇文章主要介紹了Spring Boot FeignClient 如何捕獲業(yè)務(wù)異常信息的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
Spring中BeanUtils.copyProperties的坑及解決
這篇文章主要介紹了Spring中BeanUtils.copyProperties的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
Java Map 按照Value排序的實(shí)現(xiàn)方法
Map是鍵值對(duì)的集合接口,它的實(shí)現(xiàn)類(lèi)主要包括:HashMap,TreeMap,Hashtable以及LinkedHashMap等。這篇文章主要介紹了Java Map 按照Value排序的實(shí)現(xiàn)方法,需要的朋友可以參考下2016-08-08

