zoukankan      html  css  js  c++  java
  • Spring入门---示例五---通过程序代码实现AOP【第二天】

    Spring通过程序代码实现AOP示例

    一、编写用户DAO类:

     1 package test5.aop.program;
     2 
     3  
     4 
     5 //用户DAO类
     6 
     7 public class UserDao {
     8 
     9 public void addUser(String uname,String upwd){
    10 
    11 System.out.println("addUser()");
    12 
    13 }
    14 
    15 public void deleteUser(String uname) {
    16 
    17 System.out.println("deleteUser()");
    18 
    19 }
    20 
    21 }
    22 
    23  

    二、编写检查用户安全的类:

     1 package test5.aop.program;
     2 
     3  
     4 
     5 import org.aspectj.lang.annotation.After;
     6 
     7 import org.aspectj.lang.annotation.Pointcut;
     8 
     9  
    10 
    11 //检查用户安全的类
    12 
    13 public class CheckSecurity {
    14 
    15 /**声明一个切入点 当调用以add以及delete串开始的函数时**/
    16 
    17 @Pointcut("execution(* add*(..)||execution(* delete*(..)))")
    18 
    19 private void allAddMethod(){}
    20 
    21  
    22 
    23 @After("allAddMethod()")
    24 
    25 private void check(){
    26 
    27 System.out.println("check()");
    28 
    29 }
    30 
    31 }

    三、改写Spring配置文件:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 
     3 <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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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">
     4 
     5     <!-- 以下是设置自动AOP。即通过程序设置AOP -->
     6 
     7         <aop:aspectj-autoproxy/>
     8 
     9         <bean name="check1" class="test5.aop.program.CheckSecurity"/>
    10 
    11         <bean name="dao1" class="test5.aop.program.UserDao"/>
    12 
    13 </beans>

    四、编写测试类:

     1 package test5;
     2 
     3  
     4 
     5 import org.springframework.beans.factory.BeanFactory;
     6 
     7 import org.springframework.context.support.ClassPathXmlApplicationContext;
     8 
     9  
    10 
    11 import test5.aop.program.UserDao;
    12 
    13  
    14 
    15 public class Test {
    16 
    17 @SuppressWarnings("resource")
    18 
    19 public static void main(String[] args) {
    20 
    21 BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
    22 
    23 UserDao dao =(UserDao)factory.getBean("dao1");
    24 
    25 dao.addUser("zhou", "1234");
    26 
    27 }
    28 
    29 }

     在编写过程中,配置文件由于没改和本文一致,而是包含前面写的代码,最后出现调用前面写的示例的代码,请思考,

    尝试将本文提到的配置加入前面的项目。

  • 相关阅读:
    JavaScript 将十进制数转换成格式类似于 0x000100 或 #000100 的十六进制数
    Java 从资源文件(.properties)中读取数据
    更改MySQL 5.7的数据库的存储位置
    PADS Layout VX.2.3 将PCB中的元器件封装保存到库
    怀疑安装MySQL之后,导致OrCAD Capture、Allegro就打不开
    Allegro PCB 转 PADS Layout 之后的修修补补
    Java 使用UDP传输一个小文本文件
    IntelliJ IDEA Commons IO环境搭建
    RestFul风格API(Swagger)--从零开始Swagger
    Spring容器的简单实现(IOC原理)
  • 原文地址:https://www.cnblogs.com/ciscolee/p/10938433.html
Copyright © 2011-2022 走看看