JSF 2注入的Spring bean / @ManagedProperty与无XML服务 [英] JSF 2 inject Spring bean/service with @ManagedProperty and no xml
问题描述
我想用JSF注释和一些春天
注释注入一个Spring bean /服务为一体的JSF托管bean。
(在JSF豆我只想使用JSF的注解)
我不想使用像 @Named
/ @Inject
注释。
I would like to use jsf annotations and some spring
annotations to inject a spring bean/service into a jsf managed bean.
(on the jsf bean i only want to use jsf annotations)
I dont want to use annotations like @named
/ @inject
.
我试图在网络上找到,但没有任何运气的解决方案。
I have tried to find a solution on the net but without any luck.
示例
@ManagedBean
@ViewScoped
public class MyBean {
@ManagedProperty(value = "#{mySpringBean}")
private MySpringBean mySpringBean;
public void setMySpringBean(MySpringBean mySpringBean) {
this.mySpringBean = mySpringBean;
}
public void doSomething() {
//do something with mySpringBean
}
}
是这样的事情可以不使用XML。例如,
我不喜欢用类似
Is something like this possible without the use of xml. For example, I would NOT like to use something like
FacesContextUtils.getWebApplicationContext(context).getBean("MySpringBean");
或 faces-config.xml中
<managed-bean>
<managed-bean-name>myBean</managed-bean-name>
<managed-bean-class>com.mytest.MyBean</managed-bean-class>
<managed-bean-scope>view</managed-bean-scope>
<managed-property>
<property-name>mySpringBean</property-name>
<value>#{mySpringBean}</value>
</managed-property>
</managed-bean>
是一样的东西上面可能有注释和无
定义所有的JSF豆/属性和春天豆/性能
在配置XML文件中每个bean?
Is something like the above possible with annotations and without defining all the jsf beans/properties and the spring beans/properties for every bean in the config xml files?
推荐答案
如果你已经有了Spring容器,为什么不使用它的@Autowired注解。为此,更新faces-config.xml中通过博尼的建议。在此之后,这些听众,然后添加到您的web.xml
If you already have Spring container why not use its @Autowired annotation. For that, Update your faces-config.xml as suggested by Boni. Then add these listeners to your web.xml after this
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
然后将这些添加到你的applicationContext.xml
Then add these to your applicationContext.xml
<context:component-scan base-package="com.examples" />
现在你可以使用Spring的注解,你的bean将是这样的:
Now you can use Spring annotations and your bean will be something like this:
package com.examples;
@Component
@Scope(value="request")
public class MyBean {
@Autowired
private MySpringBeanClass mySpringBean;
}
与@Service标注您MySpringBeanClass
Annotate your MySpringBeanClass with @Service
另请参见:
- @Scope("request") not working
这篇关于JSF 2注入的Spring bean / @ManagedProperty与无XML服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!