hossaindoula
hossaindoula

Reputation: 980

Spring Service called failed from non Spring Classes

I am creating my own convenient framework for automating development using Spring framework. So, I have used my non spring beans inside the application. When I call the Spring @Component/@service classes from my own non spring classes the server is either not starting (just gets halted) or if somehow I initialized after construction (using @PostConstruct) then it is showing error at the particular @Controller method after successfully running the application. My problem might be calling the Spring beans from non Spring classes. I have set up my application as Annotation based not XML based. Here is my calling of Spring beans from non Spring classes.

AnnotationConfigApplicationContext 
    ctx = new AnnotationConfigApplicationContext();
            ctx.register(AppConfiguration.class);
            ctx.refresh();
        userDetailsService = ctx.getBean("userDetailsService", UserDetailsServiceImpl.class); 

It might happen that my calling is not right.

Upvotes: 1

Views: 274

Answers (1)

sura2k
sura2k

Reputation: 7517

UserDetailsService userDetailsService = ctx.getBean("userDetailsService");

or,

UserDetailsService userDetailsService = ctx.getBean("userDetailsService", UserDetailsService.class); //Pass `requiredType` as the super type

Upvotes: 3

Related Questions