2016-06-20 8 views
2

私はカスタムJavaアノテーションを作成し、特定の属性を持っています。文字列属性のプロパティプレースホルダをサポートするにはどうすればよいですか?Javaカスタムアノテーションのプロパティプレースホルダのサポート方法

例:rabbit.event.defaultがapplication.propertiesファイルに定義されたプロパティのキーです

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

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

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


} 

私はとして使用したいです。私は春の@Valueアノテーションの場合のように、プロパティキーをvalueに置き換えたいと思っています。

私は、Spring AOPを使用してアノテーションをインターセプトして処理しています。

+0

私はそれを行うための簡単な方法を見つけるためにしようとしていた、と私が見つけた最善のアプローチは、このリンクであり、http://stackoverflow.com/questions/19316575/custom-annotation-like-value、あなたに役立つことを願っています – cralfaro

+0

@cralfaro:リンクをありがとう。私はスプリングブーツを使用しています。だから、私はPropertySourcesPlaceholderConfigurerがブートによって注入されると思います。私はAspectを使ってメソッドを試しています。そのうまくいくと完全なコードを投稿します – Sandheep

答えて

0

アノテーション属性をプロパティ値で充実させるための組み込みメソッドが見つかりませんでしたので、同じようにユーティリティクラスを作成しました。

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

    } 

} 
0

Iプレースホルダ値を解決するには、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()); 
    } 
    } 
} 
関連する問題