Spring4.X之基于Java注解的配置(与SpringBoot的诞生)

https://www.iteye.com/blog/lixh1986-2398464

最近项目用到了SpringBoot,对其没有xml配置就可以运行感到非常神奇。之前没有细细追究。 

说到 SpringBoot,需要提一下: 

一、Spring的历史版本与配置方式 

1. Spring 1.x 
完全是 xml 的配置。 
缺点:随着项目的扩大,xml越来越多,而且需要对xml分类。 

2. Spring 2.x 
在 JDK 1.5 更新的时代,可以使用注解。Spring 2.X 开始使用注解。 

那么问题是:使用xml?还是使用注解? 
得出最佳实践: 
应用的基本配置用xml,业务用注解。 

3. Spring 3.x 
推荐使用 java 代码的配置注解,取代 xml 

@Configuration, @Bean 

4. Spring 4.x 

SpringBoot 诞生 



二、Spring 4.x @Component 与 @Bean 的区别 

https://stackoverflow.com/questions/10604298/spring-component-versus-bean 

@Component 是加在类上; 
@Bean 是加在方法上;而且可以手动指定生成哪个类的具体实例。 


三、Spring 4.x @Resource 与 @Autowired 的区别 

https://stackoverflow.com/questions/4093504/resource-vs-autowired 

@Resource是 By Name 来匹配的,没有找到对应的名字时则调用 @Autowired。 
名字可以用 @Resource(name="blah")属性指定,缺省则使用变量名。 

@Autowired是 By Type 来匹配的。常与 @Autowired @Qualifier("blah") 结合使用。 

依赖注入的第三种方式: 

Java代码 

 收藏代码

  1. @Qualifier  
  2. @Retention(RUNTIME)  
  3. public @interface YourQualifier {}  

 

Java代码 

 收藏代码

  1. @YourQualifier  
  2. @Component  
  3. public class SomeBean implements Foo { .. }  


And then 

Java代码 

 收藏代码

  1. @Inject @YourQualifier private Foo foo;  




@RESOURCE VS @AUTOWIRED VS @INJECT 
 

ANNOTATIONPACKAGESOURCE
@Resourcejavax.annotationJava
@Injectjavax.injectJava
@Qualifierjavax.injectJava
@Autowiredorg.springframework.bean.factorySpring



 

Java代码 

 收藏代码

  1. @Resource  
  2. @Qualifier("person")  
  3. private Party party;  
  4.   
  5. @Autowired  
  6. @Qualifier("person")  
  7. private Party party;  
  8.   
  9. @Inject  
  10. @Qualifier("person")  
  11. private Party party;  



https://blogs.sourceallies.com/2011/08/spring-injection-with-resource-and-autowired/ 



四、Spring 4.x @PropertySources 与 @PropertySource 

https://www.mkyong.com/spring/spring-propertysources-example/ 

Some enhancements on Spring 4. 

- Introduces new @PropertySources to support Java 8 and better way to include multiple properties files. 

Java代码 

 收藏代码

  1. @Configuration  
  2. @PropertySources({  
  3.     @PropertySource("classpath:config.properties"),  
  4.     @PropertySource("classpath:db.properties")  
  5. })  
  6. public class AppConfig {  
  7.     //...  
  8. }  


- Allow @PropertySource to ignore the not found properties file. 

Java代码 

 收藏代码

  1. @Configuration  
  2. @PropertySource("classpath:missing.properties")  
  3. public class AppConfig {  
  4.     //...  
  5. }  


If missing.properties is not found, the system is unable to start and throws FileNotFoundException 

Caused by: java.io.FileNotFoundException: 
class path resource [missiong.properties] cannot be opened because it does not exist 

In Spring 4, you can use ignoreResourceNotFound to ignore the not found properties file 
 

Java代码 

 收藏代码

  1.     @Configuration  
  2.     @PropertySource(value="classpath:missing.properties", ignoreResourceNotFound=true)  
  3.     public class AppConfig {  
  4.         //...  
  5.     }  
  6.   
  7. @PropertySources({  
  8.     @PropertySource(value = "classpath:missing.properties", ignoreResourceNotFound=true),  
  9.     @PropertySource("classpath:config.properties")  
  10. })  


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hello_world!

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值