欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-16 21:30:38
1、通过bean注解装配到ioc容器创建装配的类,如下package com.sboot.pr.bean;/** * @author ygb * @mailbox 941201063@qq.com *...

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

详解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 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

详解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 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容器方式的文章就介绍到这了,更多相关springboot 注入ioc容器内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!