Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何为java自定义注释提供属性占位符支持_Java_Spring_Annotations_Aop - Fatal编程技术网

如何为java自定义注释提供属性占位符支持

如何为java自定义注释提供属性占位符支持,java,spring,annotations,aop,Java,Spring,Annotations,Aop,我创建了一个自定义Java注释,并具有某些属性。如何支持字符串属性的属性占位符 例如: 我想用作: @PublishEvent(channelName="${rabbit.event.default}") public void someMethod(){} 其中rabbit.event.default是application.properties文件中定义的属性的键。我想用value替换属性键,就像spring的@value注释一样 我正在使用SpringAOP截取注释并进行处理 由于我找不

我创建了一个自定义Java注释,并具有某些属性。如何支持字符串属性的属性占位符

例如:

我想用作:

@PublishEvent(channelName="${rabbit.event.default}")
public void someMethod(){}
其中rabbit.event.default是application.properties文件中定义的属性的键。我想用value替换属性键,就像spring的@value注释一样


我正在使用SpringAOP截取注释并进行处理

由于我找不到用属性值丰富注释属性的内置方法,因此我最终创建了一个实用程序类,该实用程序类也可以这样做

@Component
public class IntegrationUtils {

    @Autowired
    private Environment environment;

    @Resource
    private HashMap<String,String> propertyMapping;


    /**
     * Method to check if the text passed is a property and get the value
     * from the environment.
     *
     * @param text  : The text to be matched for the property
     * @return      : The property value if its starting with $ and has a matching value in
     *                environment
     *                Return the text itself is nothing matching
     */
    public String getEnvironmentProperty(String text) {

        // Check if the text is already been parsed
        if ( propertyMapping.containsKey(text)) {

            return propertyMapping.get(text);

        }


        // If the text does not start with $, then no need to do pattern
        if ( !text.startsWith("$") ) {

            // Add to the mapping with key and value as text
            propertyMapping.put(text,text);

            // If no match, then return the text as it is
            return text;

        }

        // Create the pattern
        Pattern pattern = Pattern.compile("\\Q${\\E(.+?)\\Q}\\E");

        // Create the matcher
        Matcher matcher = pattern.matcher(text);

        // If the matching is there, then add it to the map and return the value
        if( matcher.find() ) {

            // Store the value
            String key = matcher.group(1);

            // Get the value
            String value = environment.getProperty(key);

            // Store the value in the setting
            if ( value != null ) {

                // Store in the map
                propertyMapping.put(text,value);

                // return the value
                return value;

            }

        }

        // Add to the mapping with key and value as text
        propertyMapping.put(text,text);

        // If no match, then return the text as it is
        return text;

    }

}
@组件
公共类集成{
@自动连线
私人环境;
@资源
私有HashMap属性映射;
/**
*方法检查传递的文本是否为属性并获取值
*来自环境。
*
*@param text:要为属性匹配的文本
*@return:以$开头的属性值,在
*环境
*返回文本本身不匹配
*/
公共字符串getEnvironmentProperty(字符串文本){
//检查文本是否已被解析
if(propertyMapping.containsKey(文本)){
返回propertyMapping.get(文本);
}
//如果文本不以$开头,则不需要执行模式
如果(!text.startsWith($)){
//将键和值作为文本添加到映射中
propertyMapping.put(text,text);
//如果不匹配,则按原样返回文本
返回文本;
}
//创建模式
Pattern=Pattern.compile(“\\Q${\\E(+?)\\Q}\\E”);
//创建匹配器
Matcher Matcher=pattern.Matcher(文本);
//如果存在匹配项,则将其添加到映射并返回值
if(matcher.find()){
//存储值
字符串键=matcher.group(1);
//获取值
字符串值=environment.getProperty(键);
//将该值存储在设置中
if(值!=null){
//储存在地图上
propertyMapping.put(文本、值);
//返回值
返回值;
}
}
//将键和值作为文本添加到映射中
propertyMapping.put(text,text);
//如果不匹配,则按原样返回文本
返回文本;
}
}

我使用
org.springframework.util.StringValueResolver#resolveStringValue
方法解析placeholer值。可能下面的示例代码可以帮助您:

@Configuration
public class PublishEventConfiguration implements ApplicationContextAware, EmbeddedValueResolverAware {
  private ApplicationContext context;
  private StringValueResolver resolver;

  @Override
  public void setApplicationContext(ApplicationContext context) throws BeansException {
    this.context = context;
  }

   @Override
  public void setEmbeddedValueResolver(StringValueResolver resolver) {
    this.resolver = resolver;
  }


  @PostConstruct
  public void init() throws Exception {
    Collection<Object> publishEvents = context.getBeansWithAnnotation(PublishEvent.class).values();
    for (Object v : publishEvents) {
      PublishEvent cfg = v.getClass().getAnnotation(PublishEvent.class);
      String channelName = resolver.resolveStringValue(cfg.channelName());
    }
  }
}
@配置
公共类PublishedEventConfiguration实现ApplicationContextAware、EmbeddedValueResolveDraware{
私有应用程序上下文上下文;
私有字符串解析程序;
@凌驾
public void setApplicationContext(ApplicationContext上下文)引发BeansException{
this.context=上下文;
}
@凌驾
public void setEmbeddedValueResolver(StringValueResolver解析程序){
this.resolver=解析器;
}
@施工后
public void init()引发异常{
集合publishEvents=context.getBeansWithAnnotation(PublishEvent.class).values();
用于(对象v:发布事件){
PublishEvent cfg=v.getClass().getAnnotation(PublishEvent.class);
字符串channelName=resolver.resolveStringValue(cfg.channelName());
}
}
}

我试图找到一种简单的方法,我发现最好的方法就是在这个链接中,希望它能有所帮助you@cralfaro:谢谢你的链接。我用的是弹簧靴。因此,我认为PropertySourcesPlaceholderConfigurer将通过引导注入。我正在尝试使用方面的方法。如果工作正常,将发布完整的代码
@Configuration
public class PublishEventConfiguration implements ApplicationContextAware, EmbeddedValueResolverAware {
  private ApplicationContext context;
  private StringValueResolver resolver;

  @Override
  public void setApplicationContext(ApplicationContext context) throws BeansException {
    this.context = context;
  }

   @Override
  public void setEmbeddedValueResolver(StringValueResolver resolver) {
    this.resolver = resolver;
  }


  @PostConstruct
  public void init() throws Exception {
    Collection<Object> publishEvents = context.getBeansWithAnnotation(PublishEvent.class).values();
    for (Object v : publishEvents) {
      PublishEvent cfg = v.getClass().getAnnotation(PublishEvent.class);
      String channelName = resolver.resolveStringValue(cfg.channelName());
    }
  }
}