admin před 1 rokem
rodič
revize
c1b2631d96

+ 69 - 0
iocSpringBootaop/pom.xml

@@ -0,0 +1,69 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>org.example</groupId>
+    <artifactId>iocSpringBootaop</artifactId>
+    <version>1.0-SNAPSHOT</version>
+
+    <properties>
+        <maven.compiler.source>8</maven.compiler.source>
+        <maven.compiler.target>8</maven.compiler.target>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+    <parent>
+        <groupId>org.springframework.boot</groupId>
+        <artifactId>spring-boot-starter-parent</artifactId>
+        <version>2.2.2.RELEASE</version>
+    </parent>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>fastjson</artifactId>
+            <version>1.2.78</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-aop</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>cn.hutool</groupId>
+            <artifactId>hutool-all</artifactId>
+            <version>5.8.16</version>
+        </dependency>
+
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <source>1.8</source>
+                    <target>1.8</target>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+</project>

+ 57 - 0
iocSpringBootaop/src/main/java/org/example/AOPAspect.java

@@ -0,0 +1,57 @@
+package org.example;
+
+import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.date.TimeInterval;
+import lombok.extern.slf4j.Slf4j;
+import org.aspectj.lang.JoinPoint;
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.*;
+import org.springframework.stereotype.Component;
+
+@Slf4j
+@Aspect
+@Component
+public class AOPAspect {
+
+    @Pointcut("execution(public * org.example.controller..*.*(..) throws Exception)")
+    public void pointcut() {
+    }
+
+    @Before("pointcut()")
+    public void before(JoinPoint point){
+        log.info("前置通知");
+    }
+
+    @After("pointcut()")
+    public void after(JoinPoint point) {
+        log.info("后置通知");
+    }
+
+    @Around("pointcut()")
+    public Object around(ProceedingJoinPoint joinPoint) {
+        log.info("环绕通知");
+        try {
+            // 计时
+            TimeInterval timer = DateUtil.timer();
+            // 执行方法,连接点
+            Object result = joinPoint.proceed();
+            // 查看耗时
+            log.info("耗时:{}", timer.interval());
+            return result;
+        } catch (Throwable throwable) {
+            return ResultData.fail("服务器繁忙,请稍后再试");
+        }
+    }
+
+    @AfterReturning("pointcut()")
+    public void afterReturning(JoinPoint point) {
+        log.info("返回通知");
+    }
+
+    @AfterThrowing(value = "pointcut()", throwing = "t")
+    public void afterThrowing(JoinPoint point, Throwable t) {
+        log.info("异常通知");
+    }
+
+}
+

+ 17 - 0
iocSpringBootaop/src/main/java/org/example/BeanIOC.java

@@ -0,0 +1,17 @@
+package org.example;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+public class BeanIOC {
+    //获取配置参数
+    @Bean(name="configurationBean")
+    public ConfigurationBean getConfigBean(){
+        ConfigurationBean configBean = new ConfigurationBean();
+        configBean.setServer("127.0.0.1");
+        configBean.setPort(8081);
+        configBean.setStyle("通过bean注解装配到IOC容器");
+        return configBean;
+    }
+}

+ 40 - 0
iocSpringBootaop/src/main/java/org/example/ComponentIOC.java

@@ -0,0 +1,40 @@
+package org.example;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+@Component("componentIOC")
+public class ComponentIOC {
+    @Value("127.0.0.1")
+    private String host;
+
+    @Value("8082")
+    private int port;
+
+    @Value("通过Component注解扫描注入bean到IOC容器")
+    private String style = "";
+
+    public String getHost() {
+        return host;
+    }
+
+    public void setHost(String host) {
+        this.host = host;
+    }
+
+    public int getPort() {
+        return port;
+    }
+
+    public void setPort(int port) {
+        this.port = port;
+    }
+
+    public String getStyle() {
+        return style;
+    }
+
+    public void setStyle(String style) {
+        this.style = style;
+    }
+}

+ 43 - 0
iocSpringBootaop/src/main/java/org/example/ComponentScanIOC.java

