0

I had created a jar of spring project which works excellent. I have imported this jar into another spring project. Now, I would like to access some of the instances made in the spring jar. For example, the class below is from the inner spring project. I want to get the instance of the AuthenticationClient. The way I programmed it was to create a static getter method which would return me the reference of the instance. For the instance to be set in the static reference I had to autowire it in a Listener.

Since, I have imported the jar inside another Spring Project I found that the listener is not being called eventually, failing all the chain of events. Below, is the controller of the outer Spring project where I am trying to access the instance of the AuthenticationClient

Bean

public class AuthenticationClient {

private @Autowired KerberosAPI kerberosAPI;
private @Autowired KerberosSessionManager kerberosSessionManager;
private static AuthenticationClient client;

public static AuthenticationClient getAuthenticationClient(){
    return client;
}

public @Resource(name="authenticationClient") void setAuthenticationClient(AuthenticationClient client){
    AuthenticationClient.client = client;
}

Listener

public class ApplicationListenerBean implements ApplicationListener<ContextRefreshedEvent>` {

private @Autowired AuthenticationClient client;

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
    ApplicationContext applicationContext = event.getApplicationContext();
    System.out.println();
    // now you can do applicationContext.getBean(...)
    // ...
}

}

Controller

@Controller

public class HomeController {

private  AuthenticationClient client;

/**
 * Simply selects the home view to render by returning its name.
 */
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
    client = AuthenticationClient.getAuthenticationClient();

    return "home";
}

}

Raunak Agarwal
  • 7,117
  • 6
  • 38
  • 62

1 Answers1

0

For the reference of some other user who is looking for answer. We can import the jar's context file in to our current project. Make sure the jar is in the classpath. Then, you an import the context like this

<beans:import resource="classpath:/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml"/>
Raunak Agarwal
  • 7,117
  • 6
  • 38
  • 62