@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注解)的更多相关文章

  1. Spring Boot 的静态资源处理

    做web开发的时候,我们往往会有很多静态资源,如html.图片.css等.那如何向前端返回静态资源呢?以前做过web开发的同学应该知道,我们以前创建的web工程下面会有一个webapp的目录,我们只要 ...

  2. Spring Boot中静态资源(JS, 图片)等应该放在什么位置

    Spring Boot的静态资源,比如图片应该放在什么位置呢, 如果你放在传统WEB共的类似地方, 比如webapp或者WEB-INF下,你会得到一张示意文件未找到的破碎图片.那应该放哪里呢? 百度一 ...

  3. Spring boot 默认静态资源路径与手动配置访问路径的方法

    这篇文章主要介绍了Spring boot 默认静态资源路径与手动配置访问路径的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下   在application.propertis中配置 ##端口号 ...

  4. Spring Boot 中初始化资源的几种方式(转)

    假设有这么一个需求,要求在项目启动过程中,完成线程池的初始化,加密证书加载等功能,你会怎么做?如果没想好答案,请接着往下看.今天介绍几种在Spring Boot中进行资源初始化的方式,帮助大家解决和回 ...

  5. spring boot 开静态资源访问,配置视图解析器

    配置视图解析器spring.mvc.view.prefix=/pages/spring.mvc.view.suffiix= spring boot 开静态资源访问application.proerti ...

  6. Spring Boot 中初始化资源的几种方式

    假设有这么一个需求,要求在项目启动过程中,完成线程池的初始化,加密证书加载等功能,你会怎么做?如果没想好答案,请接着往下看.今天介绍几种在Spring Boot中进行资源初始化的方式,帮助大家解决和回 ...

  7. spring boot 整合quartz ,job不能注入的问题

    在使用spring boot 整合quartz的时候,新建定时任务类,实现job接口,在使用@AutoWire或者@Resource时,运行时出现nullpointException的问题.显然是相关 ...

  8. Spring Boot 设置静态资源访问

    问题描述 当使用spring Boot来架设服务系统时,有时候也需要用到前端页面,当然就不可或缺地需要访问其他一些静态资源,比如图片.css.js等文件.那么如何设置Spring Boot网站可以访问 ...

  9. Spring boot 工具类静态属性注入及多环境配置

    由于需要访问MongoDB,但是本地开发环境不能直接连接MongoDB,需要通过SecureCRT使用127.0.0.2本地IP代理.但是程序部署到线上生产环境后,是可以直接访问MongoDB的,因此 ...

随机推荐

  1. Oracle的substr函数简单用法与substring区别

    substr(字符串,截取开始位置,截取长度) //返回截取的字 substr('Hello World',0,1) //返回结果为 'H'  *从字符串第一个字符开始截取长度为1的字符串 subst ...

  2. 获取maven下resouce中的文件为File对象

    例如src/main/resources目录下面有个config/interfaces/quick-stock.xml文件我想直接在某一个Java类里面获取到这个文件的File对象,怎么整?(注意噢: ...

  3. MapReduce源码分析之InputFormat

    InputFormat描述了一个Map-Reduce作业中的输入规范.Map-Reduce框架依靠作业的InputFormat实现以下内容: 1.校验作业的输入规范: 2.分割输入文件(可能为多个), ...

  4. erlang的非平衡的二叉树的操作

    -module(tree1). -export([test1/0]). lookup(Key,nil) -> not_found; lookup(Key,{Key,Value,_,_}) -&g ...

  5. selenium 遇到chrome 弹出是否保存密码框

    --待验证(以下是java脚本) ChromeOptions options = new ChromeOptions(); options.addArguments("--start-max ...

  6. hihoCoder#1037 : 数字三角形(DP)

    [题目链接]:click here~~ 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 问题描写叙述 小Hi和小Ho在经历了螃蟹先生的任务之后被奖励了一次出国旅游的机会,于是他 ...

  7. centos7.0 安装nginx

    在centos7.0下安装nginx需要安装 prce和zlib包去官网下载相应的包 然后解压相应的包进行编译 解压nginx源码包进入到解压文件 ./configure --sbin-path=/u ...

  8. 软件测试人员需要精通的开发语言(2)--- SQL

    简单的入门语言vbs,通过实例的练习以及简单的应用,基本可以掌握vbs脚本的使用.如果说vbs只是为了实际测试中开发脚本,便捷测试的目的.那SQL语言就是测试人员的必备技能了,任何系统都会用到数据库, ...

  9. 【python】-- json & pickle、xml、requests、hashlib、shelve、shutil、configparser、subprocess

    json & pickle Python中用于序列化的两个模块 json     用于[字符串]和 [python基本数据类型] 间进行转换 pickle   用于[python特有的类型] ...

  10. 洛谷 P3629 [APIO2010]巡逻

    题目在这里 这是一个紫题,当然很难. 我们往简单的想,不建立新的道路时,从1号节点出发,把整棵树上的每条边遍历至少一次,再回到1号节点,会恰好经过每条边两次,路线总长度为$2(n-1)$,根据树的深度 ...