spring boot: EL和資源 (一般注入說明(二) @Service注解 @Component注解)


@Service用於標注業務層組件 : 將當前類注冊為spring的Bean

@Controller用於標注控制層組件(如struts中的action)

@Repository用於標注數據訪問組件,即DAO組件

@Component泛指組件,當組件不好歸類的時候,我們可以使用這個注解進行標注。: 將當前類注冊為spring的Bean

 

 

實例:

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

//注入:當前類是spring管理的一個bean
//等效(可根據需要選擇):@Service=@Component=@Repository=@Controller
@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的功能
	//等效注解: @Autowire=@Inject=@Resource
	
	//注入文字
	@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();
		
	}
}

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM