Spring | IOC AOP 注解 简单使用

写在前面的话

很久没更新笔记了,有人会抱怨:小冯啊,你是不是在偷懒啊,没有学习了。老哥,真的冤枉:我觉得我自己很菜,还在努力学习呢,正在学习Vue.js做管理系统呢。即便这样,我还是不忘更新Spring的知识,这不就来了吗?

IOC

我想把类交给Spring,让他帮我创建对象,这应该怎么做?

1、类

package com.fengwenyi.learn.java.springioc;

import org.springframework.stereotype.Component;

/**
 * @author Wenyi Feng
 */
@Component
public class Person {
    
    private String name;

    public Person() {}

    public void sayHello() {
        System.out.format("%s说:Hello.", name);
    }

    // getter and setter
}

2、Controller

package com.fengwenyi.learn.java.springioc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Wenyi Feng
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private Person person;

    @GetMapping("/hello")
    public String hello() {
        person.setName("Wenyi Feng");
        person.sayHello();
        return "s";
    }

}

3、浏览器访问

http://localhost:8080/test/hello

4、控制台 Say Hello

5、关于测试

有人说,测试时这样写的吗?

不!我只是喜欢这样,仅此而已。

AOP

1、业务

在这里,我用 @Service 代表我们要处理的业务: 吃饭

package com.fengwenyi.learn.java.springaop.service;

import org.springframework.stereotype.Service;

/**
 * @author Wenyi Feng
 */
@Service
public class EatService {

    public void eat() {
        System.out.println("吃饭了");
    }

}

2、AOP

分析:吃饭之前我们需要洗手,吃饭之后我们要擦嘴

package com.fengwenyi.learn.java.springaop;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @author Wenyi Feng
 */
@Component
@Aspect
public class Clean {

    // @Pointcut("execution(* com.fengwenyi.learn.java.springaop.service..*.*(..))")
    @Pointcut("execution(* com.fengwenyi.learn.java.springaop.service.EatService.eat())")
    public void eat() {
    }

    /**
     * 方法执行之前
     */
    @Before("eat()")
    public void doBefore() {
        System.out.println("吃饭之前,吃手");
    }

    /**
     * 方法执行之后
     */
    @After("eat()")
    public void doAfter() {
        System.out.println("吃饭之后,擦嘴");
    }

}

3、测试代码

package com.fengwenyi.learn.java.springaop;

import com.fengwenyi.learn.java.springaop.service.EatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Wenyi Feng
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private EatService eatService;

    @GetMapping("/eat")
    public String eat() {
        eatService.eat();
        return "s";
    }

}

4、浏览器请求

http://localhost:8080/test/eat

5、控制台

Spring AOP

注解

1、注解

package com.fengwenyi.learn.java.restructure.ann;

import java.lang.annotation.*;

/**
 * @author Wenyi Feng
 */
@Target({ElementType.METHOD, ElementType.TYPE}) // 方法 / 类 或者 接口 / [filed 字段]
@Retention(RetentionPolicy.RUNTIME) // 运行时
@Inherited // extends class 有效(接口 抽象类 都无效)
@Documented
public @interface Description {

    String value();
}

2、接口

package com.fengwenyi.learn.java.restructure.ann;

/**
 * @author Wenyi Feng
 */
public interface Persion {

    String name();

    String age();

    @Deprecated
    void sind();

}

3、写一个类实现接口,并使用注解

package com.fengwenyi.learn.java.restructure.ann;

/**
 * @author Wenyi Feng
 */
@Description("Class Ann")
public class Child implements Persion {

    @Override
    @Description("Method Ann")
    public String name() {
        return null;
    }

    @Override
    public String age() {
        return null;
    }

    @Override
    public void sind() {

    }
}

4、注解解析

package com.fengwenyi.learn.java.restructure.ann;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * @author Wenyi Feng
 */
public class PerseAnn {

    public static void main(String[] args) {

        try {
            // 使用类加载器加载类
            Class c = Class.forName("com.fengwenyi.learn.java.restructure.ann.Child");

            // 找到类上的注解
            boolean isExistClassAnn = c.isAnnotationPresent(Description.class);
            if (isExistClassAnn) {
                // 拿到注解实例
                Description d = (Description) c.getAnnotation(Description.class);
                System.out.println(d.value());
            }

            // 找到方法上的注解
            Method[] methods = c.getMethods();
            for (Method method : methods) {
                boolean isExistMethodAnn = method.isAnnotationPresent(Description.class);
                if (isExistMethodAnn) {
                    Description d = method.getAnnotation(Description.class);
                    System.out.println(d.value());
                }
            }

            // 另一种解析方法
            for (Method method : methods) {
                Annotation [] annotations = method.getAnnotations();
                for (Annotation annotation : annotations) {
                    if (annotation instanceof Description) {
                        Description d = (Description) annotation;
                        System.out.println(d.value());
                    }
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

}

5、效果

注解 解析

资料

本节代码已上传至Github,点击下面的工程名,即可进入:JavaLearnProject

转载于:https://my.oschina.net/fengwenyi/blog/1834389

猜你喜欢

转载自blog.csdn.net/weixin_34150503/article/details/92049154