javascript
Spring——DI
一 依賴注入
1.1 什么是依賴注入
1.2 @Bean注入
? ?spring IOC容器提供了依賴注入功能,可以將一個組件依賴到的對象,在使用之前注入到合適位置
? spring IOC解決依賴注入;在配置類congfig中為Bean組件初始化方法增加參數,spring IOC容器會在初始化時候自動根據類型注入Bean組件對象
案例:
?1.創建Saw
package cn.tedu.demo;import java.io.Serializable;/*** @Author Mr WeiLiang* @Date 2021/8/5 0005 09:22* @Version 1.0*/ public class Saw implements Serializable {private String name="開天斧";@Overridepublic String toString() {return name;} }?2.創建Worker
package cn.tedu.demo;import java.io.Serializable;/*** @Author Mr WeiLiang* @Date 2021/8/5 0005 09:22* @Version 1.0*/ public class Worker implements Serializable {private String name="光頭強";public Saw saw;public void work(){System.out.println(name + "使用" + saw +"砍樹");}}?3.創建配置文件
package cn.tedu.context;import cn.tedu.demo.Saw; import cn.tedu.demo.Worker; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;/*** @Author Mr WeiLiang* @Date 2021/8/5 0005 09:22* @Version 1.0*/ @Configuration public class Config {@Beanpublic Saw saw(){return new Saw();}@Bean/*** spring會自動根據變量類型匹配Bean組件的類型* 如果匹配上,就將Bean組件注入到方法參數中*/public Worker worker(Saw s){Worker worker = new Worker();worker.saw=s;return worker;} }?4.測試案例
package cn.tedu.test;import cn.tedu.context.Config; import cn.tedu.demo.Worker; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext;/*** @Author Mr WeiLiang* @Date 2021/8/5 0005 09:22* @Version 1.0*/ public class TestCase {AnnotationConfigApplicationContext ctx;@Beforepublic void init(){ctx=new AnnotationConfigApplicationContext(Config.class);}@Afterpublic void destroy(){ctx.close();}@Testpublic void testWorker(){Worker worker = ctx.getBean("worker", Worker.class);worker.work();} }?運行結果:
光頭強使用開天斧砍樹? ?spring IOC組件注入時候組件自動匹配規則:
注入失敗案例
package cn.tedu.context;import cn.tedu.demo.Saw; import cn.tedu.demo.Worker; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope;/*** @Author Mr WeiLiang* @Date 2021/8/5 0005 09:22* @Version 1.0*/ @Configuration public class Config {@Beanpublic Saw saw(){return new Saw();}@Beanpublic Saw saw2(){return new Saw();}@Bean/*** spring會自動根據變量類型匹配Bean組件的類型* 如果匹配上,就將Bean組件注入到方法參數中*/public Worker worker(Saw s){Worker worker = new Worker();worker.saw=s;return worker;} }運行結果:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'worker' defined in cn.tedu.context.Config: Unsatisfied dependency expressed through method 'worker' parameter 0; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'cn.tedu.demo.Saw' available: expected single matching bean but found 2: saw,saw2碰到這種問題,只需要將需要注入的類型名改為@Bean相同的類型名
1.3 @Autowired
? spring提供的組件掃描功能,在掃描時候也可以完成依賴注入,這樣可以減少編碼提高編程效率
案例
創建電鋸類Saw
package cn.tedu.demo;import org.springframework.stereotype.Component;import java.io.Serializable;/*** @Author Mr WeiLiang* @Date 2021/8/5 0005 09:22* @Version 1.0*/ @Component public class Saw implements Serializable {private String name="開天斧";@Overridepublic String toString() {return name;} }創建工人類Worker
package cn.tedu.demo;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;import java.io.Serializable;/*** @Author Mr WeiLiang* @Date 2021/8/5 0005 09:22* @Version 1.0*/ @Component public class Worker implements Serializable {private String name="光頭強";@Autowiredpublic Saw saw;public void work(){System.out.println(name + "使用" + saw +"砍樹");}}創建配置類
package cn.tedu.context;import cn.tedu.demo.Saw; import cn.tedu.demo.Worker; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration;/*** @Author Mr WeiLiang* @Date 2021/8/5 0005 09:22* @Version 1.0*/ @Configuration @ComponentScan(basePackages = "cn.tedu.demo") public class Config {}測試案例
package cn.tedu.test;import cn.tedu.context.Config; import cn.tedu.demo.Worker; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext;/*** @Author Mr WeiLiang* @Date 2021/8/5 0005 09:22* @Version 1.0*/ public class TestCase {AnnotationConfigApplicationContext ctx;@Beforepublic void init(){ctx=new AnnotationConfigApplicationContext(Config.class);}@Afterpublic void destroy(){ctx.close();}@Testpublic void testWorker(){Worker worker = ctx.getBean("worker", Worker.class);worker.work();} }測試結果
光頭強使用開天斧砍樹1.4 set方法注入
?測試案例:
?
二 IOC/DI解耦
2.1 利用接口解耦
? ?利用spring IOC容器提供的DI可以將工具對象注入給工人對象,繼而解決之間的依賴關系,并且由于依賴于接口,所以利用spring IOC就可以控制組件的組合關系,實現松耦合
案例:利用spring實現解耦
聲明工具接口
package cn.tedu.demo2;/*** @Author Mr WeiLiang* @Date 2021/8/5 0005 09:22* @Version 1.0*/ /*** 抽象的工具類型* */ public interface Tool { }聲明工人類
package cn.tedu.demo2;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;import java.io.Serializable;/*** @Author Mr WeiLiang* @Date 2021/8/5 0005 09:22* @Version 1.0*/ @Component public class Worker implements Serializable {private String name="光頭強";@Autowiredprivate Tool tool;public void work(){System.out.println(name + "使用" + tool +"砍樹");} }聲明電鋸類
package cn.tedu.demo2;/*** @Author Mr WeiLiang* @Date 2021/8/5 0005 09:22* @Version 1.0*/ public class Saw implements Tool{private String name="小鋸子";@Overridepublic String toString() {return name;} }聲明斧子類
package cn.tedu.demo2;import org.springframework.stereotype.Component;import java.io.Serializable;/*** @Author Mr WeiLiang* @Date 2021/8/5 0005 09:22* @Version 1.0*/ @Component public class Axe implements Serializable,Tool{private String name="開天斧";@Overridepublic String toString() {return name;} }配置類
package cn.tedu.context2;import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration;/*** @Author Mr WeiLiang* @Date 2021/8/5 0005 09:22* @Version 1.0*/ @Configuration @ComponentScan(basePackages = "cn.tedu.demo2") public class Config { }測試類
package cn.tedu.test;import cn.tedu.context2.Config; import cn.tedu.demo2.Worker; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext;/*** @Author Mr WeiLiang* @Date 2021/8/5 0005 09:22* @Version 1.0*/ public class TestCase2 {AnnotationConfigApplicationContext ctx;@Beforepublic void init(){ctx=new AnnotationConfigApplicationContext(Config.class);}@Afterpublic void destroy(){ctx.close();}@Testpublic void testWorker(){Worker worker = ctx.getBean("worker", Worker.class);worker.work();} }測試結果
光頭強使用開天斧砍樹2.2 @Autowired注入規則
??
?
?案例:將斧子類和電鋸類都加上@Component注解
1.在斧子類上設置自定義BeanID
package cn.tedu.demo2;import org.springframework.stereotype.Component;import java.io.Serializable;/*** @Author Mr WeiLiang* @Date 2021/8/5 0005 09:22* @Version 1.0*/ @Component("tool") public class Axe implements Serializable,Tool{private String name="開天斧";@Overridepublic String toString() {return name;} }測試結果:
光頭強使用開天斧砍樹2.重構Worker類,設定@Qualifier("saw")
package cn.tedu.demo2;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component;import java.io.Serializable;/*** @Author Mr WeiLiang* @Date 2021/8/5 0005 09:22* @Version 1.0*/ @Component public class Worker implements Serializable {private String name="光頭強";@Autowired@Qualifier("saw")private Tool tool;public void work(){System.out.println(name + "使用" + tool +"砍樹");} }測試結果:
光頭強使用小鋸子砍樹2.3 同時使用@Bean @Component
??
1.利用@Bean在配置類Config中聲明組件
package cn.tedu.context2;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration;import java.util.Date;/*** @Author Mr WeiLiang* @Date 2021/8/5 0005 09:22* @Version 1.0*/ @Configuration @ComponentScan(basePackages = "cn.tedu.demo2") public class Config {@Beanpublic Date date(){return new Date();} }?2.利用@Component聲明組件,注入Date類型組件
package cn.tedu.demo2;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;import java.util.Date;/*** @Author Mr WeiLiang* @Date 2021/8/5 0005 09:22* @Version 1.0*/ @Component public class Employee {private String name="傻狍子";@Autowiredprivate Date date;@Overridepublic String toString() {return "Employee{" +"name='" + name + '\'' +", date=" + date +'}';} }3.測試案例
@Testpublic void testEmployee(){Employee employee = ctx.getBean("employee", Employee.class);System.out.println(employee);}測試結果:輸出當前系統時間,表示Date注入成功
Employee{name='傻狍子', date=Sat Oct 30 21:32:46 CST 2021}案例:@Component 聲明的組件注入到@Bean聲明的組件
1.聲明Bean類型
package cn.tedu.demo2;/*** @Author Mr WeiLiang* @Date 2021/8/5 0005 09:22* @Version 1.0*/ public class Dept {private String name="小良哥";private Employee manager;public void setManager(Employee manager){this.manager=manager;}@Overridepublic String toString() {return "Dept{" +"name='" + name + '\'' +", manager=" + manager +'}';} }2.在配置類Config中利用@Bean聲明Bean組件
@Beanpublic Dept dept(Employee employee){Dept dept = new Dept();dept.setManager(employee);return dept;}3.測試案例
@Testpublic void testDept(){Dept dept = ctx.getBean("dept", Dept.class);System.out.println(dept);}測試結果
ept{name='小良哥', manager=Employee{name='傻狍子', date=Sat Oct 30 21:47:32 CST 2021}}3 使用Properties文件
3.1 Druld 連接池
案例:
1. 導入連接池
?
2.配置Config.java
?
?測試
測試結果
?
3.2 @PropertlesSource 和 Envlronment
? ?案例:
?
?
?3.3 @Value
? 利用@Value可以讀取當前系統環境Environment中的信息,注入到變量中,這個方式更加靈活方便
?
?
總結
以上是生活随笔為你收集整理的Spring——DI的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安全密钥传输浅解
- 下一篇: Python使用百度地图API实现地点信