zoukankan      html  css  js  c++  java
  • Spring中实现AOP 迎客

    AspectJ介绍:

    AspectJ是一个面向切面的框架,它扩展了Java语言。

    AspectJ定义了AOP语法所以它有一个专门的编译器用来生成遵守Java字节编码规范的Class文件.

    Spring 可以使用了和AspectJ 5一样的注解,并使用AspectJ来做切入点解析和匹配。

    但是,AOP在运行时仍旧是纯的Spring AOP,并不依赖于AspectJ的编译器或者织入器(weaver)。

    aspectJ下载:http://www.eclipse.org/aspectj/downloads.php

    采用AspectJ声明实现AOP:

    接口UserManager.java:

    1 package com.springdemo.manager;
    2
    3  public interface UserManager {
    4 public void add(String name,String password);
    5 public void delete(String name);
    6 public int count();
    7 }
    8  

    类UserManagerImpl.java:

    代码
    1 package com.springdemo.manager;
    2
    3
    4  public class UserManagerImpl implements UserManager {
    5
    6 public void add(String name, String password) {
    7 System.out.println(" do add user ... ");
    8 }
    9
    10 public void delete(String name) {
    11 System.out.println(" do delete user ... ");
    12 }
    13
    14 public int count() {
    15 return 10;
    16 }
    17 }
    18  

    切面UserManagerHandler.java:

    代码
    1 package com.springdemo.manager;
    2
    3  import org.aspectj.lang.annotation.Aspect;
    4  import org.aspectj.lang.annotation.Before;
    5  import org.aspectj.lang.annotation.Pointcut;
    6
    7 @Aspect
    8  public class UserManagerHandler {
    9
    10 /**
    11 * PointCut名,不能有返回值
    12 */
    13 @Pointcut("execution(* *.*(..))")
    14 public void pointCutMethods(){}
    15
    16 /**
    17 * 定义Advice
    18 */
    19 @Before("pointCutMethods()")
    20 public void checkPower() {
    21 System.out.println("check manager power ....");
    22 }
    23 }
    24  

    bean.xml:

    代码
    1 <beans xmlns="http://www.springframework.org/schema/beans"
    2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    3 xmlns:util="http://www.springframework.org/schema/util"
    4 xmlns:aop="http://www.springframework.org/schema/aop"
    5 xsi:schemaLocation="http://www.springframework.org/schema/beans
    6   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    7   http://www.springframework.org/schema/context
    8   http://www.springframework.org/schema/context/spring-context-3.0.xsd
    9   http://www.springframework.org/schema/util
    10 http://www.springframework.org/schema/util/spring-util-3.0.xsd
    11 http://www.springframework.org/schema/aop
    12 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    13
    14
    15 <aop:aspectj-autoproxy/>
    16 <bean id="UserManagerHandler" class="com.springdemo.manager.UserManagerHandler"/>
    17 <bean id="UserManagerImpl" class="com.springdemo.manager.UserManagerImpl"/>
    18 </beans>
    19

    测试代码:

    代码
    1 package com.springdemo.client;
    2
    3 import org.springframework.beans.factory.BeanFactory;
    4 import org.springframework.context.support.ClassPathXmlApplicationContext;
    5 import com.springdemo.manager.UserManager;
    6
    7 public class Client {
    8 public static void main(String[] args) {
    9 //Resource res = new ClassPathResource("bean.xml");
    10 //BeanFactory fac = new XmlBeanFactory(res);
    11 BeanFactory fac = new ClassPathXmlApplicationContext("bean.xml");
    12 UserManager userManager = (UserManager)fac.getBean("UserManagerImpl");
    13 userManager.add("pxl..", "pwd..");
    14 }
    15
    16 }
    17

    基于Schema的AOP实现:

    UserManager.java 和 UserManagerImpl.java 代码同上。

    切面UserManagerHandler.java:

    1 package com.springdemo.manager;
    2  public class UserManagerHandler {
    3 public void checkPower() {
    4 System.out.println("check manager power ....");
    5 }
    6 }
     

    配置Spring容器bean.xml:

    代码
    1 <beans xmlns="http://www.springframework.org/schema/beans"
    2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    3 xmlns:util="http://www.springframework.org/schema/util"
    4 xmlns:aop="http://www.springframework.org/schema/aop"
    5 xsi:schemaLocation="http://www.springframework.org/schema/beans
    6   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    7   http://www.springframework.org/schema/context
    8   http://www.springframework.org/schema/context/spring-context-3.0.xsd
    9   http://www.springframework.org/schema/util
    10   http://www.springframework.org/schema/util/spring-util-3.0.xsd
    11 http://www.springframework.org/schema/aop
    12 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    13
    14 <bean id="UserManagerHandler" class="com.springdemo.manager.UserManagerHandler"/>
    15 <bean id="UserManagerImpl" class="com.springdemo.manager.UserManagerImpl"/>
    16
    17 <aop:config>
    18 <aop:aspect id="UserMangerAspect" ref="UserManagerHandler">
    19 <aop:pointcut expression="execution(* *.*(..))" id="pointCutMethods"/>
    20 <aop:before method="checkPower" pointcut-ref="pointCutMethods" />
    21 </aop:aspect>
    22 </aop:config>
    23 </beans>

    测试代码:

    代码
    1 package com.springdemo.client;
    2
    3 import org.springframework.beans.factory.BeanFactory;
    4 import org.springframework.context.support.ClassPathXmlApplicationContext;
    5
    6 import com.springdemo.manager.UserManager;
    7
    8 public class Client {
    9 public static void main(String[] args) {
    10 //Resource res = new ClassPathResource("bean.xml");
    11 //BeanFactory fac = new XmlBeanFactory(res);
    12 BeanFactory fac = new ClassPathXmlApplicationContext("bean.xml");
    13 UserManager userManager = (UserManager)fac.getBean("UserManagerImpl");
    14 userManager.add("pxl..", "pwd..");
    15 }
    16
    17 }
  • 相关阅读:
    PostgreSQL数据库
    phpcms网站搬家 至 服务器 完整并且详细过程
    phpcms网页替换验证码功能 及 搜索功能
    用phpcms切换中英文网页的方法(不用解析二级域名)、phpcms完成pc和手机端切换(同一域名)
    php判断手机段登录,以及phpcms手机PC双模板调用
    php 手动搭建环境
    phpcms内容页替换
    phpcms列表页替换
    phpcms首页替换
    phpcms栏目点击选中
  • 原文地址:https://www.cnblogs.com/pengxl/p/1916096.html
Copyright © 2011-2022 走看看