广告
返回顶部
首页 > 资讯 > 后端开发 > Python >详解Springboot 注入装配到IOC容器方式
  • 252
分享到

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

2024-04-02 19:04:59 252人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

1、通过bean注解装配到ioc容器      创建装配的类,如下 package com.sboot.pr.bean; p

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;
 
 

@Configuration
public 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;
 
 

@RestController
public class TestController {
	
	@Autowired
	private 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;
 
 

@RestController
public class TestController {
	
	
	@Autowired
	private 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
@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;
 
 

@RestController
public class TestController {
	
 
	@Autowired
	private ComponentScanPOJO componentScanPOJO;
 
	
	@GetMapping("/boot/getComponentScanPOJO")
	public ComponentScanPOJO getComponentScanPOJO() {
		return componentScanPOJO;
	}
	
	
}

 访问注入的ComponentScanPOJO信息

http://localhost:1111/boot/getComponentScanPOJO

 

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

--结束END--

本文标题: 详解Springboot 注入装配到IOC容器方式

本文链接: https://www.lsjlt.com/news/155984.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作