Spring | IOC AOP 註解 簡單使用

寫在前面的話

好久沒更新筆記了,有人會抱怨:小馮啊,你是否是在偷懶啊,沒有學習了。老哥,真的冤枉:我以爲我本身很菜,還在努力學習呢,正在學習Vue.js作管理系統呢。即使這樣,我仍是不忘更新Spring的知識,這不就來了嗎?java

IOC

我想把類交給Spring,讓他幫我建立對象,這應該怎麼作?git

一、類github

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
}

二、Controllerweb

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";
    }

}

三、瀏覽器訪問spring

http://localhost:8080/test/hello瀏覽器

四、控制檯 Say Helloapp

五、關於測試ide

有人說,測試時這樣寫的嗎?學習

不!我只是喜歡這樣,僅此而已。測試

AOP

一、業務

在這裏,我用 @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("吃飯了");
    }

}

二、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("吃飯以後,擦嘴");
    }

}

三、測試代碼

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";
    }

}

四、瀏覽器請求

http://localhost:8080/test/eat

五、控制檯

Spring AOP

註解

一、註解

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();
}

二、接口

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

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

    String name();

    String age();

    @Deprecated
    void sind();

}

三、寫一個類實現接口,並使用註解

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() {

    }
}

四、註解解析

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();
        }

    }

}

五、效果

註解 解析

資料

本節代碼已上傳至Github,點擊下面的工程名,便可進入:JavaLearnProject

相關文章
相關標籤/搜索