@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. codeforces 427 div.2 F. Roads in the Kingdom

    F. Roads in the Kingdom time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  2. 微信支付v3开发(6) 收货地址共享接口

    请看新版教程  微信支付开发(7) 收货地址共享接口V2 本文介绍微信支付下的收货地址共享接口的开发过程. 一. 简单介绍 微信收货地址共享,是指用户在微信浏览器内打开网页,填写过地址后,兴许能够免填 ...

  3. LNMP环境搭建之php安装,wordpress博客搭建

    LNMP环境搭建之php安装,wordpress博客搭建 一.介绍: 1.什么是CGI CGI全称是"通用网关接口"(Common Gateway Interface),HTTP服 ...

  4. Unity合并选中物体的Mesh

    using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; pu ...

  5. PHPstorm如何安装vue.js插件

    1.什么是PHPstorm? PhpStorm是一个轻量级且便捷的PHP IDE,其旨在提高用户效率,可深刻理解用户的编码,提供智能代码补全,快速导航以及即时错误检查.----来自百度百科 一句话:P ...

  6. (转)linux设备驱动之USB数据传输分析 一

    三:传输过程的实现说到传输过程,我们必须要从URB开始说起,这个结构的就好比是网络子系统中的skb,好比是I/O中的bio.USB系统的信息传输就是打成URB结构,然后再过行传送的.URB的全称叫US ...

  7. python接口自动化(四十二)- 项目结构设计之大结局(超详解)

    简介 这一篇主要是将前边的所有知识做一个整合,把各种各样的砖块---模块(post请求,get请求,logging,参数关联,接口封装等等)垒起来,搭建一个房子.并且有很多小伙伴对于接口项目测试的框架 ...

  8. Windows 下Node.js开发环境配置

    第一步:安装VirtualBox(以管理员身份安装) 1.安装完成后,打开VirtualBox,点击“新建”按钮,输入信息,“下一步”(名称可任意) 2.设置内存为1024MB,“下一步”—>“ ...

  9. G - 湫湫系列故事——减肥记I

    G - 湫湫系列故事——减肥记I Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u De ...

  10. 让Xcode支持高版本系统设备真机测试

    最新支持11.2 (15C107) Xcode只可以支持iPhone手机对应iOS系统以下的真机测试.一般想要支持最新的iPhone手机系统,有两个方法. 第一.就需要更新Xcode,这一个方法有一个 ...