Java Can';t从其他spring项目中导入的spring项目jar访问实例
我创建了一个jar of spring项目,效果非常好。我已经将这个jar导入到另一个spring项目中。现在,我想访问springjar中的一些实例。例如,下面的类来自内部spring项目。我想获取AuthenticationClient的实例。我编程的方式是创建一个静态getter方法,它将返回实例的引用。对于要在静态引用中设置的实例,我必须在侦听器中自动连接它 因为,我在另一个Spring项目中导入了jar,我发现监听器最终没有被调用,导致所有事件链失败。下面是outer Spring项目的控制器,我正在尝试访问AuthenticationClient实例 BeanJava Can';t从其他spring项目中导入的spring项目jar访问实例,java,spring,spring-mvc,Java,Spring,Spring Mvc,我创建了一个jar of spring项目,效果非常好。我已经将这个jar导入到另一个spring项目中。现在,我想访问springjar中的一些实例。例如,下面的类来自内部spring项目。我想获取AuthenticationClient的实例。我编程的方式是创建一个静态getter方法,它将返回实例的引用。对于要在静态引用中设置的实例,我必须在侦听器中自动连接它 因为,我在另一个Spring项目中导入了jar,我发现监听器最终没有被调用,导致所有事件链失败。下面是outer 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";
}
}供其他正在寻找答案的用户参考。我们可以将jar的上下文文件导入到当前项目中。确保jar位于类路径中。然后,您可以像这样导入上下文
<beans:import resource="classpath:/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml"/>
为什么不直接使用@Autowired或@Resource对AuthenticationClient进行jct?AuthenticationClient类有一些其他依赖项,这些依赖项又有一些其他嵌套依赖项。您想单独使用AuthenticationClient吗?我的意思是没有依赖项?我想初始化AuthenticationClient及其依赖项如果你想在jar中使用bean进行注入,这个()可能会有所帮助。我听说src/main/webapp是一个源文件夹,你在启动web应用程序时测试过它吗?是的,我也这么想。之前,我给出了WEB-INF中的类路径,但未能加载上下文。但是给出完整的路径如何在jar中或在项目的扩展目录下工作?我在项目的扩展目录中有一个servlet-context.xml,在jar中有另一个servlet-context.xml,然后放在项目的servlet-context.xml中就可以了。