Announcement Announcement Module
Collapse
No announcement yet.
My custom annotation processing Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • My custom annotation processing

    Hi,

    In my current project, we create a mapping between the actors and business processor class (in a HashMap defined inside Spring config as a bean). The term actor is not used in a generic sense, rather specific to our application. An example is shown below:

    Code:
    <util:map id="businessProcessorMapping" map-class="java.util.HashMap" key-type="java.lang.String" value-type="java.lang.String">
       <entry key="EMEA_MANAGER_ORDER_CREATE" value="orderCreateBusinessProcessor" />
        <entry key="APAC_MANAGER_ORDER_CREATE" value="apacManagerOrderCreateBusinessProcessor" />
    </util:map>
    Here, EMEA_MANAGER is the actor ID in our application and ORDER_CREATE is the transaction that the actor wants to invoke. There must be a bean with ID "emeaManagerOrderCreateBusinessProcessor" which defines the class which performs this transaction.

    Now, as the number of transactions are growing in our application, it is difficult to maintain such a mapping hash map. Also, developers tend to do mistakes in defining the mapping keys, which causes runtime issues.

    To avoid this, I was thinking to introduce a custom annotation

    Code:
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface TransactonProcessor
    {
            /**
    	 * Bean ID defined in Spring config
    	 * @return
    	 */
    	String beanId();
    	
    	/**
    	 * The transaction Name for which it is applicable
    	 * 
    	 * @return
    	 */
    	String transactionName();
    	
    	/**
    	 * An array of actors for which this is applicable
    	 * 
    	 * @return
    	 */
    	String[] applicableActors();
    }
    What I want is, which spring application context is loaded at the beginning of the application deployment, spring should read all the classes inside a package (and its sub package) and scan this annotation and prepares this hash map inside the spring context so that an appropriate businessProcessor can be picked at the run time.

    Please help me implement this feature.

    Thanks,
    Niranjan

  • #2
    Hi,
    Spring uses classpath scanning to detect new components based on annotations. This is actually the goal of the <ctx:component-scan> element, which detects @Repository, @Controller, @Component, etc. and register beans in the application based on the annotated classes.

    The class that defines that mechanism is ClassPathScanningCandidateComponentProvider. This class actually returns a Set of BeanDefinition objects as the result of the classpath scanning. You can then register these bean definitions into your ApplicationContext. There is a Filter API to select the candidates.

    What you have to do is create a PostProcessor that, when invoked, scans the classpath to find components and registers them as Spring beans or into your Map or whatever.

    The only issue with ClassPathScanningCandidateComponentProvider is that it creates BeanDefinitions based on found classes, and nothing else. I submitted a jira issue (SPR-7260) to provide a Scanner template to which you can pass callbacks like any other spring template : the goal is to scan the classpath and pass a MetadataReader to the callback for each candidate. The callback can then do whatever it needs.

    Comment

    Working...
    X