欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2021年10月31日 11:09:21   作者:科技發(fā)燒愛好者-羊工  
今天通過實例代碼給大家介紹了Springboot 注入裝配到IOC容器方式,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,感興趣的朋友跟隨小編一起看看吧

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

     創(chuàng)建裝配的類,如下

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容器

創(chuàng)建裝配的類,如下

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容器

創(chuàng)建裝配的類,如下

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

 

到此這篇關(guān)于Springboot 注入裝配到IOC容器方式的文章就介紹到這了,更多相關(guān)Springboot 注入IOC容器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 使用Java將字節(jié)數(shù)組轉(zhuǎn)成16進制形式的代碼實現(xiàn)

    使用Java將字節(jié)數(shù)組轉(zhuǎn)成16進制形式的代碼實現(xiàn)

    在很多場景下,需要進行分析字節(jié)數(shù)據(jù),但是我們存起來的字節(jié)數(shù)據(jù)一般都是二進制的,這時候就需要我們將其轉(zhuǎn)成16進制的方式方便分析,本文主要介紹如何使用Java將字節(jié)數(shù)組格式化成16進制的格式并輸出,需要的朋友可以參考下
    2024-05-05
  • Java使用正則表達式判斷字符串是否以字符開始

    Java使用正則表達式判斷字符串是否以字符開始

    這篇文章主要介紹了Java使用正則表達式判斷字符串是否以字符開始的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • SpringBoot項目如何將Bean注入到普通類中

    SpringBoot項目如何將Bean注入到普通類中

    這篇文章主要介紹了SpringBoot項目如何將Bean注入到普通類中,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • SpringBoot+Nacos+Kafka微服務流編排的簡單實現(xiàn)

    SpringBoot+Nacos+Kafka微服務流編排的簡單實現(xiàn)

    本文主要介紹了SpringBoot+Nacos+Kafka微服務流編排的簡單實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • SpringMVC中RequestMapping注解(作用、出現(xiàn)的位置、屬性)

    SpringMVC中RequestMapping注解(作用、出現(xiàn)的位置、屬性)

    這篇文章主要介紹了SpringMVC中RequestMapping注解(作用、出現(xiàn)的位置、屬性),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • Intellij?IDEA如何修改配置文件位置

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

    這篇文章主要介紹了Intellij?IDEA--修改配置文件位置,文章末尾給大家介紹了Intellij?IDEA--宏的用法記錄操作過程,對此文感興趣的朋友跟隨小編一起看看吧
    2022-08-08
  • java使用xpath解析xml示例分享

    java使用xpath解析xml示例分享

    XPath基于XML的樹狀結(jié)構(gòu),提供在數(shù)據(jù)結(jié)構(gòu)樹中找尋節(jié)點的能力,下面是一小示例,需要的朋友可以參考下
    2014-03-03
  • Java super關(guān)鍵字的使用方法詳解

    Java super關(guān)鍵字的使用方法詳解

    這篇文章主要介紹了Java super關(guān)鍵字的使用方法詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家對super關(guān)鍵字徹底掌握,需要的朋友可以參考下
    2017-10-10
  • Java字符串常量池和intern方法解析

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

    本文主要介紹了Java字符串常量池和intern方法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-05-05
  • 一文盤點Java創(chuàng)建實例對象的方式

    一文盤點Java創(chuàng)建實例對象的方式

    Java對象是通過加載、鏈接、初始化三大步驟來完成對象的創(chuàng)建及初始化,那么接下來就說一下Java創(chuàng)建實例對象的方式有哪幾種,文中并通過代碼示例講解的非常詳細,需要的朋友可以參考下
    2025-02-02

最新評論