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

 更新时间:2021年10月31日 11:09:21   作者:科技发烧爱好者-羊工  
今天通过实例代码给大家介绍了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) {
		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

 

到此这篇关于Springboot 注入装配到IOC容器方式的文章就介绍到这了,更多相关Springboot 注入IOC容器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 使用Java将字节数组转成16进制形式的代码实现

    使用Java将字节数组转成16进制形式的代码实现

    在很多场景下,需要进行分析字节数据,但是我们存起来的字节数据一般都是二进制的,这时候就需要我们将其转成16进制的方式方便分析,本文主要介绍如何使用Java将字节数组格式化成16进制的格式并输出,需要的朋友可以参考下
    2024-05-05
  • Java使用正则表达式判断字符串是否以字符开始

    Java使用正则表达式判断字符串是否以字符开始

    这篇文章主要介绍了Java使用正则表达式判断字符串是否以字符开始的相关资料,需要的朋友可以参考下
    2017-06-06
  • SpringBoot项目如何将Bean注入到普通类中

    SpringBoot项目如何将Bean注入到普通类中

    这篇文章主要介绍了SpringBoot项目如何将Bean注入到普通类中,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • SpringBoot+Nacos+Kafka微服务流编排的简单实现

    SpringBoot+Nacos+Kafka微服务流编排的简单实现

    本文主要介绍了SpringBoot+Nacos+Kafka微服务流编排的简单实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • SpringMVC中RequestMapping注解(作用、出现的位置、属性)

    SpringMVC中RequestMapping注解(作用、出现的位置、属性)

    这篇文章主要介绍了SpringMVC中RequestMapping注解(作用、出现的位置、属性),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-01-01
  • Intellij IDEA如何修改配置文件位置

    Intellij IDEA如何修改配置文件位置

    这篇文章主要介绍了Intellij IDEA--修改配置文件位置,文章末尾给大家介绍了Intellij IDEA--宏的用法记录操作过程,对此文感兴趣的朋友跟随小编一起看看吧
    2022-08-08
  • java使用xpath解析xml示例分享

    java使用xpath解析xml示例分享

    XPath基于XML的树状结构,提供在数据结构树中找寻节点的能力,下面是一小示例,需要的朋友可以参考下
    2014-03-03
  • Java super关键字的使用方法详解

    Java super关键字的使用方法详解

    这篇文章主要介绍了Java super关键字的使用方法详解的相关资料,希望通过本文能帮助到大家,让大家对super关键字彻底掌握,需要的朋友可以参考下
    2017-10-10
  • Java字符串常量池和intern方法解析

    Java字符串常量池和intern方法解析

    本文主要介绍了Java字符串常量池和intern方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-05-05
  • 一文盘点Java创建实例对象的方式

    一文盘点Java创建实例对象的方式

    Java对象是通过加载、链接、初始化三大步骤来完成对象的创建及初始化,那么接下来就说一下Java创建实例对象的方式有哪几种,文中并通过代码示例讲解的非常详细,需要的朋友可以参考下
    2025-02-02

最新评论