2

I have a WEB Application. I was asked to Call a Class (Service) inside the Spring application from a POJO.

The Service is not exposed either as Webservice or REST. I was just the application as a jar. I had to add this to build path and call the service.

Will it work??

Am terribly confused!

madhairsilence
  • 3,473
  • 1
  • 30
  • 60

2 Answers2

2

There's not a lot of detail in here, but I'm assuming this is a Spring application using Spring MVC, in which case yes, there's no reason why you can't call the service as a class.

This is why you can use JUnit to test @Controller classes in Spring, it's literally just a POJO.

Link below as an example of how to do this.

How to unit test a Spring MVC controller using @PathVariable?

Community
  • 1
  • 1
david99world
  • 18,271
  • 28
  • 102
  • 127
1

If your pojo is a Spring Bean, then you can just autowire the service like this:

public class YourPojo {
   @Autowired
   YourService yourService; 
}

You might consider making your Pojo a Spring Bean by annotationg it with @Component, or declaring it in your ApplicationContext.xml

If not, you get your service like this if you have a request object:

ServletContext sc = req.getSession().getServletContext();
ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc);
YourService yourService = ac.getBean("yourService");
Dragos Bulugean
  • 349
  • 2
  • 5