Springboot如何注入装配到IOC容器
发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,这篇文章主要介绍了Springboot如何注入装配到IOC容器,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。1、通过bean注解装配到
千家信息网最后更新 2026年11月02日Springboot如何注入装配到IOC容器1、通过bean注解装配到IOC容器
这篇文章主要介绍了Springboot如何注入装配到IOC容器,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
1、通过bean注解装配到IOC容器
创建装配的类,如下
package com.sboot.pr.bean;/** * @author ygb * @Mailbox 941201063@qq.com * @date 2021年10月28日 * 通过bean注解装配到IOC容器 */public class BeanPOJO { private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }通过bean注解装配BeanPOJO到IOC容器
package com.sboot.pr.config; import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration; import com.sboot.pr.bean.BeanPOJO; /** * @author ygb * @Mailbox 941201063@qq.com * @date 2021年10月28日 * 配置类文件 */@Configurationpublic class BeanConfig { /** * 通过bean注解装配BeanPOJO到IOC容器 * @return */ @Bean(name = "beanPOJO") public BeanPOJO initBeanPOJO() { BeanPOJO pojo = new BeanPOJO(); pojo.setId(1); pojo.setName("BeanPOJO"); pojo.setAge(29); return pojo; }}把装配的BeanPOJO 注入
package com.sboot.pr.controller; import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController; import com.sboot.pr.bean.BeanPOJO;import com.sboot.pr.bean.ComponentPOJO;import com.sboot.pr.bean.ComponentScanPOJO; /** * @author ygb * @Mailbox 941201063@qq.com * @date 2021年10月28日 */@RestControllerpublic class TestController { @Autowired private BeanPOJO beanPoJO; /** * 获取通过Bean注解装配到IOC容器的对象 * @return */ @GetMapping("/boot/getBeanPOJO") public BeanPOJO getBeanPOJO() { return beanPoJO; }}访问注入的BeanPOJO信息
http://localhost:1111/boot/getBeanPOJO
2、通过Component注解扫描装配bean到IOC容器
创建装配的类,如下
package com.sboot.pr.bean; import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.ComponentScan;import org.springframework.stereotype.Component; /** * @author ygb * @Mailbox 941201063@qq.com * @date 2021年10月28日 * 通过Component注解扫描注入bean到IOC容器 * 指定bean名称,默认首个字母小写 */@Component("componentPOJO")public class ComponentPOJO { @Value("2") private int id; @Value("ComponentPOJO") private String name; @Value("29") private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }把装配的ComponentPOJO 注入
package com.sboot.pr.controller; import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController; import com.sboot.pr.bean.BeanPOJO;import com.sboot.pr.bean.ComponentPOJO;import com.sboot.pr.bean.ComponentScanPOJO; /** * @author ygb * @Mailbox 941201063@qq.com * @date 2021年10月28日 */@RestControllerpublic class TestController { @Autowired private ComponentPOJO componentPOJO; /** * 获取通过Component注解装配到IOC容器的对象 * @return */ @GetMapping("/boot/getComponentPOJO") public ComponentPOJO getComponentPOJO() { return componentPOJO; } }访问注入的ComponentPOJO 信息
http://localhost:1111/boot/getComponentPOJO
3、通过ComponentScan注解扫描装配bean到IOC容器
创建装配的类,如下
package com.sboot.pr.bean; import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration; /** * @author ygb * @Mailbox 941201063@qq.com * @date 2021年10月28日 * 通过ComponentScan注解扫描注入bean到IOC容器 * 可以指定策列,比如哪些包需要扫描、排除哪些bean被扫描 */@Configuration@ComponentScanpublic class ComponentScanPOJO { @Value("3") private int id; @Value("ComponentScanPOJO") private String name; @Value("29") private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }把装配的ComponentScanPOJO 注入
package com.sboot.pr.controller; import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController; import com.sboot.pr.bean.BeanPOJO;import com.sboot.pr.bean.ComponentPOJO;import com.sboot.pr.bean.ComponentScanPOJO; /** * @author ygb * @Mailbox 941201063@qq.com * @date 2021年10月28日 */@RestControllerpublic class TestController { @Autowired private ComponentScanPOJO componentScanPOJO; /** * 获取通过Component注解装配到IOC容器的对象 * @return */ @GetMapping("/boot/getComponentScanPOJO") public ComponentScanPOJO getComponentScanPOJO() { return componentScanPOJO; } }访问注入的ComponentScanPOJO信息
http://localhost:1111/boot/getComponentScanPOJO

感谢你能够认真阅读完这篇文章,希望小编分享的"Springboot如何注入装配到IOC容器"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!
装配
容器
注解
篇文章
信息
对象
价值
兴趣
同时
名称
字母
小写
文件
更多
朋友
知识
编带
行业
资讯
资讯频道
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
上海睿民互联网科技有限公司
互联网科技与金融科技
互联网科技的英文
互联网科技股有哪些
承德科技产业互联网发展
团飞科技与伯堂互联网服务平台
科技与互联网哪个更好
宁夏众和广鑫互联网科技有限公司
厦门比较好的科技互联网公司
港股科技和互联网
速维有品互联网信息科技(苏州)有限公司
爱医互联网络科技
2021互联网科技公司排名
互联网科技快讯热点
互联网科技融合创新
互联网公司属于科技吗
米泉互联网科技专业怎么样
玉溪新华互联网科技怎么选
互联网的科技体现在哪里
互联网科技企业文化
互联网科技公司简介
菲比寻常互联网科技
互联网科技英文翻译
互联网科技创新企业排名
欧洲十大科技互联网公司
昭通互联网科技选哪家
上海互联网科技公司ppt
互联网科技包装股份有限公司上市
昆明中奇联合互联网科技
第三届互联网大会安防黑科技