spring 注解
@Value("${key}")
此类别指的是作用于bean的属性,如是否延迟加载,Scope等,
@Qualifier 常和@autowired
@Lazy 延迟加载
@Scope 定义bean的Scope
@Required 可以标注在Set方法上,说明此方法需要被执行,否则跑出异常,长和@autowired 配合使用
@PostConstruct and@PreDestroy 来自于JSR,作用于生命周期
依赖级别
Spring可以使用这些注解进行依赖注入,通常是自动的,或者借助一些辅助信息。
@autowired自动依赖注入,有几种方式,byType,byName等,通常和@ Qualifier配合使用
@Inject 来自JSR-330,和以上类似,JSR-330相关注解
@Resource和以上类似,JSR-250相关注解
@Value 原始类型注入
@Autowired
通过类型自动注入(byType)
加入以下配置
@Resource是JSR-250的注解,其含义和@Autowired 相同,但是有限制,要使其生效,需要在bean定义文件中声明相应的beanpostProcessor
@Qualifier
byName
使用bean的name来作为@Qualifier的值来限制注入指定的bean
@value
用来自动装配基本类型(如int 等),可以和springEL 配合使用,也可以注入properties属性文件和常量值。
/**
* 注入常量
*/
@Value(" i am const")
在xml文件中加入对properties文件的读取
<context:property-placeholder location="classpath:/com/test/testvalue.properties"/>
# spring el
# 配置文件 自定义属性,可以在Controller中读取
application.hello=Hello Shanhy
application.age=21,22,23
@Value("#{'${application.age}'.split(',')}")
private List<Integer> age;
// To set a default value for property placeholder
${property:default value}
@Value("${mongodb.url:127.0.0.1}")
private String mongodbUrl;
@Value("#{config['mongodb.url']?:'127.0.0.1'}")
private String mongodbUrl;
// To set a default value in Spring expression
#{expression?:default value}
@Value("#{config['mongodb.url'] ?: '127.0.0.1'}")
private String mongodbUrl;
Componet
https://blog.csdn.net/windsunmoon/article/details/44498169
<context:component-scan base-package="com.test"/>
<context:component-scan base-package="org.example">
<context:include-filter type="regex"
expression=".*Stub.*Repository"/>
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
https://blog.csdn.net/windsunmoon/article/details/44476249
https://blog.csdn.net/windsunmoon/article/category/2848657
https://blog.csdn.net/windsunmoon/article/details/44360659