- package com.aop;
- import java.util.Date;
- interface Hello {
- public void sayHello(String name);
- }
- class MyHello implements Hello{
- @Override
- public void sayHello(String name) {
- System.out.println("Hello" +name);
- }
- }
- class HelloProxy implements Hello {
- private Hello hello;
- public HelloProxy(Hello hello){
- this.hello = hello;
- }
- @Override
- public void sayHello(String name) {
- Logger.logging(Level.DEBUGE, "sayHello() method start!");
- hello.sayHello(name);
- Logger.logging(Level.INFO, "sayHello() method end!");
- }
- }
- enum Level {
- INFO,DEBUGE;
- }
- class Logger {
- public static void logging(Level level, String context) {
- if (level.equals(Level.INFO)) {
- System.out.println(new Date()+ " " + context);
- }
- if (level.equals(Level.DEBUGE)) {
- System.out.println(new Date() + " " + context);
- }
- }
- }
- public class AopTester {
- public static void main(String[] args) {
- Hello helloProxy = new HelloProxy(new MyHello());
- helloProxy.sayHello(" Spring AOP!!");
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/180620134094.html
来源: http://www.codesnippet.cn/detail/180620134094.html