第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定

Spring | IOC AOP 注解 簡單使用

標(biāo)簽:
Java SpringBoot
写在前面的话

很久没更新笔记了,有人会抱怨:小冯啊,你是不是在偷懒啊,没有学习了。老哥,真的冤枉:我觉得我自己很菜,还在努力学习呢,正在学习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

點擊查看更多內(nèi)容
1人點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
JAVA開發(fā)工程師
手記
粉絲
1.4萬
獲贊與收藏
707

關(guān)注作者,訂閱最新文章

閱讀免費教程

感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊有機(jī)會得

100積分直接送

付費專欄免費學(xué)

大額優(yōu)惠券免費領(lǐng)

立即參與 放棄機(jī)會
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號

舉報

0/150
提交
取消