Java SpringMVC:实例化bean时如何获取上下文参数?
这是一个简单的问题 spring实例化bean时如何获取servlet上下文参数 我的servlet上下文xmlJava SpringMVC:实例化bean时如何获取上下文参数?,java,spring,spring-mvc,Java,Spring,Spring Mvc,这是一个简单的问题 spring实例化bean时如何获取servlet上下文参数 我的servlet上下文xml <beans:bean id="ApplicationInfo" class="xx.xx.xx.ApplicationInfo"/> 我的pom.xml <org.springframework-version>3.2.11.RELEASE</org.springframework-version> <org.aspectj-versio
<beans:bean id="ApplicationInfo" class="xx.xx.xx.ApplicationInfo"/>
我的pom.xml
<org.springframework-version>3.2.11.RELEASE</org.springframework-version>
<org.aspectj-version>1.6.9</org.aspectj-version>
<org.slf4j-version>1.5.10</org.slf4j-version>
<org.springsecurity-version>3.2.5.RELEASE</org.springsecurity-version>
<org.springjpa-version>1.1.0.RELEASE</org.springjpa-version>
<hibernate.version>4.1.6.Final</hibernate.version>
<postgresql.version>9.1-901-1.jdbc4</postgresql.version>
<tiles-version>2.2.2</tiles-version>
<jackson-json-version>2.1.0</jackson-json-version>
3.2.11.1发布
1.6.9
1.5.10
3.2.5.1发布
1.1.0.1发布
4.1.6.最终版本
9.1-901-1.jdbc4
2.2.2
2.1.0
注意:我试图使用@annotation获取servlet上下文,但它是空指针
如果您想在构造函数中访问Autowiredbean,必须使用构造函数类型注入:
@Component
public class ApplicationInfo {
private final FooService fooService;
@Autowired
public ApplicationInfo(ServletContext servletContext, FooService fooService) {
this.fooService = fooService;
// do something here
}
}
若您必须(或想要)坚持XMLBeans定义,那个么您必须使您的类实现接口,这使得Spring能够通过setter自动设置此依赖关系。不是在构造函数中访问它,而是在bean完全初始化时激发的带注释的方法中执行:
public class ApplicationInfo implements ServletContextAware {
private final FooService fooService;
private ServletContext servletContext;
public ApplicationInfo(FooService fooService) {
this.fooService = fooService;
}
@PostConstruct
public void init() {
// now you fooService and servletContext are set
}
@Override
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
}
尝试使用
@Value
注释
public class ApplicationInfo {
@Value("${parameterName}")
private String parameter;
}
不要。注入
环境
并使用getProperty
手动检索值,或者只需添加一个带有@value(“${your.property.name}”)注释的字段
,这将注入属性。ServletContext
是属性的参考资源之一。如何注入环境?或者我可以学习一些链接?只需在环境
类型的字段上添加@Autowired
。还要注意,它在构造之后可用,尝试在构造函数中访问它将失败。使用环境
为属性提供了一个抽象,并允许您(例如)将它们放在属性文件或JNDI或web.xml中(或者您可以混合使用)。它可以清除您的代码与servlet上下文的关联。@M.Deinum感谢您的帮助!@value不起作用(show me null),但是Maciej Walkowiak已经解决了我的问题。非常感谢您的帮助并教会了我一些东西。虽然它应该起作用,但我假设您有一个上下文:注释配置和上下文:属性占位符,后者不需要从中加载属性的位置。我真的建议使用环境
而不是ServletContext
,因为这基本上将您的bean与web联系起来,我假设您希望在应用程序中的其他地方使用它。这很有效!然而,如果我的bean有一些参数需要注入它,那么如何注入呢?就像我更新了答案一样,我认为它现在完全解决了你的问题谢谢@Maciej Walkowiak!你的解决方案现在解决了我的需要。基于此解决方案,我还有一个问题。假设applicationtinfo是Bean A,someServices是Bean B。Bean B从构造函数实例化并注入Bean A(示例:)。因此,我的排序是使用servlet上下文实例化Bean A(以便从context.xml中获得一些值),然后实例化Bean B。您的解决方案能够使Bean A在注入Bean B之前已经收集信息吗?我将在发表此评论后尝试此方案。根据我的上述方案,顺序是:Bean A构造函数,然后是Bean A@postconstruct init(),然后是Bean B构造函数。完全回答我的问题!非常感谢,非常感谢!
<bean class="demo.ApplicationInfo">
<constructor-arg name="fooService" ref="fooService" />
</bean>
<bean id="fooService" class="demo.FooService"/>
public class ApplicationInfo {
@Value("${parameterName}")
private String parameter;
}