spring怎么实现依赖注入DI

发布时间:2022-03-10 09:12:45 作者:iii
来源:亿速云 阅读:119

这篇文章主要介绍了spring怎么实现依赖注入DI的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇spring怎么实现依赖注入DI文章都会有所收获,下面我们一起来看看吧。

spring依赖注入DI

1、创建一个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、运行测试类

结果如下:

spring怎么实现依赖注入DI

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

Person类和Student类已经被依赖注入到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>

关于“spring怎么实现依赖注入DI”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“spring怎么实现依赖注入DI”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. Spring 依赖注入后行为实现
  2. 手写 Spring 事务、IOC、DI 和 MVC

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring di

上一篇:Web的Filter,Listener和Servlet怎么用

下一篇:小程序方案可以解决商家什么问题

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》