spring2 aop 例子

2024-01-01 20:38
文章标签 例子 aop spring2

本文主要是介绍spring2 aop 例子,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

记录下AOP编程常用情况,调整格式,方便阅读

首先建立个被切入的类

package  sinlff.aop;

/**
 * 
@author  sinlff
 * 人实体
 
*/
public   class  Person {
    
/**
     * 年龄
     
*/
    
private   int  age;
    
    
/**
     * 姓名
     
*/
    
private  String name;

    
public   void  birthday(String name)  throws  Exception {
        
throw   new  Exception( " birthday's Exception Message...... " );
    }
    
    
/** 返回年龄
     * 
@return  the age
     
*/
    
public   int  getAge() {
        
return  age;
    }

    
/** 设置年龄
     * 
@param  age the age to set
     
*/
    
public   void  setAge( int  age) {
        
this .age  =  age;
    }

    
/** 返回姓名
     * 
@return  the name
     
*/
    
public  String getName() {
        
return  name;
    }
    
/** 设置姓名
     * 
@param  string the name to set
     
*/
    
public   void  setName(String string) {
        
this .name  =  string;
    }

}

 

要切入Person类的 birthday()方法的类

 

package  sinlff.aop;

public   class  BirthdayExecute {

    
/** 当Person类里的birthday执行抛出异常,则执行的函数
     * 
@param  Exception exception类的对象
     
*/
    
public   void  throwExceptionBirthday(Exception exception)  throws  Exception{  
        System.out.println( exception.getMessage() 
+   "   throwExceptionBirthday execute...... " );
    }
    
    
/** 当Person类里的birthday执行之前,则执行的函数
     * 
@param  name birthday函数的字符串参数
     
*/
     
public   void  beforeBirthday(String name) { 
         System.out.println(name
+ "  beforeBirthday execute...... " );
     }
     
    
/** 当Person类里的birthday执行之后,则执行的函数
     * 
@param  person Person类的对象
     
*/
    
public   void  afterBirthday(Person person) {   
        System.out.println(person.getName() 
+ "   afterBirthday execute...... " );
    }
}

 

配置文件

 

 

 

<? 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: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.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"

       default-autowire
="byName"  default-lazy-init ="true" >
  
< aop:aspectj-autoproxy />
 
    
<!--  被切入的一般操作类  -->
    
< bean  id ="person"  class ="sinlff.aop.Person" >
    
</ bean >
    
    
<!--  切入的类  -->
    
< bean  id ="birthdayExecute"  class  = "sinlff.aop.BirthdayExecute" >
    
</ bean >

    
< tx:advice  id ="txAdvice"
        transaction-manager
="transactionManager" >
        
< tx:attributes >
            
< tx:method  name ="birth*"  read-only ="true"   />
            
< tx:method  name ="find*"  read-only ="true" />   
            
< tx:method  name ="get*"  read-only ="true" />   
            
< tx:method  name ="list*"  read-only ="true" />  
            
< tx:method  name ="*"  rollback-for ="Exception" />
        
</ tx:attributes >
    
</ tx:advice >

        
< aop:config >
            
< aop:pointcut  id ="birthday1"
                    expression
="execution(* sinlff.aop.Person.birth*(..)) and args(name)"   />
            
< aop:pointcut  id ="birthday2"
                    expression
="execution(* sinlff.aop.Person.birth*(..)) and this(person)"   />
            
< aop:pointcut  id ="birthday3"
                    expression
="execution(* sinlff.aop.Person.birth*(..))"   />
             
             
            
< aop:advisor  advice-ref ="txAdvice"  pointcut-ref ="birthday1"   />      
                     
            
< aop:aspect  id ="personBirthday"  ref ="birthdayExecute" >
                
< aop:before  pointcut-ref ="birthday1"
                    method
="beforeBirthday"  arg-names ="name"   />  
                
< aop:after  pointcut-ref ="birthday2"
                    method
="afterBirthday"  arg-names ="person"   />
                
< aop:after-throwing  pointcut-ref ="birthday3"
                    method
="throwExceptionBirthday"  throwing ="exception"   />
            
</ aop:aspect >
        
</ aop:config >
        

    
< bean  id ="Datesource"
        class
="org.apache.commons.dbcp.BasicDataSource" >
              
< property  name ="driverClassName"
                  value
="oracle.jdbc.driver.OracleDriver"   />
              
< property  name ="url"  value ="jdbc:oracle:thin:@localhost:1521:sinlff"   />
              
< property  name ="username"  value ="sinlff"   />
              
< property  name ="password"  value ="123456"   />
              
< property  name ="maxActive" >
                  
< value > 100 </ value >
        
</ property >
    
</ bean >
    
< bean  id ="SessionFactory"
        class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
        
< property  name ="dataSource" >
            
< ref  bean ="Datesource"   />
        
</ property >
        
< property  name ="hibernateProperties" >
            
< props >
                
< prop  key ="hibernate.dialect" >
                    org.hibernate.dialect.Oracle9Dialect
                
</ prop >
            
</ props >
        
</ property >
        
