spring註解
依賴注入:
1. 使用Spring註解來注入屬性
1.1. 使用註解以前我們是怎樣注入屬性的
類的實現:
Java代碼
public class UserManagerImpl implements UserManager {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
...
}
public class UserManagerImpl implements UserManager {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
...
}
配置文件:
Java代碼
<bean id="userManagerImpl" class="com.kedacom.spring.annotation.service.UserManagerImpl">
<property name="userDao" ref="userDao" />
</bean>
<bean id="userDao" class="com.kedacom.spring.annotation.persistence.UserDaoImpl">
<property name="sessionFactory" ref="mySessionFactory" />
</bean>
<bean id="userManagerImpl" class="com.kedacom.spring.annotation.service.UserManagerImpl">
<property name="userDao" ref="userDao" />
</bean>
<bean id="userDao" class="com.kedacom.spring.annotation.persistence.UserDaoImpl">
<property name="sessionFactory" ref="mySessionFactory" />
</bean>
1.2. 引入@Autowired註解(不推薦使用,建議使用@Resource)
類的實現(對成員變量進行標註)
Java代碼
public class UserManagerImpl implements UserManager {
@Autowired
private UserDao userDao;
...
}
public class UserManagerImpl implements UserManager {
@Autowired
private UserDao userDao;
...
}
或者(對方法進行標註)
Java代碼
public class UserManagerImpl implements UserManager {
private UserDao userDao;
@Autowired
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
...
}
public class UserManagerImpl implements UserManager {
private UserDao userDao;
@Autowired
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
...
}
配置文件
Java代碼
<bean id="userManagerImpl" class="com.kedacom.spring.annotation.service.UserManagerImpl" />
<bean id="userDao" class="com.kedacom.spring.annotation.persistence.UserDaoImpl">
<property name="sessionFactory" ref="mySessionFactory" />
</bean>
<bean id="userManagerImpl" class="com.kedacom.spring.annotation.service.UserManagerImpl" />
<bean id="userDao" class="com.kedacom.spring.annotation.persistence.UserDaoImpl">
<property name="sessionFactory" ref="mySessionFactory" />
</bean>
@Autowired可以對成員變量、方法和構造函數進行標註,來完成自動裝配的工作。以上兩種不同實現方式中,@Autowired的標註位置不同,它們都會在Spring在初始化userManagerImpl這個bean時,自動裝配userDao這個屬性,區別是:第一種實現中,Spring會直接將UserDao類型的唯一一個bean賦值給userDao這個成員變量;第二種實現中,Spring會調用setUserDao方法來將UserDao類型的唯一一個bean裝配到userDao這個屬性。
1.3. 讓@Autowired工作起來
要使@Autowired能夠工作,還需要在配置文件中加入以下代碼
Java代碼
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
1.4. @Qualifier
@Autowired是根據類型進行自動裝配的。在上面的例子中,如果當Spring上下文中存在不止一個UserDao類型的bean時,就會拋出BeanCreationException異常;如果Spring上下文中不存在UserDao類型的bean,也會拋出BeanCreationException異常。我們可以使用@Qualifier配合@Autowired來解決這些問題。
1. 可能存在多個UserDao實例
Java代碼
@Autowired
public void setUserDao(@Qualifier("userDao") UserDao userDao) {
this.userDao = userDao;
}
@Autowired
public void setUserDao(@Qualifier("userDao") UserDao userDao) {
this.userDao = userDao;
}
這樣,Spring會找到id爲userDao的bean進行裝配。
2. 可能不存在UserDao實例
Java代碼
@Autowired(required = false)
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Autowired(required = false)
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
1.5. @Resource(JSR-250標準註解,推薦使用它來代替Spring專有的@Autowired註解)
Spring 不但支持自己定義的@Autowired註解,還支持幾個由JSR-250規範定義的註解,它們分別是@Resource、@PostConstruct以及@PreDestroy。
@Resource的作用相當於@Autowired,只不過@Autowired按byType自動注入,而@Resource默認按byName自動注入罷了。@Resource有兩個屬性是比較重要的,分別是name和type,Spring將@Resource註解的name屬性解析爲bean的名字,而type屬性則解析爲bean的類型。所以如果使用name屬性,則使用byName的自動注入策略,而使用type屬性時則使用byType自動注入策略。如果既不指定name也不指定type屬性,這時將通過反射機制使用byName自動注入策略。
@Resource裝配順序
如果同時指定了name和type,則從Spring上下文中找到唯一匹配的bean進行裝配,找不到則拋出異常
如果指定了name,則從上下文中查找名稱(id)匹配的bean進行裝配,找不到則拋出異常
如果指定了type,則從上下文中找到類型匹配的唯一bean進行裝配,找不到或者找到多個,都會拋出異常
如果既沒有指定name,又沒有指定type,則自動按照byName方式進行裝配(見2);如果沒有匹配,則回退爲一個原始類型(UserDao)進行匹配,如果匹配則自動裝配;
1.6. @PostConstruct(JSR-250)
在方法上加上註解@PostConstruct,這個方法就會在Bean初始化之後被Spring容器執行(注:Bean初始化包括,實例化Bean,並裝配Bean的屬性(依賴注入))。
它的一個典型的應用場景是,當你需要往Bean裏注入一個其父類中定義的屬性,而你又無法複寫父類的屬性或屬性的setter方法時,如:
Java代碼
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
private SessionFactory mySessionFacotry;
@Resource
public void setMySessionFacotry(SessionFactory sessionFacotry) {
this.mySessionFacotry = sessionFacotry;
}
@PostConstruct
public void injectSessionFactory() {
super.setSessionFactory(mySessionFacotry);
}
...
}
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
private SessionFactory mySessionFacotry;
@Resource
public void setMySessionFacotry(SessionFactory sessionFacotry) {
this.mySessionFacotry = sessionFacotry;
}
@PostConstruct
public void injectSessionFactory() {
super.setSessionFactory(mySessionFacotry);
}
...
}
這裏通過@PostConstruct,爲UserDaoImpl的父類裏定義的一個sessionFactory私有屬性,注入了我們自己定義的sessionFactory(父類的setSessionFactory方法爲final,不可複寫),之後我們就可以通過調用super.getSessionFactory()來訪問該屬性了。
1.7. @PreDestroy(JSR-250)
在方法上加上註解@PreDestroy,這個方法就會在Bean初始化之後被Spring容器執行。由於我們當前還沒有需要用到它的場景,這裏不不去演示。其用法同@PostConstruct。
1.8. 使用<context:annotation-config />簡化配置
Spring2.1添加了一個新的context的Schema命名空間,該命名空間對註釋驅動、屬性文件引入、加載期織入等功能提供了便捷的配置。我們知道註釋本身是不會做任何事情的,它僅提供元數據信息。要使元數據信息真正起作用,必須讓負責處理這些元數據的處理器工作起來。
AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor就是處理這些註釋元數據的處理器。但是直接在Spring配置文件中定義這些Bean顯得比較笨拙。Spring爲我們提供了一種方便的註冊這些BeanPostProcessor的方式,這就是<context:annotation-config />:
Java代碼
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
</beans>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
</beans>
<context:annotationconfig />將隱式地向Spring容器註冊AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、 PersistenceAnnotationBeanPostProcessor以及RequiredAnnotationBeanPostProcessor這4個BeanPostProcessor。
2. 使用Spring註解完成Bean的定義
以上我們介紹了通過@Autowired或@Resource來實現在Bean中自動注入的功能,下面我們將介紹如何註解Bean,從而從XML配置文件中完全移除Bean定義的配置。
2.1. @Component(不推薦使用)、@Repository、@Service、@Controller
只需要在對應的類上加上一個@Component註解,就將該類定義爲一個Bean了:
Java代碼
@Component
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
...
}
@Component
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
...
}
使用@Component註解定義的Bean,默認的名稱(id)是小寫開頭的非限定類名。如這裏定義的Bean名稱就是userDaoImpl。你也可以指定Bean的名稱:
@Component("userDao")
@Component是所有受Spring管理組件的通用形式,Spring還提供了更加細化的註解形式:@Repository、@Service、@Controller,它們分別對應存儲層Bean,業務層Bean,和展示層Bean。目前版本(2.5)中,這些註解與@Component的語義是一樣的,完全通用,在Spring以後的版本中可能會給它們追加更多的語義。所以,我們推薦使用@Repository、@Service、@Controller來替代@Component。
2.2. 使用<context:component-scan />讓Bean定義註解工作起來
Java代碼
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com.kedacom.ksoa" />
</beans>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com.kedacom.ksoa" />
</beans>
這裏,所有通過<bean>元素定義Bean的配置內容已經被移除,僅需要添加一行<context:component-scan />配置就解決所有問題了——Spring XML配置文件得到了極致的簡化(當然配置元數據還是需要的,只不過以註釋形式存在罷了)。<context:component-scan />的base-package屬性指定了需要掃描的類包,類包及其遞歸子包中所有的類都會被處理。
<context:component-scan />還允許定義過濾器將基包下的某些類納入或排除。Spring支持以下4種類型的過濾方式:
過濾器類型 表達式範例 說明
註解 org.example.SomeAnnotation 將所有使用SomeAnnotation註解的類過濾出來
類名指定 org.example.SomeClass 過濾指定的類
正則表達式 com\.kedacom\.spring\.annotation\.web\..* 通過正則表達式過濾一些類
AspectJ表達式 org.example..*Service+ 通過AspectJ表達式過濾一些類
以正則表達式爲例,我列舉一個應用實例:
Java代碼
<context:component-scan base-package="com.casheen.spring.annotation">
<context:exclude-filter type="regex" expression="com\.casheen\.spring\.annotation\.web\..*" />
</context:component-scan>
<context:component-scan base-package="com.casheen.spring.annotation">
<context:exclude-filter type="regex" expression="com\.casheen\.spring\.annotation\.web\..*" />
</context:component-scan>
值得注意的是<context:component-scan />配置項不但啓用了對類包進行掃描以實施註釋驅動Bean定義的功能,同時還啓用了註釋驅動自動注入的功能(即還隱式地在內部註冊了AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor),因此當使用<context:component-scan />後,就可以將<context:annotation-config />移除了。
2.3. 使用@Scope來定義Bean的作用範圍
在使用XML定義Bean時,我們可能還需要通過bean的scope屬性來定義一個Bean的作用範圍,我們同樣可以通過@Scope註解來完成這項工作:
Java代碼
@Scope("session")
@Component()
public class UserSessionBean implements Serializable {
...
}
@Scope("session")
@Component()
public class UserSessionBean implements Serializable {
...
}
3. 參考
http://kingtai168.javaeye.com/blog/244002
http://www.javaeye.com/topic/244153
http://static.springframework.org/spring/docs/2.5.x/reference/beans.html#beans-annotation-config
http://static.springframework.org/spring/docs/2.5.x/reference/beans.html#beans-classpath-scanning
事物管理:
一.spring配置文件
這裏使用spring命名空間,如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
default-autowire="byName" default-lazy-init="true">
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="initialSize" value="${jdbc.initialSize}" />
<property name="maxActive" value="${jdbc.maxActive}" />
<property name="maxIdle" value="${jdbc.maxIdle}" />
<property name="minIdle" value="${jdbc.minIdle}" />
<property name="maxWait" value="${jdbc.maxWait}" />
</bean>
<!-- 設定transactionManager -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!--啓動spring註解功能-->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
說明:
1.如果事務管理器的id是transactionManager,這裏可以不對transaction-manager進行配置,即<tx:annotation-driven />就可以。
2.這個配置是告訴spring在類(接口)層面或者方法層面上檢查應用程序上下文中的所有標準了@Transactional的bean,spring將自動把事務通知的內容通知給它。
3.這個通知的事務參數將由@Transactional註釋的參數來定義。
4.如果註釋的是接口,則該接口的所有實現類都將被事務化。
二.使用@Transactional標註bean
package com.netqin.bbs.initUserData;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.netqin.bbs.initUserData.service.InitUserDataService;
import com.netqin.bbs.utils.Constant;
import com.netqin.bbs.utils.ReadFileUtil;
@Transactional(propagation=Propagation.SUPPORTS,readOnly=true)//設置默認的事務管理策略,即沒有標註@Transactional的方法的事務處理方式,意思爲不要求方法必須在一個事務中運行
public class InitUserData {
/**
* 描述 :使用方法上聲明的事務管理策略,這裏的意思爲需要在一個事務中運行. <br>
*<p>
*/
@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
public void method1(){
……………………
……………………
}
/**
* 描述 : 使用默認策略. <br>
*<p>
*/
public void method2(){
……………………
……………………
}
}
記得要將這個bean加入到spring上下文中。
一般來說,上述兩種事務策略就可以滿足要求了,不過需要注意,註解功能的使用需要在項目中加入cglib-nodep-x.x_x.jar
面向切面(AOP)
Spring AOP AOP中的概念
Aspect(切面):指橫切性關注點的抽象即爲切面,它與類相似,只是兩者的關注點不一樣,類是對物體特徵的抽象,而切面是橫切性關注點的抽象(包括切入點的描述和通知的描述)。
Joinpoint(連接點):所謂連接點是指那些被攔截到的點。在spring中,這些點指的是方法,
因爲spring只支持方法型的連接點,實際上joinpoint還可以是field或者構造器。
Pointcut(切入點):所謂切入點是指我們要對那些joinpoint進行攔截的定義。
Advice(通知):所謂通知是指攔截到jointpoint之後所要做的事情就是通知。通知分爲前置通知、後置通知、異常通知、最終通知、環繞通知。
Target(目標對象):代理的目標對象
Weave(織入): 指將aspects應用到target對象並導致proxy對象創建的過程稱爲織入
Introducton(引入):在不修改類代碼的前提下,Introduction可以在運行期爲類動態地添加一些方法或Field
Spring提供了兩種切面使用方式,實際工作中我們可以選用其中一種:
1 基於xml配置方式進行AOP開發
2 基於註解方式進行AOP開發
(一)基於註解的方式
下面是基於註解的方式
Java代碼
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<aop:aspectj-autoproxy/><!-- 啓動對@AspectJ註解的支持 -->
</beans>
Java代碼
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect @Component
public class MyInterceptor {
/**
*@Pointcut :表示規定切入點
*execution() 語法規範
* 第一個“*”表示任意返回結果類型
* “cn.itcast.service.impl.PersonServiceBean”:表示對此類進行攔截,
* 如果是cn.itcast.service..*.*:表示對包cn.itcast.service以及子包裏所
有的類的所有方法進行攔截,
* (..)表示參數
*/
@Pointcut("execution(* com.mingbai.springaop.PersonServiceBean.*(..))")
private void anyMethod(){}//聲明一個切入點
/* @Before("anyMethod()")
public void doAccessCheck(){
System.out.println("前置通知");
}*/
//此時的前置通知,只能攔截到參數個數和類型匹配的方法
//args(name)中的name必須和方法doAccessCheck的參數一至
@Before("anyMethod() && args(name)")
public void doAccessCheck(String name){
System.out.println(name+"前置通知");
}
/* @AfterReturning("anyMethod()")
public void doAfterReturn(){
System.out.println("後置通知");
}*/
//得到方法的返回值
@AfterReturning(pointcut="anyMethod()",returning="result")
public void doAfterReturn(String result){
System.out.println("後置通知 "+result);
}
@After("anyMethod()")
public void doAfter(){
System.out.println("最終通知");
}
/* @AfterThrowing("anyMethod()")
public void doAfterThrow(){
System.out.println("異常通知");
}*/
@AfterThrowing(pointcut="anyMethod()",throwing="e")
public void doAfterThrow(Exception e){
System.out.println("異常通知------"+e.getMessage());
}
@Around("anyMethod()")
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("環繞通知 開始");
Object obj = pjp.proceed();
System.out.println("環繞通知 結束");
return obj;
}
}
(二)基於xml配置文件的
切面只是一個普通的javabean
Java代碼
import org.aspectj.lang.ProceedingJoinPoint;
public class MyInterceptor1 {
public void doAccessCheck(){
System.out.println("前置通知-------");
}
public void doAfterReturn(){
System.out.println("後置通知");
}
public void doAfter(){
System.out.println("最終通知");
}
public void doAfterThrow(){
System.out.println("異常通知");
}
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("環繞通知 開始");
Object obj = pjp.proceed();
System.out.println("環繞通知 結束");
return obj;
}
} 配置文件 :
Java代碼
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
[color=brown] <bean id="per" class="com.mingbai.springaop.PersonServiceBean"/>
<bean id="myInterceptor" class="com.mingbai.springaop.MyInterceptor1"/>
<!--
<aop:config>
<aop:aspect id="asp" ref="myInterceptor">
<aop:pointcut id="mycut" expression="execution(* com.mingbai.springaop.*.*(..))"/>
<aop:before pointcut-ref="mycut" method="doAccessCheck"/>
<aop:after-returning pointcut-ref="mycut" method="doAfterReturn"/>
<aop:after pointcut-ref="mycut" method="doAfter"/>
<aop:after-throwing pointcut-ref="mycut" method="doAfterThrow"/>
<aop:around pointcut-ref="mycut" method="doBasicProfiling"/>
</aop:aspect>
</aop:config>[/color]
-->
<!-- 只是攔截返回類型爲java.lang.String的方法
<aop:config>
<aop:aspect id="asp" ref="myInterceptor">
<aop:pointcut id="mycut" expression="execution(java.lang.String com.mingbai.springaop.*.*(..))"/>
<aop:before pointcut-ref="mycut" method="doAccessCheck"/>
<aop:after-returning pointcut-ref="mycut" method="doAfterReturn"/>
<aop:after pointcut-ref="mycut" method="doAfter"/>
<aop:after-throwing pointcut-ref="mycut" method="doAfterThrow"/>
<aop:around pointcut-ref="mycut" method="doBasicProfiling"/>
</aop:aspect>
</aop:config>
-->
<!-- 返回非void的方法 -->
<aop:config>
<aop:aspect id="asp" ref="myInterceptor">
<aop:pointcut id="mycut" expression="execution(!void com.mingbai.springaop.*.*(..))"/>
<aop:before pointcut-ref="mycut" method="doAccessCheck"/>
<aop:after-returning pointcut-ref="mycut" method="doAfterReturn"/>
<aop:after pointcut-ref="mycut" method="doAfter"/>
<aop:after-throwing pointcut-ref="mycut" method="doAfterThrow"/>
<aop:around pointcut-ref="mycut" method="doBasicProfiling"/>
</aop:aspect>
</aop:config>
<!-- 匹配第一個參數爲java.lang.String,其它的無所謂
<aop:config>
<aop:aspect id="asp" ref="myInterceptor">
<aop:pointcut id="mycut" expression="execution(* com.mingbai.springaop.*.*(..))"/>
<aop:before pointcut-ref="mycut" method="doAccessCheck"/>
<aop:after-returning pointcut-ref="mycut" method="doAfterReturn"/>
<aop:after pointcut-ref="mycut" method="doAfter"/>
<aop:after-throwing pointcut-ref="mycut" method="doAfterThrow"/>
<aop:around pointcut-ref="mycut" method="doBasicProfiling"/>
</aop:aspect>
</aop:config>
-->
</beans>
1. 使用Spring註解來注入屬性
1.1. 使用註解以前我們是怎樣注入屬性的
類的實現:
Java代碼
public class UserManagerImpl implements UserManager {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
...
}
public class UserManagerImpl implements UserManager {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
...
}
配置文件:
Java代碼
<bean id="userManagerImpl" class="com.kedacom.spring.annotation.service.UserManagerImpl">
<property name="userDao" ref="userDao" />
</bean>
<bean id="userDao" class="com.kedacom.spring.annotation.persistence.UserDaoImpl">
<property name="sessionFactory" ref="mySessionFactory" />
</bean>
<bean id="userManagerImpl" class="com.kedacom.spring.annotation.service.UserManagerImpl">
<property name="userDao" ref="userDao" />
</bean>
<bean id="userDao" class="com.kedacom.spring.annotation.persistence.UserDaoImpl">
<property name="sessionFactory" ref="mySessionFactory" />
</bean>
1.2. 引入@Autowired註解(不推薦使用,建議使用@Resource)
類的實現(對成員變量進行標註)
Java代碼
public class UserManagerImpl implements UserManager {
@Autowired
private UserDao userDao;
...
}
public class UserManagerImpl implements UserManager {
@Autowired
private UserDao userDao;
...
}
或者(對方法進行標註)
Java代碼
public class UserManagerImpl implements UserManager {
private UserDao userDao;
@Autowired
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
...
}
public class UserManagerImpl implements UserManager {
private UserDao userDao;
@Autowired
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
...
}
配置文件
Java代碼
<bean id="userManagerImpl" class="com.kedacom.spring.annotation.service.UserManagerImpl" />
<bean id="userDao" class="com.kedacom.spring.annotation.persistence.UserDaoImpl">
<property name="sessionFactory" ref="mySessionFactory" />
</bean>
<bean id="userManagerImpl" class="com.kedacom.spring.annotation.service.UserManagerImpl" />
<bean id="userDao" class="com.kedacom.spring.annotation.persistence.UserDaoImpl">
<property name="sessionFactory" ref="mySessionFactory" />
</bean>
@Autowired可以對成員變量、方法和構造函數進行標註,來完成自動裝配的工作。以上兩種不同實現方式中,@Autowired的標註位置不同,它們都會在Spring在初始化userManagerImpl這個bean時,自動裝配userDao這個屬性,區別是:第一種實現中,Spring會直接將UserDao類型的唯一一個bean賦值給userDao這個成員變量;第二種實現中,Spring會調用setUserDao方法來將UserDao類型的唯一一個bean裝配到userDao這個屬性。
1.3. 讓@Autowired工作起來
要使@Autowired能夠工作,還需要在配置文件中加入以下代碼
Java代碼
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
1.4. @Qualifier
@Autowired是根據類型進行自動裝配的。在上面的例子中,如果當Spring上下文中存在不止一個UserDao類型的bean時,就會拋出BeanCreationException異常;如果Spring上下文中不存在UserDao類型的bean,也會拋出BeanCreationException異常。我們可以使用@Qualifier配合@Autowired來解決這些問題。
1. 可能存在多個UserDao實例
Java代碼
@Autowired
public void setUserDao(@Qualifier("userDao") UserDao userDao) {
this.userDao = userDao;
}
@Autowired
public void setUserDao(@Qualifier("userDao") UserDao userDao) {
this.userDao = userDao;
}
這樣,Spring會找到id爲userDao的bean進行裝配。
2. 可能不存在UserDao實例
Java代碼
@Autowired(required = false)
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Autowired(required = false)
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
1.5. @Resource(JSR-250標準註解,推薦使用它來代替Spring專有的@Autowired註解)
Spring 不但支持自己定義的@Autowired註解,還支持幾個由JSR-250規範定義的註解,它們分別是@Resource、@PostConstruct以及@PreDestroy。
@Resource的作用相當於@Autowired,只不過@Autowired按byType自動注入,而@Resource默認按byName自動注入罷了。@Resource有兩個屬性是比較重要的,分別是name和type,Spring將@Resource註解的name屬性解析爲bean的名字,而type屬性則解析爲bean的類型。所以如果使用name屬性,則使用byName的自動注入策略,而使用type屬性時則使用byType自動注入策略。如果既不指定name也不指定type屬性,這時將通過反射機制使用byName自動注入策略。
@Resource裝配順序
如果同時指定了name和type,則從Spring上下文中找到唯一匹配的bean進行裝配,找不到則拋出異常
如果指定了name,則從上下文中查找名稱(id)匹配的bean進行裝配,找不到則拋出異常
如果指定了type,則從上下文中找到類型匹配的唯一bean進行裝配,找不到或者找到多個,都會拋出異常
如果既沒有指定name,又沒有指定type,則自動按照byName方式進行裝配(見2);如果沒有匹配,則回退爲一個原始類型(UserDao)進行匹配,如果匹配則自動裝配;
1.6. @PostConstruct(JSR-250)
在方法上加上註解@PostConstruct,這個方法就會在Bean初始化之後被Spring容器執行(注:Bean初始化包括,實例化Bean,並裝配Bean的屬性(依賴注入))。
它的一個典型的應用場景是,當你需要往Bean裏注入一個其父類中定義的屬性,而你又無法複寫父類的屬性或屬性的setter方法時,如:
Java代碼
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
private SessionFactory mySessionFacotry;
@Resource
public void setMySessionFacotry(SessionFactory sessionFacotry) {
this.mySessionFacotry = sessionFacotry;
}
@PostConstruct
public void injectSessionFactory() {
super.setSessionFactory(mySessionFacotry);
}
...
}
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
private SessionFactory mySessionFacotry;
@Resource
public void setMySessionFacotry(SessionFactory sessionFacotry) {
this.mySessionFacotry = sessionFacotry;
}
@PostConstruct
public void injectSessionFactory() {
super.setSessionFactory(mySessionFacotry);
}
...
}
這裏通過@PostConstruct,爲UserDaoImpl的父類裏定義的一個sessionFactory私有屬性,注入了我們自己定義的sessionFactory(父類的setSessionFactory方法爲final,不可複寫),之後我們就可以通過調用super.getSessionFactory()來訪問該屬性了。
1.7. @PreDestroy(JSR-250)
在方法上加上註解@PreDestroy,這個方法就會在Bean初始化之後被Spring容器執行。由於我們當前還沒有需要用到它的場景,這裏不不去演示。其用法同@PostConstruct。
1.8. 使用<context:annotation-config />簡化配置
Spring2.1添加了一個新的context的Schema命名空間,該命名空間對註釋驅動、屬性文件引入、加載期織入等功能提供了便捷的配置。我們知道註釋本身是不會做任何事情的,它僅提供元數據信息。要使元數據信息真正起作用,必須讓負責處理這些元數據的處理器工作起來。
AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor就是處理這些註釋元數據的處理器。但是直接在Spring配置文件中定義這些Bean顯得比較笨拙。Spring爲我們提供了一種方便的註冊這些BeanPostProcessor的方式,這就是<context:annotation-config />:
Java代碼
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
</beans>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
</beans>
<context:annotationconfig />將隱式地向Spring容器註冊AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、 PersistenceAnnotationBeanPostProcessor以及RequiredAnnotationBeanPostProcessor這4個BeanPostProcessor。
2. 使用Spring註解完成Bean的定義
以上我們介紹了通過@Autowired或@Resource來實現在Bean中自動注入的功能,下面我們將介紹如何註解Bean,從而從XML配置文件中完全移除Bean定義的配置。
2.1. @Component(不推薦使用)、@Repository、@Service、@Controller
只需要在對應的類上加上一個@Component註解,就將該類定義爲一個Bean了:
Java代碼
@Component
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
...
}
@Component
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
...
}
使用@Component註解定義的Bean,默認的名稱(id)是小寫開頭的非限定類名。如這裏定義的Bean名稱就是userDaoImpl。你也可以指定Bean的名稱:
@Component("userDao")
@Component是所有受Spring管理組件的通用形式,Spring還提供了更加細化的註解形式:@Repository、@Service、@Controller,它們分別對應存儲層Bean,業務層Bean,和展示層Bean。目前版本(2.5)中,這些註解與@Component的語義是一樣的,完全通用,在Spring以後的版本中可能會給它們追加更多的語義。所以,我們推薦使用@Repository、@Service、@Controller來替代@Component。
2.2. 使用<context:component-scan />讓Bean定義註解工作起來
Java代碼
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com.kedacom.ksoa" />
</beans>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com.kedacom.ksoa" />
</beans>
這裏,所有通過<bean>元素定義Bean的配置內容已經被移除,僅需要添加一行<context:component-scan />配置就解決所有問題了——Spring XML配置文件得到了極致的簡化(當然配置元數據還是需要的,只不過以註釋形式存在罷了)。<context:component-scan />的base-package屬性指定了需要掃描的類包,類包及其遞歸子包中所有的類都會被處理。
<context:component-scan />還允許定義過濾器將基包下的某些類納入或排除。Spring支持以下4種類型的過濾方式:
過濾器類型 表達式範例 說明
註解 org.example.SomeAnnotation 將所有使用SomeAnnotation註解的類過濾出來
類名指定 org.example.SomeClass 過濾指定的類
正則表達式 com\.kedacom\.spring\.annotation\.web\..* 通過正則表達式過濾一些類
AspectJ表達式 org.example..*Service+ 通過AspectJ表達式過濾一些類
以正則表達式爲例,我列舉一個應用實例:
Java代碼
<context:component-scan base-package="com.casheen.spring.annotation">
<context:exclude-filter type="regex" expression="com\.casheen\.spring\.annotation\.web\..*" />
</context:component-scan>
<context:component-scan base-package="com.casheen.spring.annotation">
<context:exclude-filter type="regex" expression="com\.casheen\.spring\.annotation\.web\..*" />
</context:component-scan>
值得注意的是<context:component-scan />配置項不但啓用了對類包進行掃描以實施註釋驅動Bean定義的功能,同時還啓用了註釋驅動自動注入的功能(即還隱式地在內部註冊了AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor),因此當使用<context:component-scan />後,就可以將<context:annotation-config />移除了。
2.3. 使用@Scope來定義Bean的作用範圍
在使用XML定義Bean時,我們可能還需要通過bean的scope屬性來定義一個Bean的作用範圍,我們同樣可以通過@Scope註解來完成這項工作:
Java代碼
@Scope("session")
@Component()
public class UserSessionBean implements Serializable {
...
}
@Scope("session")
@Component()
public class UserSessionBean implements Serializable {
...
}
3. 參考
http://kingtai168.javaeye.com/blog/244002
http://www.javaeye.com/topic/244153
http://static.springframework.org/spring/docs/2.5.x/reference/beans.html#beans-annotation-config
http://static.springframework.org/spring/docs/2.5.x/reference/beans.html#beans-classpath-scanning
事物管理:
一.spring配置文件
這裏使用spring命名空間,如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
default-autowire="byName" default-lazy-init="true">
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="initialSize" value="${jdbc.initialSize}" />
<property name="maxActive" value="${jdbc.maxActive}" />
<property name="maxIdle" value="${jdbc.maxIdle}" />
<property name="minIdle" value="${jdbc.minIdle}" />
<property name="maxWait" value="${jdbc.maxWait}" />
</bean>
<!-- 設定transactionManager -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!--啓動spring註解功能-->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
說明:
1.如果事務管理器的id是transactionManager,這裏可以不對transaction-manager進行配置,即<tx:annotation-driven />就可以。
2.這個配置是告訴spring在類(接口)層面或者方法層面上檢查應用程序上下文中的所有標準了@Transactional的bean,spring將自動把事務通知的內容通知給它。
3.這個通知的事務參數將由@Transactional註釋的參數來定義。
4.如果註釋的是接口,則該接口的所有實現類都將被事務化。
二.使用@Transactional標註bean
package com.netqin.bbs.initUserData;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.netqin.bbs.initUserData.service.InitUserDataService;
import com.netqin.bbs.utils.Constant;
import com.netqin.bbs.utils.ReadFileUtil;
@Transactional(propagation=Propagation.SUPPORTS,readOnly=true)//設置默認的事務管理策略,即沒有標註@Transactional的方法的事務處理方式,意思爲不要求方法必須在一個事務中運行
public class InitUserData {
/**
* 描述 :使用方法上聲明的事務管理策略,這裏的意思爲需要在一個事務中運行. <br>
*<p>
*/
@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
public void method1(){
……………………
……………………
}
/**
* 描述 : 使用默認策略. <br>
*<p>
*/
public void method2(){
……………………
……………………
}
}
記得要將這個bean加入到spring上下文中。
一般來說,上述兩種事務策略就可以滿足要求了,不過需要注意,註解功能的使用需要在項目中加入cglib-nodep-x.x_x.jar
面向切面(AOP)
Spring AOP AOP中的概念
Aspect(切面):指橫切性關注點的抽象即爲切面,它與類相似,只是兩者的關注點不一樣,類是對物體特徵的抽象,而切面是橫切性關注點的抽象(包括切入點的描述和通知的描述)。
Joinpoint(連接點):所謂連接點是指那些被攔截到的點。在spring中,這些點指的是方法,
因爲spring只支持方法型的連接點,實際上joinpoint還可以是field或者構造器。
Pointcut(切入點):所謂切入點是指我們要對那些joinpoint進行攔截的定義。
Advice(通知):所謂通知是指攔截到jointpoint之後所要做的事情就是通知。通知分爲前置通知、後置通知、異常通知、最終通知、環繞通知。
Target(目標對象):代理的目標對象
Weave(織入): 指將aspects應用到target對象並導致proxy對象創建的過程稱爲織入
Introducton(引入):在不修改類代碼的前提下,Introduction可以在運行期爲類動態地添加一些方法或Field
Spring提供了兩種切面使用方式,實際工作中我們可以選用其中一種:
1 基於xml配置方式進行AOP開發
2 基於註解方式進行AOP開發
(一)基於註解的方式
下面是基於註解的方式
Java代碼
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<aop:aspectj-autoproxy/><!-- 啓動對@AspectJ註解的支持 -->
</beans>
Java代碼
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect @Component
public class MyInterceptor {
/**
*@Pointcut :表示規定切入點
*execution() 語法規範
* 第一個“*”表示任意返回結果類型
* “cn.itcast.service.impl.PersonServiceBean”:表示對此類進行攔截,
* 如果是cn.itcast.service..*.*:表示對包cn.itcast.service以及子包裏所
有的類的所有方法進行攔截,
* (..)表示參數
*/
@Pointcut("execution(* com.mingbai.springaop.PersonServiceBean.*(..))")
private void anyMethod(){}//聲明一個切入點
/* @Before("anyMethod()")
public void doAccessCheck(){
System.out.println("前置通知");
}*/
//此時的前置通知,只能攔截到參數個數和類型匹配的方法
//args(name)中的name必須和方法doAccessCheck的參數一至
@Before("anyMethod() && args(name)")
public void doAccessCheck(String name){
System.out.println(name+"前置通知");
}
/* @AfterReturning("anyMethod()")
public void doAfterReturn(){
System.out.println("後置通知");
}*/
//得到方法的返回值
@AfterReturning(pointcut="anyMethod()",returning="result")
public void doAfterReturn(String result){
System.out.println("後置通知 "+result);
}
@After("anyMethod()")
public void doAfter(){
System.out.println("最終通知");
}
/* @AfterThrowing("anyMethod()")
public void doAfterThrow(){
System.out.println("異常通知");
}*/
@AfterThrowing(pointcut="anyMethod()",throwing="e")
public void doAfterThrow(Exception e){
System.out.println("異常通知------"+e.getMessage());
}
@Around("anyMethod()")
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("環繞通知 開始");
Object obj = pjp.proceed();
System.out.println("環繞通知 結束");
return obj;
}
}
(二)基於xml配置文件的
切面只是一個普通的javabean
Java代碼
import org.aspectj.lang.ProceedingJoinPoint;
public class MyInterceptor1 {
public void doAccessCheck(){
System.out.println("前置通知-------");
}
public void doAfterReturn(){
System.out.println("後置通知");
}
public void doAfter(){
System.out.println("最終通知");
}
public void doAfterThrow(){
System.out.println("異常通知");
}
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("環繞通知 開始");
Object obj = pjp.proceed();
System.out.println("環繞通知 結束");
return obj;
}
} 配置文件 :
Java代碼
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
[color=brown] <bean id="per" class="com.mingbai.springaop.PersonServiceBean"/>
<bean id="myInterceptor" class="com.mingbai.springaop.MyInterceptor1"/>
<!--
<aop:config>
<aop:aspect id="asp" ref="myInterceptor">
<aop:pointcut id="mycut" expression="execution(* com.mingbai.springaop.*.*(..))"/>
<aop:before pointcut-ref="mycut" method="doAccessCheck"/>
<aop:after-returning pointcut-ref="mycut" method="doAfterReturn"/>
<aop:after pointcut-ref="mycut" method="doAfter"/>
<aop:after-throwing pointcut-ref="mycut" method="doAfterThrow"/>
<aop:around pointcut-ref="mycut" method="doBasicProfiling"/>
</aop:aspect>
</aop:config>[/color]
-->
<!-- 只是攔截返回類型爲java.lang.String的方法
<aop:config>
<aop:aspect id="asp" ref="myInterceptor">
<aop:pointcut id="mycut" expression="execution(java.lang.String com.mingbai.springaop.*.*(..))"/>
<aop:before pointcut-ref="mycut" method="doAccessCheck"/>
<aop:after-returning pointcut-ref="mycut" method="doAfterReturn"/>
<aop:after pointcut-ref="mycut" method="doAfter"/>
<aop:after-throwing pointcut-ref="mycut" method="doAfterThrow"/>
<aop:around pointcut-ref="mycut" method="doBasicProfiling"/>
</aop:aspect>
</aop:config>
-->
<!-- 返回非void的方法 -->
<aop:config>
<aop:aspect id="asp" ref="myInterceptor">
<aop:pointcut id="mycut" expression="execution(!void com.mingbai.springaop.*.*(..))"/>
<aop:before pointcut-ref="mycut" method="doAccessCheck"/>
<aop:after-returning pointcut-ref="mycut" method="doAfterReturn"/>
<aop:after pointcut-ref="mycut" method="doAfter"/>
<aop:after-throwing pointcut-ref="mycut" method="doAfterThrow"/>
<aop:around pointcut-ref="mycut" method="doBasicProfiling"/>
</aop:aspect>
</aop:config>
<!-- 匹配第一個參數爲java.lang.String,其它的無所謂
<aop:config>
<aop:aspect id="asp" ref="myInterceptor">
<aop:pointcut id="mycut" expression="execution(* com.mingbai.springaop.*.*(..))"/>
<aop:before pointcut-ref="mycut" method="doAccessCheck"/>
<aop:after-returning pointcut-ref="mycut" method="doAfterReturn"/>
<aop:after pointcut-ref="mycut" method="doAfter"/>
<aop:after-throwing pointcut-ref="mycut" method="doAfterThrow"/>
<aop:around pointcut-ref="mycut" method="doBasicProfiling"/>
</aop:aspect>
</aop:config>
-->
</beans>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.