Results 1 to 9 of 9

Thread: Annotations ignored by Spring

  1. #1

    Default Annotations ignored by Spring

    Maybe I'm doing something wrong but, I can't find a way to get the annotations to be read by Spring for my managed Beans.

    Code:
    ManagedResource(description = "Configurable Data Service Factory allows REST client swapping")
    public class DataServiceFactory implements IDataServiceFactory, DataServiceFactoryMBean {
    (There is an 'at' sign on the MangedResource but, the forum code was blocking it as a url?)

    Code:
    <context:mbean-export default-domain="BeerCode" />
    
    
    	<bean id="dataClientFactory" class="com.willcode4beer.rest.client.DataServiceFactory">
    		<description>Configurable Data Client Factory</description>
    		<property name="httpClient">
    			<bean class="com.willcode4beer.rest.client.HttpDataClient" />
    		</property>
    		<property name="mockClient" ref="mockDataClient" />
    	</bean>
    When I pull up jconsole, the bean does show up under the name "BeerCode" but, the description is the default instead of what is defined in the annotation. Setting objectName also gets ignored.

    Am I missing something?

  2. #2

    Default Specify "assembler" and "attributeSource"

    In the Spring XML configuration file, you need to specify the MetadataMBeanInfoAssembler and associate it with a specified attributeSource that uses either Apache Commons Attributes or Java 5/6 Annotations.

    Good coverage of using these is available in section 20.3 of the the Spring 2.5 Reference Manual. Because you're using @ManagedResource, it appears that you are using Java annotations, so your Spring XML would look something like that shown in the XML snippets shown in the Reference Manual under sections 20.3.3 and 20.3.5.

    You can find examples of the annotations being used in the blog entry Using Spring Metadata MBean Exporting for Greater Descriptive Detail.
    Last edited by spring_dm; Jun 5th, 2008 at 10:22 PM.

  3. #3

    Default maybe something else?

    thanks for the reply, I was beginning to think the forum was inactive.

    I ripped the config off from 20.3.3 (copy/paste); still, none of the meta-data from the annotations is showing up...

    It did change the names of the mbeans (as seen in jconsole) to use the canonical class name instead of the nice names before.
    It appears the annotations are ignored completely. Everything looks/acts the same with or without them.
    Last edited by willcode4beer; Jun 6th, 2008 at 12:32 PM.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    It works for me.

    I think that your problem is the fact that you are mixing 2 MBean strategies. Annotation based and interface based (You implement an MBean interface). Choose one way I would say.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5

    Default

    If I don't implement an XXXMBean interface then my beans don't get exported to JMX unless I do the old style configuration (which also ignores the annotations).

    The problem with the old style configuration is it exports more methods than I want available to JMX.

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    As I said before it works for me. I hacked together a quick project 2 classes and a few lines of xml. And it works.

    If you implement the MBean interface your annotations are useless...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7

    Default

    could you post the code?

    It looks like the annotations are ignored for me.

  8. #8
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    Interface
    Code:
    public interface SomeService {
    	public void setSomething(String something);
    	public String getSomething();
    	public void doIt();	
    }
    Class
    Code:
    @ManagedResource(description="SomeService with a description")
    public class SomeServiceImpl implements SomeService {
    	
    	private String something;
    
    	@Override
    	@ManagedOperation(description="Method doing it.")
    	public void doIt() {
    		System.out.println("doing it");
    	}
    
    	@Override
    	@ManagedAttribute(description="Getting the string Something.")
    	public String getSomething() {
    		return this.something;
    	}
    
    	@Override
    	public void setSomething(String something) {
    		this.something=something;
    	}
    
    }
    Configuration
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:util="http://www.springframework.org/schema/util"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
        		http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context-2.5.xsd
                  ">
    
    
    	<bean id="someService" class="com.springsource.demo.jmx2.SomeServiceImpl">
    		<property name="something" value="default value" />
    	</bean>
    
    	<context:mbean-export default-domain="SpringSource-JMX2" />
    </beans>
    Runner class
    Code:
    public class Runner {
    
    	public static void main(String[] args) {
    		ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext-jmx.xml");
    		SomeService caller = (SomeService) context.getBean("someService");
    		try {
    			System.out.println("Start JConsole!");
    			System.in.read();
    			System.out.println("END");
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    
    }
    So nothing fancy and I see the description quite clearly...
    Attached Images Attached Images
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  9. #9

    Default

    awesome, thank you very much.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •