服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - spring如何实现依赖注入DI(spring-test方式)

spring如何实现依赖注入DI(spring-test方式)

2022-09-06 11:10archer.wu Java教程

本文主要介绍如何实现spring 的依赖注入,并且浅显的讲述一下注入需要注意的事项。如有错误或未考虑完全的地方,望不吝赐教

spring依赖注入DI

1、创建一个maven项目

?
1
mvn archetype:generate -DarchetypeCatalog=internal

2、修改pom.xml

引入需要的依赖,首先spring-context,还是spring-test,最后还有junit。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<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

?
1
2
3
4
5
6
7
8
9
10
11
12
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;
}
?
1
2
3
4
5
6
7
8
9
10
11
12
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

需要注意的是,这里需要指定扫描的包

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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、新建一个测试类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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、运行测试类

结果如下:

spring如何实现依赖注入DI(spring-test方式)

7、从运行结果中我们能看到

Person类和Student类已经被依赖注入到spring中,spring能够使用这两个类了。 

spring-test依赖无法使用问题

?
1
2
3
4
5
6
<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.3.7.RELEASE</version>
            <scope>test</scope>
</dependency>

去掉

?
1
<scope>test</scope>

好了,解决!以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://archer.blog.csdn.net/article/details/71055262

延伸 · 阅读

精彩推荐