我创建了一个运行良好的春季项目罐子。我已将此 jar 导入另一个 spring 项目。现在,我想访问一些在 spring jar 中创建的实例。例如,下面的类来自内部弹簧项目。我想获取 AuthenticationClient 的实例。我对其进行编程的方式是创建一个静态 getter 方法,该方法将返回实例的引用。对于要在静态引用中设置的实例,我必须在侦听器中自动装配它。
因为,我已经将 jar 导入到另一个 Spring 项目中,我发现最终没有调用监听器,导致所有事件链都失败。下面是我试图访问 AuthenticationClient 实例的外部 Spring 项目的控制器
豆
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;
}
听众
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
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";
}
}