spring boot: EL和资源 (一般注入说明(二) @Service注解 @Component注解)
2024-08-25 22:56:57
@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(); }
}
最新文章
- OOP过度抽象
- hession
- 界面显示两个ListView
- 让你忘记 Flash 的15款精彩 HTML5 游戏
- .Net操作音频
- Google Guava学习笔记——简介
- HDU4010 Query on The Trees(LCT)
- SpringMVC集成shrio框架
- A + B Problem II(大数加法)
- 题解:[GXOI/GZOI2019]与或和
- oracle 分析函数和开窗函数
- poj1416
- Hadoop 故障整理
- python两个列表合并为字典,一个作为key,一个作为value
- Dom4j工具类源码解析
- 修改QGIS来支持DPI为96的WMTS/WMS服务
- idea补丁破解
- poj 1088 (dfs+记忆化) 滑雪
- svn 设置 excel 比对工具为 SPREADSHEETCOMPARE.EXE
- 关于spring session redis共享session的跨子域的处理
热门文章
- linux下tomcat6无法显示图片验证码 少了图形插件
- Maven学习----dependencies与dependencyManagement的区别(转)
- 最小生成树——Prim(普利姆)算法
- git入门三(远程、标签)
- vue构建完整项目-以及实现
- 自定义验证----required属性
- PHP-Manual的学习----【语言参考】----【类型】-----【NULL】
- Linux nginx部署laravel
- 大海教你学手游2015CocosLua第一季_02场景跳转和用户触摸
- Android 适配(drawable文件夹)图片适配(二)