Spring3註解零配置

咱們在之前學習 Spring 的時候,其全部的配置信息都寫在 applicationContext.xml 裏,大體示例以下:html


java代碼:java

查看複製到剪貼板打印spring

  1. <beans>  
  2. <bean name="ds" class="org.apache.commons.dbcp.BasicDataSource">  
  3. <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>  
  4. <property name="url" value="jdbc:oracle:thin:@localhost :1521:wangbin"/>  
  5. <property name="username" value="tech37"/>  
  6. <property name="password" value="tech37"/>  
  7. </bean>  
  8.    
  9. <bean name="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  10. <property name="dataSource" ref="ds"/>  
  11. </bean>  
  12. <tx:advice id="txAdvice" transaction-manager="txManager">  
  13. <tx:attributes>  
  14. <tx:method name="get*" read-only="true"/>  
  15. <tx:method name="*"/>  
  16. </tx:attributes>  
  17. </tx:advice>  
  18.    
  19. <aop:config>  
  20. <aop:advisor advice-ref="txAdvice"  
  21. pointcut="execution(* cn.javass..business.ebo.*Ebo.*(..))"/>  
  22. </aop:config>  
  23. </beans>  


 

在上面的示例中,咱們能夠典型的看到Spring的三種功能:apache

一、IoC容器,如:oracle

<bean …>app

<property…/>學習

</bean>ui

二、AOPurl

<aop:config/>spa

三、事務

<tx:advice/>

首先咱們學習如何使用註解來構造IoC容器。

用註解來向Spring容器註冊Bean。須要在applicationContext.xml中註冊<context:component-scan base-package=「cn.javass」/>。代表cn.javass包及其子包中,若是某個類的頭上帶有特定的註解【@Component/@Repository/@Service/@Controller】,就會將這個對象做爲Bean註冊進Spring容器。

以上的4個註解,用法徹底一摸同樣,只有語義上的區別。

@Component 是全部受Spring 管理組件的通用形式,Spring 還提供了更加細化的註解形式:  @Repository 、@Service 、@Controller ,它們分別對應數據層Bean ,業務層Bean ,和表現層Bean 。

其中,@Component不推薦使用。

這四個註解均可以放在類的頭上,若是不指定其value【@Service】,則默認的bean名字爲這個類的簡單類名首字母小寫;若是指定value【@Service(「dao」)】,則使用value做爲ban名字。

注意:若是cn.javass.SampleDao和cn.javass1.SampleDao都只用@Service註解,而不指定value會發生什麼事?

除了註冊Bean以外,還能夠經過在<bean>上設置屬性來控制Bean的其餘屬性,好比:

<bean name="" class=""

lazy-init=「true」  //是否延遲初始化

scope=「prototype」  //bean的生命週期

depends-on=「其餘bean「 //依賴其餘bean

/>

在Spring中也有相應的註解去對應

@Lazy

@Scope

@DependsOn

他們都是放在類的頭上。

除了註冊Bean以外,還能夠經過在<bean>上設置屬性來控制Bean的初始化方法和析構方法,好比:

<bean name="" class=""

init-method=「init「 //初始化方法

destroy-method=「close「 //析構方法

/>

在Spring中也有相應的Bean去對應,固然,這兩個註解是jdk裏內置的

@PostConstruct

@PreDestroy

這兩個註解放在方法頭上。

@Autowired 根據bean 類型從spring 上下文中進行查找。咱們須要從如下幾方面來學習這個註解:

一、它都能放在什麼頭上?

它能夠放在屬性、方法和構造方法頭上。

二、根據什麼注入?

2.一、若是某個接口的實現類在Spring容器中惟一的話,僅使用@Autowired就能夠正確注入,如:

@Autowired

private SampleDao dao;

2.二、若是某個接口的實現類在Spring容器中不惟一

2.2.一、用@Qualifier來指定注入Bean的名字,如

@Autowired

@Qualifier(「sampleDaoImpl」)

private SampleDao dao;

2.2.二、用@Primary在多個Bean之中指定哪一個爲最優先者,注意它不是跟@Autowired配合使用,而是跟@Service配合使用,如

@Service @Primary SampleDaoImpl

2.2.三、用屬性名、方法參數名、構造方法參數名來設置注入哪一個 Bean,如

public class SampleDaoImpl implements SampleDao

public class SampleDaoImpl1 implements SampleDao

對應

@Autowired

private SampleDao sampleDaoImpl;

注意:屬性名在編譯後是必定存在的;可是方法參數名和構造方法參數名必須指定相應的編譯選項才能保留。

三、@Autowired只有一個選項, boolean required() default true;是否必須,且默認爲true。所以,若是僅僅使用@Autowired,就必需要能注入,不然會報錯。

 

@Resource擁有和@Autowired相似的做用。

Spring還支持使用@Configuration,把一個類做爲一個IoC容器,它的某個方法頭上若是註冊了@Bean,就會做爲這個Spring容器中的Bean。


java代碼:

查看複製到剪貼板打印

  1. @Configuration("ctx")  
  2. public class JavaApplicationContext {  
  3. @Bean  
  4. public String hello(){  
  5. return "hello";  
  6. }  
  7. @Bean  
  8. public int max(){  
  9. return 9;  
  10. }  
  11. }  

使用AnnotationConfigApplicationContext得到Spring容器

ApplicationContext context = new AnnotationConfigApplicationContext(JavaApplicationContext.class);

使用@ImportResource (「classpath:applicationContext.xml」)能夠把其餘容器導入到這個容器中。

Spring使用的AOP註解分爲三個層次:

一、@Aspect放在類頭上,把這個類做爲一個切面,可是這個類必定要顯式的註冊在Spring容器中。

二、 @Pointcut放在方法頭上,定義一個可被別的方法引用的切入點表達式。

三、5種通知。

3.一、@Before,前置通知,放在方法頭上。

3.二、@After,後置【finally】通知,放在方法頭上。

3.三、@AfterReturning,後置【try】通知,放在方法頭上,使用returning來引用方法返回值。

3.四、@AfterThrowing,後置【catch】通知,放在方法頭上,使用throwing來引用拋出的異常。

3.五、@Around,環繞通知,放在方法頭上,這個方法要決定真實的方法是否執行,並且必須有返回值。

相關文章
相關標籤/搜索