spring boot: 一般注入说明

Posted 穆晟铭

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot: 一般注入说明相关的知识,希望对你有一定的参考价值。

实例:

DemoService :文件:
package ch2.el;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

//注入:当前类是spring管理的一个bean
//等效(可根据需要选择):@[email protected][email protected][email protected]
@Service
public class DemoService {

	//注入普通字符串
	@Value("其他类的属性")
	private String another;
	
	public String getAnother()
	{
		return another;
	}
	
	public void setAnother(String another)
	{
		this.another = another;
	}
}

  

 

test.txt文件:

wwwweeebbfddfd

  

test.propeties文件:

book.author=wangyunfei
book.name=spring boot

  

ResourceConfig文件:

package ch2.el;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

//声明当前类是一个配置类
@Configuration
//自动扫描ch2.el包下的所有@Service,@Component,@Repository和@Controller注册为Bean;
@ComponentScan("ch2.el")
public class ResourceConfig {

}

  

Eiconfig文件:

package ch2.el;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;


//声明当前类是一个配置类
@Configuration
//自动扫描包下的所有@Service,@Component,@Repository和@Controller注册为Bean;
@ComponentScan("ch2.el")
//注入配置文件
@PropertySource("classpath:ch2/el/test.propeties")
public class ElConfig {

	
	//将FunctionService类的实体Bean注入到UseFunctionService中,让UseFunctionService拥有FunctionService的功能
	//等效注解: @[email protected][email protected]
	
	//注入文字
	@Value("I love you")
	private String normal;
	
	//注入操作系统属性
	@Value("#{systemProperties[‘os.name‘]}")
	private String osName;
	
	
	//注入表达式结果
	@Value("#{ T(java.lang.Math).random() * 100.0 }")
	private double randomNumber;
	
	
	//注入其他Bean属性
	@Value("#{demoService.another}")
	private String fromAnother;
	
	//注入文件资源
	@Value("classpath:ch2/el/test.txt")
	private Resource testFile;
	
	//注入网址资源
	@Value("http://www.baidu.com")
	private Resource testUrl;
	
	
	//注入配置文件
	@Value("${book.name}")
	private String bookName;
	
	//注入配置文件
	@Autowired
	private Environment environment;
	
	//注入配置文件
	@Bean
	public static PropertySourcesPlaceholderConfigurer propertyConfigure()
	{
		return new PropertySourcesPlaceholderConfigurer();
	}
	
	public void outputResource()
	{
		
		
		
		try {
			System.out.println(normal);
			System.out.println(osName);
			System.out.println(randomNumber);
			System.out.println(fromAnother);
			System.out.println(testFile);
			System.out.println(testUrl);			
			
			System.out.println(IOUtils.toString(testUrl.getInputStream()));
			System.out.println(bookName);
			System.out.println(environment.getProperty("book.author"));
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	
	
	
	
	
	
	
	
}

  

Main文件:

package ch2.el;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {

	
	public static void main(String[] args)
	{
		
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ResourceConfig.class);
		ElConfig resourceElconfig = context.getBean(ElConfig.class);
		
		resourceElconfig.outputResource();
		context.close();
		
	}
}

  

 

以上是关于spring boot: 一般注入说明的主要内容,如果未能解决你的问题,请参考以下文章

spring boot: 一般注入说明 @Component, application event事件为Bean与Bean之间通信提供了支持

Spring Boot使用ServletFilter或Listener的方式

Spring Boot集成Quartz注入Spring管理的类

外置 tomcat启动Spring Boot程序模式下解决过滤器注入bean的空指针问题

Spring Boot 工程中Bean的依赖注入分析

Kotlin Spring Boot 单元测试 - 添加 @TestExecutionListeners 不会注入依赖项