spring-boot自定义注解,解析自定义注解的属性值,配置文件的参数值

3 篇文章 0 订阅
2 篇文章 0 订阅

1.依赖

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

2.配置文件

test.key=testValue

3.注解

package com.qin.annotation;

import java.lang.annotation.*;

@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AuthDemo {
    String value() default "";
}

4.切面AOP

package com.qin.aspectj;

import com.qin.annotation.AuthDemo;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class AuthDemoAspect implements EnvironmentAware {
    Environment environment;

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    @Pointcut("@annotation(com.qin.annotation.AuthDemo)")
    public void myPointCut() {

    }

    @Before(value = "myPointCut()")
    public void check(){
        System.out.println("check");
    }


    @After(value = "myPointCut()")
    public void bye(){
        System.out.println("bye");
    }

    /**
     *配置文件配置
     *test.key=testValue
     */
    @Around("myPointCut() && @annotation(authDemo)")
    public void around(ProceedingJoinPoint joinPoint, AuthDemo authDemo){
        //解析配置文件参数代码:${test.key}
        String s = environment.resolvePlaceholders(authDemo.value());
        System.out.println("未解析的值:"+authDemo.value());//${test.key}
        System.out.println("解析后的值:"+ s);//testValue
        //目标方法返回对象
        Object proceed = null;
        try {
           	 //执行目标方法:可修改目标方法的参数
             proceed = joinPoint.proceed(new Object[]{s});
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return ;
    }
}

5.测试

package com.qin.controller.MyResolveController;

import com.qin.annotation.AuthDemo;
import org.springframework.web.bind.annotation.*;


/**
 *@author WZB
 *@date 2020/03/18
 *@desciption 注解属性获取配置文件参数测试
 *
 */
@RestController
@RequestMapping(value = "/api/")
public class MyResolveController{


    @PostMapping(value = "/test")
    @AuthDemo(value = "${test.key}")
    public CommonResponse test(String param) {
        CommonResponse response=new CommonResponse();
        System.out.println(param);
        return response;
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要获取spring-boot-admin-starter-client自定义属性,可以按照以下步骤进行操作: 1. 首先,确保你的项目中已经添加了spring-boot-admin-starter-client包,并成功进行了打包。 2. 在项目的配置文件(比如application.properties或application.yml)中,可以使用以下格式设置spring-boot-admin-starter-client的自定义属性: ``` spring.boot.admin.client.custom-property=value ``` 其中,custom-property是你自定义属性名,value是对应的属性。 3. 可以通过在spring-boot-admin-starter-server项目的代码中使用@ConfigurationProperties注解来获取spring-boot-admin-starter-client的自定义属性。例如: ```java @ConfigurationProperties(prefix = "spring.boot.admin.client") public class MyConfiguration { private String customProperty; // getter and setter // other configurations } ``` 在这个配置类中,使用prefix属性指定了属性的前缀为"spring.boot.admin.client",这样就能获取到spring-boot-admin-starter-client的自定义属性。 4. 运行spring-boot-admin-starter-server项目时,就可以通过MyConfiguration类获取spring-boot-admin-starter-client的自定义属性了。 综上所述,你可以通过在项目的配置文件中设置属性,并使用@ConfigurationProperties注解获取这些自定义属性来获取spring-boot-admin-starter-client的自定义属性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [spring-boot-admin-client-2.6.2-API文档-中文版.zip](https://download.csdn.net/download/qq_36462452/85294581)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [spring-boot-admin-starter-client与spring-boot版本不匹配的坑](https://blog.csdn.net/mahui_1980/article/details/117528352)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值