spring如何實(shí)現(xiàn)依賴注入DI(spring-test方式)
spring依賴注入DI
1、創(chuàng)建一個(gè)maven項(xiàng)目
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、新建一個(gè)測(cè)試類
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、運(yùn)行測(cè)試類
結(jié)果如下:

7、從運(yùn)行結(jié)果中我們能看到
Person類和Student類已經(jīng)被依賴注入到spring中,spring能夠使用這兩個(gè)類了。
spring-test依賴無(wú)法使用問題
<dependency> ? ? ? ? ? ? <groupId>org.springframework</groupId> ? ? ? ? ? ? <artifactId>spring-test</artifactId> ? ? ? ? ? ? <version>4.3.7.RELEASE</version> ? ? ? ? ? ? <scope>test</scope> </dependency>
去掉
<scope>test</scope>
好了,解決!以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java8中的LocalDateTime和Date一些時(shí)間操作方法
這篇文章主要介紹了Java8中的LocalDateTime和Date一些時(shí)間操作方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
利用Spring插件實(shí)現(xiàn)策略模式的案例詳解
Spring插件提供了一種更實(shí)用的插件開發(fā)方法,它提供了插件實(shí)現(xiàn)擴(kuò)展核心系統(tǒng)功能的核心靈活性,但當(dāng)然不提供核心OSGi功能,如動(dòng)態(tài)類加載或運(yùn)行時(shí)安裝和部署插件,本文就來(lái)聊下如何使用spring插件來(lái)實(shí)現(xiàn)策略模式,需要的朋友可以參考下2023-05-05
基于Java設(shè)計(jì)一個(gè)高并發(fā)的秒殺系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了如何基于Java設(shè)計(jì)一個(gè)高并發(fā)的秒殺系統(tǒng),文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以參考下2023-10-10
利用JSONObject.toJSONString()包含或排除指定的屬性
這篇文章主要介紹了利用JSONObject.toJSONString()包含或排除指定的屬性,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
SpringCloud2020版本配置與環(huán)境搭建教程詳解
這篇文章主要介紹了SpringCloud2020版本配置與環(huán)境搭建教程詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
Java編程小實(shí)例—數(shù)字時(shí)鐘的實(shí)現(xiàn)代碼示例
正所謂拳不離手曲不離口,java學(xué)習(xí)的過程中,練習(xí)還是要多一點(diǎn)比較好。接下來(lái)分享給大家一個(gè)Java編程的小實(shí)例,供朋友們參考。2017-10-10

