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>  





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值