spring怎么实现依赖注入DI
发表于:2025-11-08 作者:千家信息网编辑
千家信息网最后更新 2025年11月08日,这篇文章主要介绍了spring怎么实现依赖注入DI的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇spring怎么实现依赖注入DI文章都会有所收获,下面我们一起来看看吧。s
千家信息网最后更新 2025年11月08日spring怎么实现依赖注入DI
这篇文章主要介绍了spring怎么实现依赖注入DI的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇spring怎么实现依赖注入DI文章都会有所收获,下面我们一起来看看吧。
spring依赖注入DI
1、创建一个maven项目
mvn archetype:generate -DarchetypeCatalog=internal
2、修改pom.xml
引入需要的依赖,首先spring-context,还是spring-test,最后还有junit。
UTF-8 4.3.7.RELEASE junit junit 4.12 test org.springframework spring-context ${springframework.version} org.springframework spring-test ${springframework.version} org.apache.maven.plugins maven-compiler-plugin 1.8 1.8 utf-8 maven-assembly-plugin 3.0.0 com.xueyoucto.xueyou.App jar-with-dependencies make-assembly package single
3、添加类Person和Body
package com.xueyou.demo;import org.springframework.stereotype.Component;@Componentpublic 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;@Componentpublic 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、运行测试类
结果如下:

7、从运行结果中我们能看到
Person类和Student类已经被依赖注入到spring中,spring能够使用这两个类了。
spring-test依赖无法使用问题
org.springframework spring-test 4.3.7.RELEASE test
去掉
test
关于"spring怎么实现依赖注入DI"这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对"spring怎么实现依赖注入DI"知识都有一定的了解,大家如果还想学习更多知识,欢迎关注行业资讯频道。
知识
内容
篇文章
结果
测试
运行
两个
价值
操作简单
文章
易懂
更多
看吧
行业
资讯
资讯频道
还是
问题
项目
频道
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
服务器 虚拟化 优势
企业站数据库设计
app软件开发与设计专业
山西电子软件开发
企业管理香港服务器
做了一年网络安全 想转行
软件开发资本化会计视野
榆林国家网络安全宣传活动
350分能上北航网络安全吗
amec软件开发
穿越火线是用什么软件开发的
如何返回切换服务器的菜单
ao财务数据库采集
网络安全b端
如何在用友数据库中修改税率
cs 服务器 命令
北京推广软件开发多少钱
外卖数据库系统用到什么技术
浏览器服务器ip代理
华为服务器供应问题
腾讯服务器ping地址
国内外数据库技术发展现状
浙江软件开发公司工资
中国工行软件开发笔试题
数据库查询所有员工信息吗
国内机械软件开发公司
软件开发视频大讲堂读书笔记
服务器主板都是通电自动开机吗
服务器cpu装机稳定吗
万方数据库怎样进入