问题描述
我想使用 jsf 注释和一些 spring将 spring bean/服务注入 jsf 托管 bean 的注释.(在 jsf bean 上我只想使用 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 beans/properties 和 spring beans/properties配置 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 注解.为此,请按照 Boni 的建议更新您的 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") 不起作用