一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 詳解Springboot 注入裝配到IOC容器方式

詳解Springboot 注入裝配到IOC容器方式

2022-03-07 13:00科技發燒愛好者-羊工 Java教程

今天通過實例代碼給大家介紹了Springboot 注入裝配到IOC容器方式,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,感興趣的朋友跟隨小編一起看看吧

1、通過bean注解裝配到IOC容器

創建裝配的類,如下

package com.sboot.pr.bean;
/**
* @author ygb
* @Mailbox [email protected]
* @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 [email protected]
* @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 [email protected]
* @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

詳解Springboot 注入裝配到IOC容器方式

 

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 [email protected]
* @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 [email protected]
* @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

詳解Springboot 注入裝配到IOC容器方式

 

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 [email protected]
* @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 [email protected]
* @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容器方式的文章就介紹到這了,更多相關Springboot 注入IOC容器內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://blog.csdn.net/a13826548428/article/details/121049589

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 91麻豆精品国产自产在线 | 四虎最新免费观看网址 | 国产在线综合网 | 亚洲社区在线 | 日韩精品欧美高清区 | 九九国产在线观看 | 精品国产一二三区在线影院 | 小货SAO边洗澡边CAO你动漫 | 日本视频在线观看播放 | 我年轻漂亮的继坶2中字在线播放 | 动漫美女被吸乳羞羞小说 | 欧美精品三区 | 精品AV亚洲乱码一区二区 | 国产日本欧美亚洲精品视 | 国产极品美女在线 | 四虎永久在线精品波多野结衣 | 亚洲va在线va天堂成人 | 91精品手机国产在线观 | 亚欧成人中文字幕一区 | 调教女帝 | 91视频99 | 91制片厂制作传媒破解版免费 | 亚洲+国产+图片 | 国产99视频精品免费视频7 | 国产情侣偷国语对白 | 国产成人刺激视频在线观看 | 亚洲色图中文字幕 | 国产成人精品午夜在线播放 | 亚洲美日韩 | 免费永久观看美女视频网站网址 | 欧美色图日韩色图 | 出轨娇妻的呻吟1—9 | 精品国产免费观看一区高清 | 村上里沙40分钟在线观看 | 男男视频18免费网站 | 亚洲国产成人精品无码区5566 | 国产精品 视频一区 二区三区 | 国产九九热视频 | 国产123区在线视频观看 | 久久久精品免费免费直播 | 久久婷婷五月免费综合色啪 |