Springboot如何注入装配到IOC容器
短信预约 -IT技能 免费直播动态提醒
这篇文章主要介绍了Springboot如何注入装配到IOC容器,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
1、通过bean注解装配到IOC容器
创建装配的类,如下
package com.sboot.pr.bean;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; @Configurationpublic class BeanConfig { @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; @RestControllerpublic class TestController {@Autowiredprivate BeanPOJO beanPoJO; @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; @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; @RestControllerpublic class TestController {@Autowiredprivate ComponentPOJO componentPOJO; @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; @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; @RestControllerpublic class TestController { @Autowiredprivate ComponentScanPOJO componentScanPOJO; @GetMapping("/boot/getComponentScanPOJO")public ComponentScanPOJO getComponentScanPOJO() {return componentScanPOJO;}}
访问注入的ComponentScanPOJO信息
http://localhost:1111/boot/getComponentScanPOJO
感谢你能够认真阅读完这篇文章,希望小编分享的“Springboot如何注入装配到IOC容器”这篇文章对大家有帮助,同时也希望大家多多支持编程网,关注编程网行业资讯频道,更多相关知识等着你来学习!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341