Results 1 to 5 of 5

Thread: How do I enable @Required Annotation in JavaConfig?

  1. #1
    Join Date
    Oct 2008
    Location
    Cambridge, MA
    Posts
    56

    Default 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 at 03:14 PM. Reason: testing if validation is shut off now

  2. #2
    Join Date
    Oct 2008
    Location
    Cambridge, MA
    Posts
    56

    Default 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());
    	}
    	
    }

  3. #3
    Join Date
    Apr 2007
    Posts
    307

    Default

    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
    Chris Beams
    Spring Framework committer, VMware
    http://github.com/cbeams

  4. #4
    Join Date
    Oct 2008
    Location
    Cambridge, MA
    Posts
    56

    Default 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 at 01:26 PM. Reason: removed whitespace

  5. #5
    Join Date
    Apr 2007
    Posts
    307

    Default

    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.
    Chris Beams
    Spring Framework committer, VMware
    http://github.com/cbeams

Posting Permissions

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