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




	
	
	
		
			  
			---在运行readBook方法之前运行borrowBook方法
			
		
	

运行结果

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




转载于:https://www.cnblogs.com/ldxsuanfa/p/10906125.html

你可能感兴趣的:(Spring之AOP简单demo)