欧美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

 

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

相關文章

  • File.createTempFile創(chuàng)建臨時文件的示例詳解

    File.createTempFile創(chuàng)建臨時文件的示例詳解

    這篇文章主要介紹了File.createTempFile創(chuàng)建臨時文件的示例詳解,在默認臨時文件目錄中創(chuàng)建一個空文件,使用給定前綴和后綴生成其名稱。 如果感興趣來了解一下
    2020-07-07
  • maven父子工程中的依賴引用的實現(xiàn)

    maven父子工程中的依賴引用的實現(xiàn)

    本文主要介紹了Maven父子工程中的依賴引用的實現(xiàn),包括<dependencyManagement>和<dependencies>兩個標簽的使用,以及父子pom文件的配置,感興趣的可以了解一下
    2024-10-10
  • windows7配置java環(huán)境變量的圖文教程

    windows7配置java環(huán)境變量的圖文教程

    這篇文章主要介紹了windows7配置java環(huán)境變量的教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02
  • 最新評論