spring boot: EL和资源 (一般注入说明(二) @Service注解 @Component注解)
2023-09-14 08:57:51 时间
@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学习笔记(七)——SpringMVC常用的注解
- Spring+Quartz实现定时任务的配置方法[通俗易懂]
- Spring boot Mybatis 整合(注解版)
- 谷粒商城-Spring Sleuth服务链路追踪
- SSO 基于token vue + element ui spring boot前后端分离
- 一起学习Spring boot 2.1.X | 第五篇:Mybatis Druid 数据库(注解版)「建议收藏」
- Spring Boot配置WebMvc
- Spring Cloud:第一章:基本介绍
- Spring Batch 可以在一个 Step 中有多个 Tasklet 吗
- 一篇文章带你掌握主流基础框架——Spring
- Spring bean注入之注解注入-- @Autowired原理
- Spring Boot集成websocket
- Spring Boot使用WebSocket模拟聊天
- Spring Boot中如何优雅地表述XML资源
- Spring:声明式事务
- Spring Boot 3 升级 踩坑笔记
- 什么是Spring Boot
- Spring Boot的核心注解和配置(二)
- Spring Boot的加密和解密(二)
- Spring Boot的性能优化(二)
- 集成Spring Cloud Sleuth和Zipkin
- spring boot开发 静态资源加载不出来详解编程语言
- Spring Boot(二十):使用spring-boot-admin对spring-boot服务进行监控详解编程语言
- Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例详解编程语言
- Spring Boot(十四):spring boot整合shiro-登录认证和权限管理详解编程语言
- Spring Boot(十三):spring boot小技巧详解编程语言
- Spring Boot(五):spring data jpa的使用详解编程语言
- Spring boot+vue前后端开发学习笔记2详解编程语言
- spring boot 配置随机数那些小技详解编程语言
- 关于java idea 创建spring boot没有下载依赖的解决方案详解编程语言
- Spring MVC的国际化