zoukankan      html  css  js  c++  java
  • Spring之AOP简单demo

    1.加入JAR包。出了Spring自身的Jar包还要一些依赖的JAR包。不然会报ClassNotFound。

    Student.java

    package com.lubby.bean;
    
    import org.springframework.stereotype.Component;
    @Component("student")
    public class Student {
    	private String id;
    	private String name;
    
    	public Student() {
    		super();
    	}
    	public Student(String id, String name) {
    		super();
    		this.id = id;
    		this.name = name;
    	}
    	public void readBook() {
    		System.out.println("I am reading book now.....");
    	}
    	public String getId() {
    		return id;
    	}
    	public void setId(String id) {
    		this.id = id;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    }
    

    Something.java

    package com.lubby.bean;
    
    import org.springframework.stereotype.Component;
    
    @Component("something")
    public class Something {
    	public void borrowBook() {
    		System.out.println("Borrow the book from library......");
    	}
    	public void returnBook() {
    		System.out.println("Return the book to library........");
    	}
    }
    

    test.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:p="http://www.springframework.org/schema/p"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
    	default-lazy-init="true"
    	xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        ">
    
    	<!-- 自己主动检測并注冊为spring中的bean+启用注解 -->
    	<context:component-scan base-package="com.lubby.bean"></context:component-scan>
    	<aop:config>
    		<aop:aspect ref="something">
    			<aop:pointcut expression="execution(* com.lubby.bean.Student.readBook(..))"  --定义切点为readBook方法
    				id="readBook" />  
    			<aop:before pointcut-ref="readBook" method="borrowBook" />---在运行readBook方法之前运行borrowBook方法
    			<aop:after-returning pointcut-ref="readBook"    ----在运行readBook方法之后运行returnBook
    				method="returnBook" />
    		</aop:aspect>
    	</aop:config>
    </beans>

    运行结果

    Borrow the book from library......
    I am reading book now.....
    Return the book to library........
    




查看全文
  • 相关阅读:
    unity3d进程通信利用WM_COPYDATE和HOOK
    java解析XML saxReader.read(xml) 错误:org.dom4j.DocumentException: no protocol
    Linux下永久改动MAC地址和ifconfig命令总结
    Android-Universal-Image-Loader学习笔记(3)--内存缓存
    图片3d轮放查看效果
    Hadoop-eclipse-plugin插件安装
    Hadoop格式化HDFS报错java.net.UnknownHostException: localhost.localdomain: localhost.localdomain
    linux出现bash: ./java: cannot execute binary file 问题的解决办法
    分析windows宿主机Ping不通linux虚拟机的其中一种情况
    http://jingyan.baidu.com/article/08b6a591f0fafc14a9092275.html
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10906125.html
  • Copyright © 2011-2022 走看看