Get the App
SLTechnology News&Howtos  ›  Development  › 

How to inject Springboot into IOC Container

Shulou Source: shulou.com Published: 2022-06-02 20:14:42 10月02日 Update

This article mainly introduces how to inject Springboot into the IOC container, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.

1. Assemble to the IOC container through bean annotations

Create the assembled class, as follows

Package com.sboot.pr.bean;/** * @ author ygb * @ Mailbox 941201063@qq.com * @ date October 28, 2021 * assembled to IOC container * / public class BeanPOJO {private int id; private String name; private int age; public int getId () {return id via bean annotations } 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;}}

Assemble BeanPOJO to IOC container via bean annotations

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 October 28, 2021 * configuration class file * / @ Configurationpublic class BeanConfig {/ * assemble BeanPOJO to the IOC container * @ return * / @ Bean (name = "beanPOJO") public BeanPOJO initBeanPOJO () {BeanPOJO pojo = new BeanPOJO () via bean annotations Pojo.setId (1); pojo.setName ("BeanPOJO"); pojo.setAge (29); return pojo;}}

Inject assembled BeanPOJO into

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 October 28, 2021 * / @ RestControllerpublic class TestController {@ Autowired private BeanPOJO beanPoJO / * get the objects assembled to the IOC container through Bean annotations * @ return * / @ GetMapping ("/ boot/getBeanPOJO") public BeanPOJO getBeanPOJO () {return beanPoJO;}}

Access injected BeanPOJO information

Http://localhost:1111/boot/getBeanPOJO

2. Scan the assembly bean to the IOC container through Component annotations

Create the assembled class, as follows

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 October 28, 2021 * injecting bean into the IOC container through Component annotation scan * specify the bean name, with the default initials lowercase * / @ 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;}}

Inject assembled ComponentPOJO into

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 October 28, 2021 * / @ RestControllerpublic class TestController {@ Autowired private ComponentPOJO componentPOJO / * get the objects assembled to the IOC container through Component annotations * @ return * / @ GetMapping ("/ boot/getComponentPOJO") public ComponentPOJO getComponentPOJO () {return componentPOJO;}}

Access injected ComponentPOJO information

Http://localhost:1111/boot/getComponentPOJO

3. Scan the assembly bean to the IOC container through ComponentScan annotations

Create the assembled class, as follows

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 October 28th, 2021 * injecting bean into the IOC container through ComponentScan annotation scanning * you can specify policy columns, such as which packets need to be scanned and which bean are excluded * / @ 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;}}

Inject assembled ComponentScanPOJO into

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 October 28, 2021 * / @ RestControllerpublic class TestController {@ Autowired private ComponentScanPOJO componentScanPOJO / * get the objects assembled to the IOC container through Component annotations * @ return * / @ GetMapping ("/ boot/getComponentScanPOJO") public ComponentScanPOJO getComponentScanPOJO () {return componentScanPOJO;}}

Access injected ComponentScanPOJO information

Http://localhost:1111/boot/getComponentScanPOJO

Thank you for reading this article carefully. I hope the article "how to inject Springboot into the IOC container" shared by the editor will be helpful to everyone. At the same time, I also hope you can support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Assemblies containers annotations articles information objects values interests at the same time names letters lowercase documents more friends knowledge ribbons industry information information channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information MariaDB MySQL Docker vpn