1

I am using Spring Boot 1.3, and I have the configuration class below:

@Configuration

public class MainConfig {

    @Bean(name="dateAndTimeFormater")
    public SimpleDateFormat dateAndTimeFormater(){
        return new SimpleDateFormat("yyyy-MM-dd"+Constants.STRING_SEPARATOR+"hh:mm");
    }
    @Bean(name="dateFormater")
    public SimpleDateFormat dateFormaterBean(){
        return new SimpleDateFormat("yyyy-MM-dd"+Constants.STRING_SEPARATOR+"hh:mm");
    }
}

When I try to inject one of the below beans by name, it throws : No qualifying bean of type [java.text.SimpleDateFormat] is defined: expected single matching bean but found 2: dateAndTimeFormater,dateFormater.

here is where I am injecting the bean: private static SimpleDateFormat sdf;

@Autowired
@Qualifier("dateAndTimeFormater")
public static void setSdf(SimpleDateFormat sdf) {
    myClass.sdf = sdf;
}

I tried with @Ressource, @Inject. it didn't work.

Any Advise will be much appreciated?

reos
  • 8,766
  • 6
  • 28
  • 34
  • I run your code and it's almost OK. If you want to autowire the parameter sdf then it must not be 'static'. However the error that you're getting is if you don't have '@Qualifier' so I think you have a totally different code in your project – reos Oct 11 '16 at 17:05

1 Answers1

1

It is because you are trying to wire that static method, spring container will not wire dependencies looking static references or methods, why can't you do that

@Autowired
@Qualifier("dateAndTimeFormater")
public void setSdf(SimpleDateFormat sdf) {
    myClass.sdf = sdf;
}
Community
  • 1
  • 1
kuhajeyan
  • 10,727
  • 10
  • 46
  • 71
  • i tried without static it did not work as well. I think when inject the bean with annotation Bean, the annotation Qualifier does not work. when I tried it with a simple annotation Component class it works fine ??!!! – Tarek Sahalia Oct 13 '16 at 13:50