Announcement Announcement Module
Collapse

JavaConfig forum decommissioned in favor of Core Container

As described at

http://static.springsource.org/sprin...fig/README.TXT

key features of the Spring JavaConfig project have been migrated into the core Spring Framework as of version 3.0.

Please see the Spring 3.0 documentation on @Configuration and @Bean support:

http://static.springsource.org/sprin...tml#beans-java

For any questions related to @Configuration classes and @Bean methods in Spring 3.0, please post in the dedicated 'Core Container' forum at

http://forum.springsource.org/forumdisplay.php?f=26
See more
See less
How do I enable @Required Annotation in JavaConfig? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How do I enable @Required Annotation in JavaConfig?

    Hello,
    I'm trying to port an existing application to JavaConfig. How do I enable the @Required annotation? In XML, I did so via the <context:annotation-config/> tag. My code is:
    Code:
    @ContextConfiguration(locations = "mypackage.ApplicationConfig", loader = JavaConfigContextLoader.class)
    public class ReplicatedStoreTest extends AbstractTestNGSpringContextTests{
    
    	@Test
    	public void test() {
    		Assert.assertNotNull(config);
    	}
    	
    	@Autowired
    	StartupParameters config;
    }
    It's injecting properly into the config object, but ignoring the @Required in StartupParameters

    Thanks in Advance,
    Steven
    Harvard Children's Hospital Informatics Program
    Last edited by JavaGeek_Boston; Oct 21st, 2008, 04:14 PM. Reason: testing if validation is shut off now

  • #2
    Nevermind, found the answer

    Code:
    @Configuration(checkRequired=true)
    public class ApplicationConfig {
    	@Bean
    	public StartupConfig getConfig() {
    		StartupConfig startup = new StartupConfig();
    		return startup;
    	}
    
    	@Bean
    	public BDBWrapper getWrapper() throws DatabaseException {
    		return new BDBWrapper(getConfig());
    	}
    	
    }

    Comment


    • #3
      Yes, checkRequired=true is part of the equation. For @Required checking to work, you must enable an aspect - take a look at SJC's atrequired sample for details:

      https://fisheye.springframework.org/...les.atrequired

      Comment


      • #4
        Spoke too soon. Claims I need to set variables when I already have.

        Thanks for the quick reply Chris,
        My code is now checking the @Required annotation, but doesn't recognize it when I set it. I get "Method 'setCacheSize' is required for bean 'getConfig'" despite having clearly set the property. Did I use the annotations incorrectly? Could this have something to do with AbstractTestNGSpringContextTests?

        Here's my test:
        Code:
        @ContextConfiguration(locations = "mydomain.ApplicationConfig", loader = JavaConfigContextLoader.class)
        public class ReplicatedStoreTest extends AbstractTestNGSpringContextTests{
        
        	@Test
        	public void test() {
        		logger.debug(config);
        		Assert.assertNotNull(config);
        	}
        	
        	@Autowired
        	StartupConfig config;
        }
        Here's my config:
        Code:
        //@PropertiesValueSource(locations="file:${user.home}/.isg/test.properties")   //temporarily hidden until I can get this reading ${user.home}
        @PropertiesValueSource(locations={"classpath:/myproject.properties", 
                "file:/home/myname/.isg/test.properties"})
        @Configuration(checkRequired=true)
        public abstract class ApplicationConfig {
        	@Bean
        	public StartupConfig getConfig() {
        		StartupConfig startup = new StartupConfig();
        		startup.setCacheSize(getCache());
        		return startup;
        	}
        	@ExternalValue(value="myproject.bdbhome")
        	public abstract String getBDBDir();
        
        	@ExternalValue(value="myproject.cache")
        	public abstract int getCache();
        }
        Here's the bean with the @Required annotation.
        Code:
        public class StartupConfig {
        	private long cacheSize;
        	public long getCacheSize() {
        		return cacheSize;
        	}
        	@Required
        	public void setCacheSize(long cacheSize) {
        		this.cacheSize = cacheSize;
        	}
        }
        Thanks in Advance,
        Steven
        Last edited by JavaGeek_Boston; Oct 22nd, 2008, 02:26 PM. Reason: removed whitespace

        Comment


        • #5
          Read the following Javadoc: https://fisheye.springframework.org/...java?r=597#l17

          JavaConfig cannot directly support @Required in the same way that Spring XML can, due to fundamental limitations. In order to emulate this support, you must bytecode weave an aspect against your objects that have @Required methods. That aspect is already written for you (RequiredMethodInvocationTracker), and is included in the SJC jar. You just need to either apply it using compile-time weaving or load-time weaving.

          After reading, let me know what questions you have.

          Comment

          Working...
          X