spring如何實現(xiàn)依賴注入DI(spring-test方式)
spring依賴注入DI
1、創(chuàng)建一個maven項目
mvn archetype:generate -DarchetypeCatalog=internal
2、修改pom.xml
引入需要的依賴,首先spring-context,還是spring-test,最后還有junit。
<properties> ? ? ? ? <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> ? ? ? ? <springframework.version>4.3.7.RELEASE</springframework.version> ? ? </properties> ? ? ? <dependencies> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>junit</groupId> ? ? ? ? ? ? <artifactId>junit</artifactId> ? ? ? ? ? ? <version>4.12</version> ? ? ? ? ? ? <scope>test</scope> ? ? ? ? </dependency> ? ? ? ? <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.springframework</groupId> ? ? ? ? ? ? <artifactId>spring-context</artifactId> ? ? ? ? ? ? <version>${springframework.version}</version> ? ? ? ? </dependency> ? ? ? ? <!-- https://mvnrepository.com/artifact/org.springframework/spring-test --> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.springframework</groupId> ? ? ? ? ? ? <artifactId>spring-test</artifactId> ? ? ? ? ? ? <version>${springframework.version}</version> ? ? ? ? </dependency> ? ? ? </dependencies> ? ? <build> ? ? ? ? <plugins> ? ? ? ? ? ? <plugin> ? ? ? ? ? ? ? ? <groupId>org.apache.maven.plugins</groupId> ? ? ? ? ? ? ? ? <artifactId>maven-compiler-plugin</artifactId> ? ? ? ? ? ? ? ? <configuration> ? ? ? ? ? ? ? ? ? ? <source>1.8</source> ? ? ? ? ? ? ? ? ? ? <target>1.8</target> ? ? ? ? ? ? ? ? ? ? <encoding>utf-8</encoding> ? ? ? ? ? ? ? ? </configuration> ? ? ? ? ? ? </plugin> ? ? ? ? ? ? <plugin> ? ? ? ? ? ? ? ? <artifactId>maven-assembly-plugin</artifactId> ? ? ? ? ? ? ? ? <version>3.0.0</version> ? ? ? ? ? ? ? ? <configuration> ? ? ? ? ? ? ? ? ? ? <archive> ? ? ? ? ? ? ? ? ? ? ? ? <manifest> ? ? ? ? ? ? ? ? ? ? ? ? ? ? <mainClass>com.xueyoucto.xueyou.App</mainClass> ? ? ? ? ? ? ? ? ? ? ? ? </manifest> ? ? ? ? ? ? ? ? ? ? </archive> ? ? ? ? ? ? ? ? ? ? <descriptorRefs> ? ? ? ? ? ? ? ? ? ? ? ? <descriptorRef>jar-with-dependencies</descriptorRef> ? ? ? ? ? ? ? ? ? ? </descriptorRefs> ? ? ? ? ? ? ? ? </configuration> ? ? ? ? ? ? ? ? <executions> ? ? ? ? ? ? ? ? ? ? <execution> ? ? ? ? ? ? ? ? ? ? ? ? <id>make-assembly</id> <!-- this is used for inheritance merges --> ? ? ? ? ? ? ? ? ? ? ? ? <phase>package</phase> <!-- bind to the packaging phase --> ? ? ? ? ? ? ? ? ? ? ? ? <goals> ? ? ? ? ? ? ? ? ? ? ? ? ? ? <goal>single</goal> ? ? ? ? ? ? ? ? ? ? ? ? </goals> ? ? ? ? ? ? ? ? ? ? </execution> ? ? ? ? ? ? ? ? </executions> ? ? ? ? ? ? </plugin> ? ? ? ? </plugins> ? ? </build>
3、添加類Person和Body
package com.xueyou.demo; import org.springframework.stereotype.Component; @Component public class Person { ? ? public String getName() { ? ? ? ? return name; ? ? } ? ? public void setName(String name) { ? ? ? ? this.name = name; ? ? } ? ? private String name; }
package org.xueyou.demo; import org.springframework.stereotype.Component; @Component public class Body { ? ? public int getId() { ? ? ? ? return id; ? ? } ? ? public void setId(int id) { ? ? ? ? this.id = id; ? ? } ? ? private int id; }
4、在配置類App中,添加ComponentScan
需要注意的是,這里需要指定掃描的包
package com.xueyou.demo; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; /** ?* Hello world! ?*/ @Configuration @ComponentScan(basePackages = {"org.xueyou.demo","com.xueyou.demo"}) public class App { ? ? public static void main(String[] args) { ? ? ? ? System.out.println("Hello World!"); ? ? } }
5、新建一個測試類
package com.xueyou.demo; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.xueyou.demo.Body; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = App.class) public class Springtest { ? ? @Autowired ? ? private Body body; ? ? @Autowired ? ? private Person person; ? ? @Test ? ? public void testBodyIsNull(){ ? ? ? ? Assert.assertNotNull(body); ? ? } ? ? @Test ? ? public void testPersonIsNull(){ ? ? ? ? Assert.assertNotNull(person); ? ? } }
6、運行測試類
結(jié)果如下:
7、從運行結(jié)果中我們能看到
Person類和Student類已經(jīng)被依賴注入到spring中,spring能夠使用這兩個類了。
spring-test依賴無法使用問題
<dependency> ? ? ? ? ? ? <groupId>org.springframework</groupId> ? ? ? ? ? ? <artifactId>spring-test</artifactId> ? ? ? ? ? ? <version>4.3.7.RELEASE</version> ? ? ? ? ? ? <scope>test</scope> </dependency>
去掉
<scope>test</scope>
好了,解決!以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決mybatis竟然報Invalid value for getInt()的問題
使用mybatis遇到一個非常奇葩的問題,總是報Invalid value for getInt()的問題,怎么解決呢?下面小編通過場景分析給大家代來了mybatis報Invalid value for getInt()的解決方法,感興趣的朋友參考下吧2021-10-10IDEA 如何導(dǎo)入別人的javaweb項目進(jìn)行部署
這篇文章主要介紹了IDEA 如何導(dǎo)入別人的javaweb項目進(jìn)行部署,本文給大家分享我的詳細(xì)部署過程及遇到問題解決方法,需要的朋友可以參考下2023-03-03詳解如何把Java中if-else代碼重構(gòu)成高質(zhì)量代碼
這篇文章主要介紹了詳解如何把Java中if-else代碼重構(gòu)成高質(zhì)量代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Springboot通過aop實現(xiàn)事務(wù)控制過程解析
這篇文章主要介紹了Springboot通過aop實現(xiàn)事務(wù)控制過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-03-03SpringBoot默認(rèn)使用HikariDataSource數(shù)據(jù)源方式
這篇文章主要介紹了SpringBoot默認(rèn)使用HikariDataSource數(shù)據(jù)源方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10Spring MVC下 bootStrap服務(wù)器分頁代碼
因為Spring 對于ajax直接返回對象,到了WEB頁面就轉(zhuǎn)換成json 所以不需要使用JSON轉(zhuǎn)換封裝可以直接使用。接下來通過本文給大家分享Spring MVC下 bootStrap服務(wù)器分頁代碼,需要的的朋友參考下2017-03-03從JVM的內(nèi)存管理角度分析Java的GC垃圾回收機制
這篇文章主要介紹了從JVM的內(nèi)存管理角度分析Java的GC垃圾回收機制,帶有GC是Java語言的重要特性之一,需要的朋友可以參考下2015-11-11java通過控制鼠標(biāo)實現(xiàn)屏幕廣播的方法
這篇文章主要介紹了java通過控制鼠標(biāo)實現(xiàn)屏幕廣播的方法,針對前面一篇Java屏幕共享功能進(jìn)行了改進(jìn),實現(xiàn)了鼠標(biāo)控制功能,具有一定的實用價值,需要的朋友可以參考下2014-12-12基于Jackson實現(xiàn)API接口數(shù)據(jù)脫敏的示例詳解
用戶的一些敏感數(shù)據(jù),例如手機號、郵箱、身份證等信息,在數(shù)據(jù)庫以明文存儲,但在接口返回數(shù)據(jù)給瀏覽器(或三方客戶端)時,希望對這些敏感數(shù)據(jù)進(jìn)行脫敏,所以本文就給大家介紹以惡如何利用Jackson實現(xiàn)API接口數(shù)據(jù)脫敏,需要的朋友可以參考下2023-08-08