spring基础系列之JavaConfig配置详解
发布时间 - 2026-01-11 02:21:19 点击率:次早以前,Spring推荐使用XML的方式来定义Bean及Bean之间的装配规则,但是在Spring3之后,Spring提出的强大的JavaConfig这种类型安全的Bean装配方式,它基于Java代码的灵活性,使得装配的过程也变得及其灵活。

使用JavaConfig来装配Bean拥有其自己的一套规则,我们在这里来看一看:
1、规则
规则一:@Configuration注解
我们在定义JavaConfig类时,都会在其上加注@Configuration注解,来表明这是一个配置类,@Configuration注解底层是@Component注解,而且这个注解会被AnnotationConfigApplicationContext来进行加载,AnnotationConfigApplicationContext是ApplicationContext的一个具体实现,代表依据配置注解启动应用上下文。
规则二:@ComponentScan注解
我们使用JavaConfig的目的是为了实现以前XML配置实现的功能,首先就是组件扫描功能,将我们使用特定注解标注的类统一扫描加载到Spring容器,这一功能就是依靠@ComponentScan注解来实现的,我们可以为其指定位置参数来指定要扫描的包。
规则三:@Bean注解
使用@Bean注解我们可以实现XML配置中手动配置第三方Bean的功能,这里我们使用方法来定义Bean,并在方法前面加注@Bean注解,表示要将该方法返回的对象加载到Spring容器中,这样就对我们的方法定义带来了一些限制,这些限制包括方法的大概格式:
1-方法带返回值,且返回类型为你要加载的第三方类类型
2-方法的名称为默认的Bean的name,如果要自定义Bean的name,可以使用@Bean注解的name属性。
3-要实现注入只需要将要注入的Bean的类型作为参数,调用该类的带参数的构造器构建这个Bean,或者采用第二种方式:先创建这个类的对象,然后调用该对象的set方法进行注入,以被注入的Bean的方法为参数
规则验证:
首先我们创建几个测试类
针对第一种注入方式:
1-StudentService
import org.springframework.stereotype.Service;
@Service
public class StudentService {
public void study(){
System.out.println("学生学习Java");
}
}
2-TeacherService
import org.springframework.stereotype.Service;
@Service
public class TeacherService {
private StudentService studentService;
public TeacherService(StudentService studentService){
this.studentService=studentService;
}
public void teach(){
studentService.study();
}
}
3-Config:这是针对第一种注入方式而设,需要在TeacherService 中定义带参数的构造器
import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
//@ComponentScan
public class Config {
@Bean(name="student")
public StudentService studentService(){
return new StudentService();
}
@Bean(name="teacher")
public TeacherService teacherService(StudentService studentService){
return new TeacherService(studentService);
}
}
针对第二种注入方式:
1-StudentService
public class StudentService {
public void study(){
System.out.println("学生在学习Java");
}
}
2-TeacherService
public class TeacherService {
private StudentService studentService;
public StudentService getStudentService() {
return studentService;
}
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}
public void teach(){
studentService.study();
}
}
3-Config:这是采用第二种注入方式:需要在TeacherService中提供set方法
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Config {
@Bean
public StudentService student(){
return new StudentService();
}
@Bean
public TeacherService teacher(){
TeacherService teacherService = new TeacherService();
teacherService.setStudentService(student());
return teacherService;
}
}
4-测试类:TestMain
import java.util.Iterator;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestMain {
public static void main(String[] args) {
AnnotationConfigApplicationContext acac = new AnnotationConfigApplicationContext(Config.class);
TeacherService teacher = acac.getBean(TeacherService.class);
teacher.teach();
Iterator<String> i = acac.getBeanFactory().getBeanNamesIterator();
while(i.hasNext()){
System.out.println(i.next());
}
acac.close();
}
}
执行结果:
七月 14, 2017 4:10:56 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7e6cbb7a: startup date [Fri Jul 14 16:10:56 CST 2017]; root of context hierarchy 学生学习Java org.springframework.context.annotation.internalConfigurationAnnotationProcessor org.springframework.context.annotation.internalAutowiredAnnotationProcessor org.springframework.context.annotation.internalRequiredAnnotationProcessor org.springframework.context.annotation.internalCommonAnnotationProcessor org.springframework.context.event.internalEventListenerProcessor org.springframework.context.event.internalEventListenerFactory config student teacher environment systemProperties systemEnvironment org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry messageSource applicationEventMulticaster lifecycleProcessor 七月 14, 2017 4:10:59 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose 信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@7e6cbb7a: startup date [Fri Jul 14 16:10:56 CST 2017]; root of context hierarchy
该测试结果中打印出的是Spring上下文中所有加载的Bean的名称(name)。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# spring
# JavaConfig配置
# JavaConfig
# Spring中基于Java的配置@Configuration和@Bean用法详解
# Java语言读取配置文件config.properties的方法讲解
# 使用纯java config来配置spring mvc方式
# Spring 使用JavaConfig实现配置的方法步骤
# spring使用JavaConfig进行配置的方法
# 使用JavaConfig配置Spring的流程步骤
# Spring配置扩展之JavaConfig的使用小结
# 加载
# 这是
# 第二种
# 第三方
# 学生学习
# 第一种
# 自己的
# 的是
# 下午
# 几个
# 这一
# 你要
# 在这
# 推荐使用
# 会在
# 并在
# 这是一个
# 我们可以
# 带来了
# 自定义
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
Win11怎么恢复误删照片_Win11数据恢复工具使用【推荐】
宙斯浏览器怎么屏蔽图片浏览 节省手机流量使用设置方法
黑客如何利用漏洞与弱口令入侵网站服务器?
Laravel如何安装使用Debugbar工具栏_Laravel性能调试与SQL监控插件【步骤】
php打包exe后无法访问网络共享_共享权限设置方法【教程】
php中::能调用final静态方法吗_final修饰静态方法调用规则【解答】
网站视频制作书签怎么做,ie浏览器怎么将网站固定在书签工具栏?
UC浏览器如何切换小说阅读源_UC浏览器阅读源切换【方法】
独立制作一个网站多少钱,建立网站需要花多少钱?
BootStrap整体框架之基础布局组件
如何在IIS中新建站点并配置端口与物理路径?
用v-html解决Vue.js渲染中html标签不被解析的问题
图片制作网站免费软件,有没有免费的网站或软件可以将图片批量转为A4大小的pdf?
Laravel如何发送邮件_Laravel Mailables构建与发送邮件的简明教程
高防服务器:AI智能防御DDoS攻击与数据安全保障
如何自定义safari浏览器工具栏?个性化设置safari浏览器界面教程【技巧】
Laravel API资源类怎么用_Laravel API Resource数据转换
如何安全更换建站之星模板并保留数据?
浅谈redis在项目中的应用
香港服务器建站指南:外贸独立站搭建与跨境电商配置流程
简单实现Android文件上传
广州网站制作公司哪家好一点,广州欧莱雅百库网络科技有限公司官网?
使用PHP下载CSS文件中的所有图片【几行代码即可实现】
高端云建站费用究竟需要多少预算?
晋江文学城电脑版官网 晋江文学城网页版直接进入
Laravel模型事件有哪些_Laravel Model Event生命周期详解
原生JS实现图片轮播切换效果
Laravel如何将应用部署到生产服务器_Laravel生产环境部署流程
郑州企业网站制作公司,郑州招聘网站有哪些?
Laravel怎么实现模型属性转换Casting_Laravel自动将JSON字段转为数组【技巧】
如何用已有域名快速搭建网站?
Laravel怎么实现API接口鉴权_Laravel Sanctum令牌生成与请求验证【教程】
如何彻底卸载建站之星软件?
如何在IIS7中新建站点?详细步骤解析
如何快速生成可下载的建站源码工具?
软银砸40亿美元收购DigitalBridge 强化AI资料中心布局
音乐网站服务器如何优化API响应速度?
html5的keygen标签为什么废弃_替代方案说明【解答】
深圳网站制作的公司有哪些,dido官方网站?
Laravel如何正确地在控制器和模型之间分配逻辑_Laravel代码职责分离与架构建议
使用spring连接及操作mongodb3.0实例
Swift中switch语句区间和元组模式匹配
Laravel如何实现本地化和多语言支持?(i18n教程)
JS实现鼠标移上去显示图片或微信二维码
Win11怎样安装网易有道词典_Win11安装词典教程【步骤】
无锡营销型网站制作公司,无锡网选车牌流程?
Laravel如何理解并使用服务容器(Service Container)_Laravel依赖注入与容器绑定说明
如何用腾讯建站主机快速创建免费网站?
弹幕视频网站制作教程下载,弹幕视频网站是什么意思?
如何为不同团队 ID 动态生成多个“认领值班”按钮

