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、首先我們需要引入依賴
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類,同時(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類,存入id為config的對(duì)象中,并使用<property>標(biāo)簽來(lái)為變量賦值。
(3)新建Test類
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 基于注解(常用)
基于注解有兩種方式,
- 配置類
- 掃包+注解
第一種:配置類
用一個(gè)Java來(lái)替代XML文件,把在XML中配置的內(nèi)容放到配置類
(1)添加BeanConfiguration配置類
@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è)配置類,啟動(dòng)時(shí)加載
@Bean表示加載時(shí)去調(diào)用dataConfig,然后將返回的對(duì)象DataConfig放入到IoC容器中,供開(kāi)發(fā)者使用。后面的name相當(dāng)于id,也可以用value替代
(2)編寫(xiě)Test類
public class Test { public static void main(String[] args) { //參數(shù)可以是類名.class,也可使用包名 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.wkx.configuration"); //這里傳入的時(shí)前面我們?cè)O(shè)置的Bean的name/value,如果不設(shè)置的話,可以傳方法名,或接口名 System.out.println(context.getBean("config")); } }
第二種:掃包+注解
更簡(jiǎn)單的方式,不再需要依賴于XML或者配置類,而是直接將bean的創(chuàng)建交給目標(biāo)類,在目標(biāo)類添加注解來(lái)創(chuàng)建
(1)給DataConfig添加@Component注解,目的是告訴spring框架,現(xiàn)在這個(gè)類需要被注入到IoC。然后spring讀到這個(gè)類的時(shí)候,就會(huì)將這個(gè)類創(chuàng)建對(duì)象,注入到IoC,@Value注解用來(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ì)象。還有能夠依賴注入
那什么是依賴注入呢?
比如A中有B的對(duì)象,然后我們創(chuàng)建A和B兩個(gè)對(duì)象,對(duì)會(huì)自動(dòng)的把B裝入A
下面我舉個(gè)例子
(1)聲明一個(gè)全局變量類GlobalConfig,里面包含DataConfig對(duì)象,因此要完成依賴注入。此時(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ò)類型byType注入,如果要通過(guò)名字注入,給他取名字,可以加上@Qualifier("name")來(lái)完成名稱的映射,同時(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)文章
淺析非對(duì)稱加密在接口參數(shù)中的實(shí)現(xiàn)
接口層做數(shù)據(jù)加密應(yīng)該算是老生常談的一件事了,業(yè)界用的比較多的,不外乎是對(duì)稱加密,非對(duì)稱加密以及兩者的結(jié)合。本文就來(lái)聊聊非對(duì)稱加密在接口參數(shù)中的實(shí)現(xiàn),希望對(duì)大家有所幫助2023-02-02Java中的Semaphore信號(hào)量簡(jiǎn)析
這篇文章主要介紹了Java中的Semaphore信號(hào)量簡(jiǎn)析,Semaphore:信號(hào)量,用來(lái)限制能同時(shí)訪問(wèn)共享資源的線程上限,使用Semaphore實(shí)現(xiàn)簡(jiǎn)單連接池,對(duì)比享元模式下的實(shí)現(xiàn)(用wait和notify),性能和可讀性要更好,需要的朋友可以參考下2023-12-12Java實(shí)現(xiàn)圖書(shū)管理系統(tǒng)的示例代碼
這篇文章將通過(guò)Java實(shí)現(xiàn)一個(gè)簡(jiǎn)答的圖書(shū)管理系統(tǒng),本圖書(shū)管理系統(tǒng)用對(duì)象數(shù)組的方式來(lái)提供操作方法,比較特別,建議新手學(xué)習(xí),這對(duì)理解Java面向?qū)ο笥泻艽髱椭?/div> 2022-11-11實(shí)現(xiàn)了基于TCP的Java Socket編程實(shí)例代碼
這篇文章主要介紹了基于TCP的Java Socket編程實(shí)例代碼,有需要的朋友可以參考一下2013-12-12使用mybatis報(bào)Invalid bound statement解決分析
這篇文章主要為大家介紹了使用mybatis報(bào)Invalid bound statement原因解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12Spring緩存注解@Cacheable @CacheEvit @CachePut使用介紹
Spring在3.1版本,就提供了一條基于注解的緩存策略,實(shí)際使用起來(lái)還是很絲滑的,本文將針對(duì)幾個(gè)常用的注解進(jìn)行簡(jiǎn)單的介紹說(shuō)明,有需要的小伙伴可以嘗試一下2021-07-07快速學(xué)習(xí)JavaWeb中監(jiān)聽(tīng)器(Listener)的使用方法
這篇文章主要幫助大家快速學(xué)習(xí)JavaWeb中監(jiān)聽(tīng)器(Listener)的使用方法,感興趣的小伙伴們可以參考一下2016-09-09最新評(píng)論