Spring3註解

        在用spring框架的時候,一般都是將配置信息寫在xml配置文件中,非常複雜也很亂,大致示例如下:
<beans> 
<bean name="ds" class="org.apache.commons.dbcp.BasicDataSource"> 
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> 
<property name="url" value="jdbc:oracle:thin:@localhost:1521:wangbin"/> 
<property name="username" value="tech37"/> 
<property name="password" value="tech37"/> 
</bean> 
  
<bean name="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
<property name="dataSource" ref="ds"/> 
</bean> 
<tx:advice id="txAdvice" transaction-manager="txManager"> 
<tx:attributes> 
<tx:method name="get*" read-only="true"/> 
<tx:method name="*"/> 
</tx:attributes> 
</tx:advice> 
  
<aop:config> 
<aop:advisor advice-ref="txAdvice" 
pointcut="execution(* cn.javass..business.ebo.*Ebo.*(..))"/> 
</aop:config> 
</beans> 
在上面的示例中,我們可以典型的看到Spring的三種功能:
1、IoC容器,如:
<bean …>
<property…/>
</bean>
2、AOP
<aop:config/>
3、事務
<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 上下文中進行查找。我們需要從以下幾方面來學習這個註解:
1、它都能放在什麼頭上?
它可以放在屬性、方法和構造方法頭上。
2、根據什麼注入?
2.1、如果某個接口的實現類在Spring容器中唯一的話,僅使用@Autowired就可以正確注入,如:
@Autowired
private SampleDao dao;
2.2、如果某個接口的實現類在Spring容器中不唯一
2.2.1、用@Qualifier來指定注入Bean的名字,如
@Autowired
@Qualifier(“sampleDaoImpl”)
private SampleDao dao;
2.2.2、用@Primary在多個Bean之中指定哪個爲最優先者,注意它不是跟@Autowired配合使用,而是跟@Service配合使用,如
@Service @Primary SampleDaoImpl
2.2.3、用屬性名、方法參數名、構造方法參數名來設置注入哪個 Bean,如
public class SampleDaoImpl implements SampleDao
public class SampleDaoImpl1 implements SampleDao
對應

@Autowired
private SampleDao sampleDaoImpl;
注意:屬性名在編譯後是一定存在的;但是方法參數名和構造方法參數名必須指定相應的編譯選項才能保留。
3、@Autowired只有一個選項, boolean required() default true;是否必須,且默認爲true。因此,如果僅僅使用@Autowired,就必須要能注入,否則會報錯。
@Resource擁有和@Autowired類似的作用。
Spring還支持使用@Configuration,把一個類作爲一個IoC容器,它的某個方法頭上如果註冊了@Bean,就會作爲這個Spring容器中的Bean。
java代碼:
@Configuration("ctx") 
public class JavaApplicationContext { 
@Bean 
public String hello(){ 
return "hello"; 

@Bean 
public int max(){ 
return 9; 


使用AnnotationConfigApplicationContext獲得Spring容器
ApplicationContext context = new AnnotationConfigApplicationContext(JavaApplicationContext.class);
使用@ImportResource (“classpath:applicationContext.xml”)可以把其他容器導入到這個容器中。


Spring使用的AOP註解分爲三個層次:
1、@Aspect放在類頭上,把這個類作爲一個切面,但是這個類一定要顯式的註冊在Spring容器中。
2、 @Pointcut放在方法頭上,定義一個可被別的方法引用的切入點表達式。
3、5種通知。 3.1、@Before,前置通知,放在方法頭上。
3.2、@After,後置【finally】通知,放在方法頭上。
3.3、@AfterReturning,後置【try】通知,放在方法頭上,使用returning來引用方法返回值。
3.4、@AfterThrowing,後置【catch】通知,放在方法頭上,使用throwing來引用拋出的異常。
3.5、@Around,環繞通知,放在方法頭上,這個方法要決定真實的方法是否執行,而且必須有返回值。

發佈了22 篇原創文章 · 獲贊 3 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章