Announcement Announcement Module
Collapse
No announcement yet.
@Required annotation not working when creating instance in @Bean Java config class Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Required annotation not working when creating instance in @Bean Java config class

    I'm trying to enforce the @Required annotation on a bean created inside a Java config class (@Bean instance). But it doesn't seem to work, the property is always returned as null


    The bean class:
    public class SequenceGenerator {

    private String suffix;
    ...
    @Required
    public void setSuffix(String suffix) {
    this.suffix = suffix;
    }

    The I do this in the Java config class, without calling the setSuffix:

    @Configuration
    public class SequenceConfiguration {
    @Bean
    public SequenceGenerator sequenceGenerator() {
    SequenceGenerator sequence= new SequenceGenerator();
    return sequence;
    }
    }

    However, when I run the app there are no errors and the suffix value is returned as null.

    Anyone encountered this issue ? Is it even possible to enforce @Required using a @Bean Java config class ?

    Thanks in advance for any help or tip

  • #2
    Did you tell the container that you're using Java Config?

    You can do that by instantiating your Application Context like this

    ClassPathXmlApplicationContext appcontext = new ClassPathXmlApplicationContext("MyAppContext.xml") ;


    where MyAppContext.xml should look like this.


    <context:annotation-config />
    <context:component-scan base-package="your.package" />


    If it helps, take a look at my blog article on this topic http://arecordon.blogspot.com.ar/p/s...educe-xml.html

    Regards!
    Last edited by _0R10N; Apr 30th, 2013, 11:10 AM.

    Comment


    • #3
      Hello

      Next time use the code tags....

      Could you post how you are doing the configuration...?

      Comment

      Working...
      X