【问题标题】:How to give property placeholder support for java custom annotation如何为 java 自定义注解提供属性占位符支持
【发布时间】:2017-10-30 09:29:48
【问题描述】:

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

例如:

Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface PublishEvent {

    public EventStore eventStore();
    public boolean isPersistent() default false;
    public String channelName();


}

我想用作:

@PublishEvent(channelName="${rabbit.event.default}")
public void someMethod(){}

其中 rabbit.event.default 是 application.properties 文件中定义的属性的键。我希望将属性键替换为值,就像 spring 的 @Value 注释一样。

我正在使用 Spring AOP 来拦截注解并进行处理。

【问题讨论】:

  • 我试图找到一个简单的方法来做到这一点,我发现最好的方法是在这个链接中,stackoverflow.com/questions/19316575/…,希望它对你有所帮助
  • @cralfaro :感谢您的链接。我正在使用弹簧引导。所以我认为 PropertySourcesPlaceholderConfigurer 会被引导注入。我正在尝试使用 Aspect 的方法。如果工作正常,将发布完整的代码

标签: java spring annotations aop


【解决方案1】:

我使用org.springframework.util.StringValueResolver#resolveStringValue 方法来解析占位符值。可能是下面的示例代码可以帮助你:

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

【讨论】:

    【解决方案2】:

    由于我无法找到使用属性值丰富注释属性的内置方法,因此我最终创建了一个具有相同功能的实用程序类。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-21
      • 1970-01-01
      • 2016-02-28
      • 2014-05-16
      • 2013-02-07
      • 2012-02-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多