< property  name ="mappingResources" >
            
< list >
            
</ list >
        
</ property >
    
</ bean >
    
    
< bean  id ="transactionManager"  class ="org.springframework.orm.hibernate3.HibernateTransactionManager" >    
        
< property  name ="sessionFactory" >
            
< ref  bean ="SessionFactory"   />
        
</ property >
    
</ bean >
 
</ beans >

 测试

package  sinlff.test;

import  org.springframework.context.ApplicationContext;
import  org.springframework.context.support.ClassPathXmlApplicationContext;

import  sinlff.aop.Person; 
 

import  junit.framework.TestCase;

public   class  TestAOP  extends  TestCase {

    
private  Person person;
    
    
public   void  setUp(){
        ApplicationContext applicationContext 
=   new  ClassPathXmlApplicationContext( " applicationContext.xml " );
        person 
=  (Person) applicationContext.getBean( " person " );    
    }
    
    
public   void  testSendCardAOP()  throws  Exception {
        person.setName(
" sinlff " );
        person.setAge(
22 );
        person.birthday(
" new name " );
    }
}

 

 

 

暂时只给出完整例子,因为我查到的资料都很零散,而且格式排版都很难看,内容给的不完整,导致学习障碍 

 

 

 

 

 

 

这篇关于spring2 aop 例子的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/560452

相关文章

SpringBoot实现动态插拔的AOP的完整案例

《SpringBoot实现动态插拔的AOP的完整案例》在现代软件开发中,面向切面编程(AOP)是一种非常重要的技术,能够有效实现日志记录、安全控制、性能监控等横切关注点的分离,在传统的AOP实现中,切... 目录引言一、AOP 概述1.1 什么是 AOP1.2 AOP 的典型应用场景1.3 为什么需要动态插

JavaFX环境的搭建和一个简单的例子

之前在网上搜了很多与javaFX相关的资料,都说要在Eclepse上要安装sdk插件什么的,反正就是乱七八糟的一大片,最后还是没搞成功,所以我在这里写下我搭建javaFX成功的环境给大家做一个参考吧。希望能帮助到你们! 1.首先要保证你的jdk版本能够支持JavaFX的开发,jdk-7u25版本以上的都能支持,最好安装jdk8吧,因为jdk8对支持JavaFX有新的特性了,比如:3D等;

javaScript日期相加减例子

当前时间加上2天 var d = new Date(“2015-7-31”); d.setDate(d.getDate()+2); var addTwo=d.getFullYear()+”年”+(d.getMonth()+1)+”月”+d.getDate()+”日”; “控制台输出===============”+”当前日期加2天:”+addTwo; 使用这种方法,月份也会给你计算.

SSM项目使用AOP技术进行日志记录

本步骤只记录完成切面所需的必要代码 本人开发中遇到的问题: 切面一直切不进去,最后发现需要在springMVC的核心配置文件中中开启注解驱动才可以,只在spring的核心配置文件中开启是不会在web项目中生效的。 之后按照下面的代码进行配置,然后前端在访问controller层中的路径时即可观察到日志已经被正常记录到数据库,代码中有部分注释,看不懂的可以参照注释。接下来进入正题 1、导入m

SpringBoot中利用EasyExcel+aop实现一个通用Excel导出功能

一、结果展示 主要功能:可以根据前端传递的参数,导出指定列、指定行 1.1 案例一 前端页面 传递参数 {"excelName": "导出用户信息1725738666946","sheetName": "导出用户信息","fieldList": [{"fieldName": "userId","fieldDesc": "用户id"},{"fieldName": "age","fieldDe

设计模式大全和详解,含Python代码例子

若有不理解,可以问一下这几个免费的AI网站 https://ai-to.cn/chathttp://m6z.cn/6arKdNhttp://m6z.cn/6b1quhhttp://m6z.cn/6wVAQGhttp://m6z.cn/63vlPw 下面是设计模式的简要介绍和 Python 代码示例,涵盖主要的创建型、结构型和行为型模式。 一、创建型模式 1. 单例模式 (Singleton

JSP 简单表单显示例子

<html><!--http://localhost:8080/test_jsp/input.html --><head><meta http-equiv="Content-Type" content="text/HTML; charset=utf-8"><title>input页面</title></head><body><form action="input.jsp" method

Spring6详细学习笔记(IOC+AOP)

一、Spring系统架构介绍 1.1、定义 Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器(框架)。Spring官网 Spring是一款主流的Java EE 轻量级开源框架,目的是用于简化Java企业级引用的开发难度和开发周期。从简单性、可测试性和松耦合度的角度而言,任何Java应用都可以从Spring中受益。Spring框架提供自己提供功能外,还提供整合其他技术和框架

shell循环sleep while例子 条件判断

i=1# 小于5等于时候才执行while [ ${i} -le 5 ]doecho ${i}i=`expr ${i} + 1`# 休眠3秒sleep 3doneecho done 参考 http://c.biancheng.net/cpp/view/2736.html

【ReactJS】通过一个例子学习React组件的生命周期

源代码 <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Reac