Announcement Announcement Module
No announcement yet.
@Required annotation not working when creating instance in @Bean Java config class Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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;
    public void setSuffix(String suffix) {
    this.suffix = suffix;

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

    public class SequenceConfiguration {
    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

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


    • #3

      Next time use the code tags....

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