WebSphere Application Server & Liberty

 View Only
  • 1.  How to injecting Spring Beans to JAX-RS resource running on Websphere Application Server 9 , JDK 1.8

    Posted Thu September 21, 2023 01:09 PM

    Hi

    I am running WebSphere application server 9 (Java 1.8) which hosts a JAX-RS  application and would like to inject spring beans ( Spring-Core version 5.3.27) into the application. 

    Since websphere JAX-RS depends on IBMRestServlet, following the reference http://www.javavillage.in/spring-ioc-on-servlets.php,  I tried  creating a class that extends the IBMRestServlet and adding in an init() like one below

    @Override
        public void init(ServletConfig config) throws ServletException {
            super.init(config);
            SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
    
        }

    I had to add in was_public.jar and j2ee.jar provided in the WebSphere Dev folder as "provided" dependency in maven but maven was still complaining about following

    [ERROR] COMPILATION ERROR :
    [INFO] -------------------------------------------------------------
    [ERROR] cannot access com.ibm.ws.ras.instrument.annotation.InjectedFFDC
      class file for com.ibm.ws.ras.instrument.annotation.InjectedFFDC not found
    

    I was not able to resolve the dependency. I searched IBM resources for a solution but could not find any.  Greatly appreciate if anyone can guide me here or suggest a better approach

    In meanwhile, I tried the following alternate method

    Alternate Method : Load Spring within Resource Class

    I tried loading spring context within the resource class and was successfully able to use Spring beans 

     @Component
    @Path("/mypath")
    public class MyResource {
    
    
    	@Autowired
    	TestClass testClass;
    
    
    	@POST
    	@Path("/subpath")
    	@Consumes(MediaType.APPLICATION_JSON)
    	@Produces(MediaType.APPLICATION_JSON)
    	public Response getSomeWork(MyRequest request)  {
    		
    		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    		TestClass tClass = applicationContext.getBean(TestClass.class);
    		
    		..
    	}

    the Web.xml needed the following

    <listener>
    		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    	</listener>
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>/WEB-INF/webApplicationContext.xml</param-value>
    	</context-param>

    Unfortunately as it is obvious, there would be a big overhead of the initialization per resource along with having multiple Spring ApplicationContext ( one per resource)  for the  application which is undesirable

    Thanks in advance

    Sibi



    ------------------------------
    Sibi Vaithara
    ------------------------------


  • 2.  RE: How to injecting Spring Beans to JAX-RS resource running on Websphere Application Server 9 , JDK 1.8

    Posted Fri September 22, 2023 01:08 AM

    Hi All,

    Apologies, I posted the same twice, kindly respond to 

    https://community.ibm.com/community/user/wasdevops/discussion/injecting-spring-beans-to-jax-rs-resource-running-on-websphere-application-server-9-jdk-18?ReturnUrl=%2fcommunity%2fuser%2fwasdevops%2fcommunities%2fcommunity-home%2fdigestviewer%3fcommunitykey%3d5c4ba155-561a-4794-9883-bb0c6164e14e

    Thanks



    ------------------------------
    Sibi Vaithara
    ------------------------------