spring boot: EL和资源 (一般注入说明(二) @Service注解 @Component注解)
@Service用于标注业务层组件 : 将当前类注册为spring的Bean
@Controller用于标注控制层组件(如struts中的action)
@Repository用于标注数据访问组件,即DAO组件
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。: 将当前类注册为spring的Bean
实例:
1 | DemoService :文件: |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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文件:
1 | wwwweeebbfddfd |
test.propeties文件:
1 2 | book.author=wangyunfei book.name=spring boot |
ResourceConfig文件:
1 2 3 4 5 6 7 8 9 10 11 | 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文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | 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文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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(); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2016-12-26 java:StringBuffer字符处理对象
2016-12-26 小程序 web 端实时运行工具