zoukankan      html  css  js  c++  java
  • Spring AOP 创建Advice 基于Annotation

    public interface IHello {
      public void  sayHello(String str);
    }
    public class Hello implements IHello {
    
    	@Override
    	public void sayHello(String str) {
    		// TODO Auto-generated method stub
    	   System.out.println("你好"+str);
    	}
    
    }
    

      

    aspectBean.java

    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    
    //引入Aspect注解,声明切面
    @Aspect
    public class aspectBean {
    	//定义为切入点
    	@Pointcut("execution(* hello.*(..))")
    	public void log() {
    		
    	}
    	@Before(value="log()") //在切入点之前执行
    	public void startLog()
    	{
    		System.out.println("开始记录");
    	}
    	@After(value="log()") //在切入点之后执行
    	public void endLog()
    	{
    		System.out.println("结束记录");
    	}
    }
    

      applicationContext.xml

    <?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:p="http://www.springframework.org/schema/p"
    	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">
    	
    	<!-- 启动aspectj -->
    	<aop:aspectj-autoproxy/>
    	<bean id="apbean" class="com.pb.aspectBean"></bean>
    	<bean id="he" class="com.pb.Hello"></bean>
    </beans>
    

      Maintest文件:

    public static void main(String[] args) {
    		// TODO Auto-generated method stub
            ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
            
            IHello hello=(IHello)context.getBean("he");
            hello.sayHello("访客");
    	}
    

      执行:

  • 相关阅读:
    Query实例的ajax应用之二级联动的后台是采用php来做的
    关于jquery的css的一些知识
    这个代码给所有带有name属性的链接加了一个背景色
    jQuery生成一个DIV容器,ID是"rating".
    被included或者被required的文件都来自哪里呢
    msql_createdb: 建立一个新的 mSQL 数据库。
    php中调用这个功能可以在web页面中显示hello world这个经典单词
    啦啦啦测试心得
    maven命令
    robotframework使用
  • 原文地址:https://www.cnblogs.com/schangxiang/p/11149067.html
Copyright © 2011-2022 走看看