Announcement Announcement Module
Collapse
No announcement yet.
Annotations ignored by Spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    Specify &quot;assembler&quot; and &quot;attributeSource&quot;

    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, 10:22 PM.

    Comment


    • #3
      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, 12:32 PM.

      Comment


      • #4
        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.

        Comment


        • #5
          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.

          Comment


          • #6
            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...

            Comment


            • #7
              could you post the code?

              It looks like the annotations are ignored for me.

              Comment


              • #8
                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...

                Comment


                • #9
                  awesome, thank you very much.

                  Comment

                  Working...
                  X