详解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容器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Maven仓库无用文件和文件夹清理的方法实现

    Maven仓库无用文件和文件夹清理的方法实现

    这篇文章主要介绍了Maven仓库无用文件和文件夹清理的方法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • Java Applet查找素数小程序代码实例

    Java Applet查找素数小程序代码实例

    这篇文章主要介绍了Java Applet查找素数小程序代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-02-02
  • 基于java涉及父子类的异常详解

    基于java涉及父子类的异常详解

    下面小编就为大家带来一篇基于java涉及父子类的异常详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-09-09
  • java 代码中预防空指针异常的处理办法

    java 代码中预防空指针异常的处理办法

    个人在做项目时,对NullPointerException的几点总结,请网友拍砖!!!多多提意见,
    2013-03-03
  • JDK12的新特性之CompactNumberFormat详解

    JDK12的新特性之CompactNumberFormat详解

    这篇文章主要介绍了JDK12的新特性之CompactNumberFormat,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-05-05
  • 详谈锁和监视器之间的区别_Java并发

    详谈锁和监视器之间的区别_Java并发

    下面小编就为大家带来一篇详谈锁和监视器之间的区别_Java并发。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • SpringMVC DispatcherServlet组件实现解析

    SpringMVC DispatcherServlet组件实现解析

    这篇文章主要介绍了SpringMVC DispatcherServlet组件实现解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • 分享5个Java接口性能提升的通用技巧

    分享5个Java接口性能提升的通用技巧

    作为后端开发人员,我们总是在编写各种API。这些API在服务初期可能表现不错,但随着用户数量的增长,一开始响应很快的API越来越慢,这时候你就需要考虑如何优化你的API性能了。在这篇文章中,我总结了一些行之有效的API性能优化技巧,希望能给有需要的朋友一些帮助
    2023-01-01
  • Java Web项目中使用Socket通信多线程、长连接的方法

    Java Web项目中使用Socket通信多线程、长连接的方法

    很多时候在javaweb项目中我们需要用到Socket通信来实现功能,在web中使用Socket我们需要建立一个监听程序,在程序启动时,启动socket监听。接下来通过本文给大家介绍Java Web项目中使用Socket通信多线程、长连接的方法,感兴趣的朋友一起学习
    2016-04-04
  • Javafx实现国际象棋游戏

    Javafx实现国际象棋游戏

    这篇文章主要为大家详细介绍了Javafx实现国际象棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-05-05

最新评论