Simple Spring AOP demo

Source: Internet
Author: User

1. Add the JAR package and generate some dependent Jar packages for the JAR package of Spring, otherwise ClassNotFound will be reported.

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

 
 
  
  Aspect ref = "something"> --- execute the borrowBook method before running the readBook Method
 

Execution result

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




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.