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(); }
}
spring boot: EL和资源 (一般注入说明(二) @Service注解 @Component注解)的更多相关文章
- Spring Boot 的静态资源处理
做web开发的时候,我们往往会有很多静态资源,如html.图片.css等.那如何向前端返回静态资源呢?以前做过web开发的同学应该知道,我们以前创建的web工程下面会有一个webapp的目录,我们只要 ...
- Spring Boot中静态资源(JS, 图片)等应该放在什么位置
Spring Boot的静态资源,比如图片应该放在什么位置呢, 如果你放在传统WEB共的类似地方, 比如webapp或者WEB-INF下,你会得到一张示意文件未找到的破碎图片.那应该放哪里呢? 百度一 ...
- Spring boot 默认静态资源路径与手动配置访问路径的方法
这篇文章主要介绍了Spring boot 默认静态资源路径与手动配置访问路径的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下 在application.propertis中配置 ##端口号 ...
- Spring Boot 中初始化资源的几种方式(转)
假设有这么一个需求,要求在项目启动过程中,完成线程池的初始化,加密证书加载等功能,你会怎么做?如果没想好答案,请接着往下看.今天介绍几种在Spring Boot中进行资源初始化的方式,帮助大家解决和回 ...
- spring boot 开静态资源访问,配置视图解析器
配置视图解析器spring.mvc.view.prefix=/pages/spring.mvc.view.suffiix= spring boot 开静态资源访问application.proerti ...
- Spring Boot 中初始化资源的几种方式
假设有这么一个需求,要求在项目启动过程中,完成线程池的初始化,加密证书加载等功能,你会怎么做?如果没想好答案,请接着往下看.今天介绍几种在Spring Boot中进行资源初始化的方式,帮助大家解决和回 ...
- spring boot 整合quartz ,job不能注入的问题
在使用spring boot 整合quartz的时候,新建定时任务类,实现job接口,在使用@AutoWire或者@Resource时,运行时出现nullpointException的问题.显然是相关 ...
- Spring Boot 设置静态资源访问
问题描述 当使用spring Boot来架设服务系统时,有时候也需要用到前端页面,当然就不可或缺地需要访问其他一些静态资源,比如图片.css.js等文件.那么如何设置Spring Boot网站可以访问 ...
- Spring boot 工具类静态属性注入及多环境配置
由于需要访问MongoDB,但是本地开发环境不能直接连接MongoDB,需要通过SecureCRT使用127.0.0.2本地IP代理.但是程序部署到线上生产环境后,是可以直接访问MongoDB的,因此 ...
随机推荐
- Swing实现右下角消息框
package com.ui; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; impo ...
- PHP 使用 GeoLiteCity 库解析 IP 为地理位置
关于把 IP 地址转换为地理位置可以使用网络上很多的 API,好处就是不用在本地存储一个 IP 数据库,而且一般网络上的 IP 库会自动更新,不利的地方就是太依赖于网络,性能表现也可能会弱些.比如像下 ...
- 【JavaEE】Springmvc搭建方法及example
现在介绍SSH的文章很多,但是适合自己需求的却经常找不到,这些东西呢,会了之后总会感觉别人的程序哪里哪里别扭,会之前呢就感觉很混乱,而且SSH的官方文档,至少在我看来是“会者勉强能看.不会者一片迷茫” ...
- lua(简单的传参)
#include <iostream> #include <string.h> extern "C" { /*头文件lua.h定义了Lua提供的基础函数,包 ...
- Leftmost Digit(hdu1060)(数学题)
Leftmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- OLTP和OLAP
1 OLTP和OLAP online transaction processing,联机事务处理.业务类系统主要供基层人员使用,进行一线业务操作,通常被称为联机事务处理. online analyti ...
- xCode9 一直Indexing
解决办法:打开终端输入:defaults write com.apple.dt.XCode IDEIndexDisable 1 然后重新打开工程就可以了
- A vectorized example
http://cs231n.stanford.edu/slides/2017/cs231n_2017_lecture4.pdf
- zabbix server 端安装
1.系统环境 [root@crazy-acong ~]# cat /etc/redhat-release CentOS release 6.6 (Final) [root@crazy-acong ~] ...
- can not find java.util.map java.lang.Double问题
mybatis @Param注解和ParamType属性不能共存