Spring 使用JDBC

---恢复内容开始---

注解配置AOP

  项目路径:E:\JavaWebSrc\FirstSpringAOP

  1:接口代码 

    接口为 IPerson ,接口不需要写注释

public interface IPerson {
    public void sayNmae();
    public void introduceOneSelf();
}

  2:实体类代码

     student继承了IPerson接口 

//将student实例化。装配到spring容器中
@Component("student") public class Student implements IPerson { @Value(value = "1") private int id; @Value(value = "余文辉") private String name; @Value(value = "") private String sex; @Override public void sayNmae() { System.out.println(this.name); } @Override public void introduceOneSelf() { System.out.println(this.toString()); } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", sex='" + sex + '\'' + '}'; } }

  3:切面类

    这里使用的注解需要导入(aopaliance.jar和  aspectjweaver.jar 两个jar包) 

//切面声明
@Aspect
//装配到spring容器中
@Component
public class LoggingAspect {
//    声明切入点  声明切入点表达式
    @Pointcut("execution(* com.yuwenhui.annotation.Student.* (..))")
    public void JoinPointExpecssion(){};
//前置通知
    @Before("JoinPointExpecssion()")
    public void beforMethod(JoinPoint joinPoint){
//        获得方法名
        String name = joinPoint.getSignature().getName();
//        获得参数列表,再将参数数组转换成List集合
        List args = Arrays.asList(joinPoint.getArgs());
        System.out.println("前置通知");
        System.out.println("参数:"+args);

    }
//后置通知
    @After("JoinPointExpecssion()")
    public void afterMethod(JoinPoint joinPoint){
        String name = joinPoint.getSignature().getName();
        List args = Arrays.asList(joinPoint.getArgs());
        System.out.println("后置通知");
        System.out.println("参数:"+args);
    }
//    返回通知,在方法正常结束的时候返回,且 必须要有一个返回值
    @AfterReturning(value = "JoinPointExpecssion()",returning = "result")
    public void afterMethodRetuning(JoinPoint joinPoint,Object result){
        String name = joinPoint.getSignature().getName();
        List args = Arrays.asList(joinPoint.getArgs());
        System.out.println("返回通知"+ result);
    }
//    异常通知 ,将在方法抛出异常时触
    @AfterThrowing(value = "JoinPointExpecssion()",throwing ="e")
    public void  afterThrowing(JoinPoint joinPoint,Exception e){
        String name = joinPoint.getSignature().getName();
        System.out.println("返回通知");
        System.out.println("异常信息"+e);
    }
} 
   
  

  xml配置:


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




  测试类

public class TestAop {
    @Test
    public void testAop(){
        ApplicationContext applicationContext = new  ClassPathXmlApplicationContext("applicationContext.xml");
       IPerson student= (IPerson) applicationContext.getBean("student");
       student.sayNmae();
       student.introduceOneSelf();
    }
}

 

XML配置AOP

  1:接口配置

    不管是XML配置还是annotation配置,接口类都不需要改变

  

public interface IPerson {
    public void sayNmae();
    public void introduceOneSelf();
}

  2:实体类配置

    

public class Student implements IPerson {

    private int id;

    private String name;
    private String sex;

    @Override
    public void sayNmae() {
        System.out.println(this.name);
    }

    @Override
    public void introduceOneSelf() {
        System.out.println(this.toString());
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

  3:切面类

  

public class LoggingAspect {

    public void JoinPointExpecssion(){};

    public void beforMethod(JoinPoint joinPoint){
        String name = joinPoint.getSignature().getName();
        List args = Arrays.asList(joinPoint.getArgs());
        System.out.println("前置通知");
        System.out.println("参数:"+args);

    }

    public void afterMethod(JoinPoint joinPoint){
        String name = joinPoint.getSignature().getName();
        List args = Arrays.asList(joinPoint.getArgs());
        System.out.println("后置通知");
        System.out.println("参数:"+args);
    }

    public void afterMethodRetuning(JoinPoint joinPoint,Object result){
        String name = joinPoint.getSignature().getName();
        List args = Arrays.asList(joinPoint.getArgs());
        System.out.println("返回通知"+ result);
    }

    public void  afterThrowing(JoinPoint joinPoint,Exception e){
        String name = joinPoint.getSignature().getName();
        System.out.println("返回通知");
        System.out.println("异常信息"+e);
    }
} 
   
  

  4:测试类

    

public class TestXml {
    @Test
    public void testAop(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-beanxml.xml");
        IPerson student= (IPerson) applicationContext.getBean("student");
        student.sayNmae();
        student.introduceOneSelf();
    }
}

   5:配置文件

    需要格外注意   

"1.0" encoding="UTF-8"?>
"http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
    
    
    "student" class="com.yuwenhui.xml.Student">
        "name" value="余文辉">
        "id" value="1">
        "sex" value="">
    
    
    "loggingAspect" class="com.yuwenhui.xml.LoggingAspect">
    
    
        
        "pointcut" expression="execution(* com.yuwenhui.xml.Student.* (..))"/>
        
        ref="loggingAspect">
            
            "beforMethod" pointcut-ref="pointcut"/>
            
            "afterMethod" pointcut-ref="pointcut"/>
            
            "afterMethodRetuning" pointcut-ref="pointcut" returning="result"/>
            
            "afterThrowing" pointcut-ref="pointcut" throwing="e"/>
        
    

 

---恢复内容结束---

你可能感兴趣的:(Spring 使用JDBC)