Peter Penzov
Peter Penzov

Reputation: 1648

Call Spring component from Java code

I have this Spring component which I want to call from several locations in Java web application:

@Component
public class NotificationListener {

    public void notificationProcess(TransactionsBean ro) {

        // some code
    }    
}

Can I use it as a normal object in Spring or I should use other way to call the Spring code?

Upvotes: 1

Views: 794

Answers (1)

KenCoenen
KenCoenen

Reputation: 513

What you can do is retrieve the current ApplicationContext by implementing ApplicationContextAware and lookup the bean yourself with getBean.

Another thing you can try is autowire like you're used to with @Autowired and then use the following utility from Spring:

SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);

Upvotes: 1

Related Questions