详解Springboot 注入装配到IOC容器方式

网友投稿 1134 2022-11-26

详解Springboot 注入装配到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日

* 配置类文件

*/

@Configuration

public 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日

*/

@RestController

public 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) {zHehuGIVdk

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日

*/

@RestController

public 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

@ComponentScan

public 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日

*/

@RestController

public class TestController {

@Autowired

private ComponentScanPOJO componentScanPOJO;

/**

* 获取通过Component注解装配到IOC容器的对象

* @return

*/

@GetMapping("/boot/getComponentScanPOJO")

public ComponentScanPOJO getComponentScanPOJO() {

return componentScanPOJO;

}

}

访问注入的ComponentScanPOJO信息

http://localhost:1111/boot/getComponentScanPOJO

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:使用maven将项目打成jar包
下一篇:小信号谐振器电路仿真实验
相关文章

 发表评论

暂时没有评论,来抢沙发吧~