@@ -0,0 +1,43 @@
+package org.example;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+@ComponentScan
+public class ComponentScanIOC {
+    @Value("127.0.0.1")
+    private String host;
+
+    @Value("8083")
+    private int port;
+
+    @Value("通过ComponentScan注解扫描注入bean到IOC容器")
+    private String style = "";
+
+    public String getHost() {
+        return host;
+    }
+
+    public void setHost(String host) {
+        this.host = host;
+    }
+
+    public int getPort() {
+        return port;
+    }
+
+    public void setPort(int port) {
+        this.port = port;
+    }
+
+    public String getStyle() {
+        return style;
+    }
+
+    public void setStyle(String style) {
+        this.style = style;
+    }
+}
+

+ 33 - 0
iocSpringBootaop/src/main/java/org/example/ConfigurationBean.java

@@ -0,0 +1,33 @@
+package org.example;
+
+// A normal bean
+
+public class ConfigurationBean {
+    private String server = "";
+    private int port;
+    private String style = "";
+
+    public String getServer() {
+        return server;
+    }
+
+    public void setServer(String server) {
+        this.server = server;
+    }
+
+    public int getPort() {
+        return port;
+    }
+
+    public void setPort(int port) {
+        this.port = port;
+    }
+
+    public String getStyle() {
+        return style;
+    }
+
+    public void setStyle(String style) {
+        this.style = style;
+    }
+}

+ 44 - 0
iocSpringBootaop/src/main/java/org/example/IOCController.java

@@ -0,0 +1,44 @@
+package org.example;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@RequestMapping(value = "/ioc")
+public class IOCController {
+    @Autowired
+    private BeanIOC beanIOC;
+
+    @Autowired
+    private ComponentIOC componentIOC;
+
+    @Autowired
+    private ComponentScanIOC componentScanIOC;
+
+    @RequestMapping(value = "/beanStyle", method = RequestMethod.GET)
+    public String beanStyle() {
+        return JSON.toJSONString(beanIOC.getConfigBean());
+    }
+
+    @RequestMapping(value = "/componentStyle", method = RequestMethod.GET)
+    public String componentStyle() {
+        return JSON.toJSONString(componentIOC);
+    }
+
+    @RequestMapping(value = "/componentScanStyle", method = RequestMethod.GET)
+    public String componentScanStyle() {
+        return JSON.toJSONString(componentScanIOC);
+        // 或者
+//        JSONObject jsonObject = new JSONObject();
+//        jsonObject.put("host", componentScanIOC.getHost());
+//        jsonObject.put("port", componentScanIOC.getPort());
+//        jsonObject.put("style", componentScanIOC.getStyle());
+//        return  jsonObject.toJSONString();
+    }
+
+}
+

+ 13 - 0
iocSpringBootaop/src/main/java/org/example/Main.java

@@ -0,0 +1,13 @@
+package org.example;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class Main {
+    public static void main(String[] args){
+        SpringApplication.run(Main.class, args);
+    }
+}
+
+// http://localhost:8080/hello?name=xxx vs http://localhost:8080/hello?name=

+ 29 - 0
iocSpringBootaop/src/main/java/org/example/ResultData.java

@@ -0,0 +1,29 @@
+package org.example;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+public class ResultData<T> {
+
+    private Integer errCode;
+
+    private String errMsg;
+
+    private T data;
+
+    public static ResultData success(){
+        return new ResultData(0, "", null);
+    }
+
+    public static ResultData fail(String errMsg){
+        return new ResultData(-1, errMsg, null);
+    }
+
+    public static <T> ResultData success(T t) {
+        return new ResultData(0, "成功", t);
+    }
+}

+ 20 - 0
iocSpringBootaop/src/main/java/org/example/controller/AOPController.java

@@ -0,0 +1,20 @@
+package org.example.controller;
+
+import lombok.extern.slf4j.Slf4j;
+import org.example.ResultData;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import cn.hutool.core.util.StrUtil;
+import org.springframework.web.bind.annotation.RestController;
+
+@Slf4j
+@RestController
+public class AOPController {
+    @RequestMapping("/hello")
+    public ResultData<String> hello(@RequestParam(required = false) String name) throws Exception {
+        if(StrUtil.isBlank(name))
+            throw new UnsupportedOperationException("无名,不允许操作");
+        log.info("模拟业务操作:{}", name);
+        return ResultData.success(name);
+    }
+}