我正在使用Spring MVC作为我的Web应用程序。我的bean是用“spring-servlet.xml
”文件
现在我有一个类MyClass
,我想使用spring bean
在spring-servlet.xml
我写过以下
<bean id="myClass" class="com.lynas.MyClass" />
现在我需要使用ApplicationContext
ApplicationContext context = ??
这样我才能做到
MyClass myClass = (MyClass) context.getBean("myClass");
怎么做?
答案 0 :(得分:137)
答案 1 :(得分:76)
我认为这个link演示了在任何地方获取应用程序上下文的最佳方法,即使在非bean类中也是如此。我发现它非常有用。希望对你来说一样。以下是它的抽象代码
创建一个新类ApplicationContextProvider.java
package com.java2novice.spring;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class ApplicationContextProvider implements ApplicationContextAware{
private static ApplicationContext context;
public static ApplicationContext getApplicationContext() {
return context;
}
@Override
public void setApplicationContext(ApplicationContext ac)
throws BeansException {
context = ac;
}
}
在application-context.xml中添加条目
<bean id="applicationContextProvider"
class="com.java2novice.spring.ApplicationContextProvider"/>
在注释案例中(而不是application-context.xml)
@Component
public class ApplicationContextProvider implements ApplicationContextAware{
...
}
获取此类上下文
TestBean tb = ApplicationContextProvider.getApplicationContext().getBean("testBean", TestBean.class);
干杯!!
答案 2 :(得分:38)
如果您需要从 HttpServlet中访问上下文,而HttpServlet本身并未由Spring实例化(因此@Autowire和ApplicationContextAware都不起作用)...
WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
或
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
至于其他一些回复,在你这样做之前要三思而后行:
new ClassPathXmlApplicationContext("..."); // are you sure?
...因为这不会给你当前的上下文,而是为你创建它的另一个实例。这意味着1)在这两个应用程序上下文中不共享大量内存和2)bean。
答案 3 :(得分:25)
如果你正在实现一个没有被Spring实例化的类,就像你可以使用的JsonDeserializer那样:
WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
MyClass myBean = context.getBean(MyClass.class);
答案 4 :(得分:8)
将此添加到您的代码中
@Autowired
private ApplicationContext _applicationContext;
//Add below line in your calling method
MyClass class = (MyClass) _applicationContext.getBean("myClass");
// Or you can simply use this, put the below code in your controller data member declaration part.
@Autowired
private MyClass myClass;
这只会将myClass注入您的应用程序
答案 5 :(得分:5)
基于Vivek的回答,但我认为以下情况会更好:
@Component("applicationContextProvider")
public class ApplicationContextProvider implements ApplicationContextAware {
private static class AplicationContextHolder{
private static final InnerContextResource CONTEXT_PROV = new InnerContextResource();
private AplicationContextHolder() {
super();
}
}
private static final class InnerContextResource {
private ApplicationContext context;
private InnerContextResource(){
super();
}
private void setContext(ApplicationContext context){
this.context = context;
}
}
public static ApplicationContext getApplicationContext() {
return AplicationContextHolder.CONTEXT_PROV.context;
}
@Override
public void setApplicationContext(ApplicationContext ac) {
AplicationContextHolder.CONTEXT_PROV.setContext(ac);
}
}
从实例方法写入静态字段是一种不好的做法,如果操作多个实例则会很危险。
答案 6 :(得分:1)
第1步:在课程
中注入以下代码@Autowired
private ApplicationContext _applicationContext;
第2步:写入Getter&amp;塞特
第3步:在定义了bean的xml文件中定义autowire =“byType”
答案 7 :(得分:0)
另一种方法是通过servlet注入applicationContext。
这是在使用Spring Web服务时如何注入依赖项的示例。
<servlet>
<servlet-name>my-soap-ws</servlet-name>
<servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
<init-param>
<param-name>transformWsdlLocations</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:my-applicationContext.xml</param-value>
</init-param>
<load-on-startup>5</load-on-startup>
</servlet>
替代方法是在web.xml中添加应用程序上下文,如下所示
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/my-another-applicationContext.xml
classpath:my-second-context.xml
</param-value>
</context-param>
基本上,您正在尝试告诉servlet它应该查找在这些上下文文件中定义的bean。
答案 8 :(得分:0)
有很多方法可以在Spring应用程序中获取应用程序上下文。那些在下面:
通过ApplicationContextAware :
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class AppContextProvider implements ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
在这里setApplicationContext(ApplicationContext applicationContext)
方法中,您将获得applicationContext
通过自动连线:
@Autowired
private ApplicationContext applicationContext;
此处@Autowired
关键字将提供applicationContext。
有关更多信息,请访问this thread
谢谢:)
答案 9 :(得分:0)
即使您的类不是RestController或Configuration类,即使在添加@Autowire之后,applicationContext对象也将作为null出现。尝试使用下面的方法创建新类,并且运行良好:
@Component
public class SpringContext implements ApplicationContextAware{
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws
BeansException {
this.applicationContext=applicationContext;
}
}
然后,您可以根据需要在同一个类中实现getter方法,例如通过以下方式获取Implemented类引用:
applicationContext.getBean(String serviceName,Interface.Class)
答案 10 :(得分:0)
让它通过构造函数注入比使用@Autowired更好。在构造函数注入here
中找到一些参数@Component
public class MyClass{
private final ApplicationContext applicationContext;
public MyClass(ApplicationContext applicationContext){
this.applicationContext = applicationContext;
}
//here will be your methods using the applicationcontext
}
答案 11 :(得分:-8)
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring-servlet.xml");
然后你可以检索bean:
MyClass myClass = (MyClass) context.getBean("myClass");