我想使用 jsf 注释和一些 spring
将 spring bean/服务注入(inject) jsf 托管 bean 的注释。
(在 jsf bean 上我只想使用 jsf 注释)
我不想使用像 @named
/@inject
这样的注解。
我试图在网上找到解决方案,但没有任何运气。
例子
@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 的情况下,这样的事情是否可行。例如, 我不想使用类似的东西
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?
最佳答案
如果你已经有了 Spring 容器,为什么不使用它的 @Autowired 注解。为此,请按照 Boni 的建议更新您的 faces-config.xml。然后在此之后将这些监听器添加到您的 web.xml 中
<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
<context:component-scan base-package="com.examples" />
现在您可以使用 Spring 注释,您的 bean 将是这样的:
package com.examples;
@Component
@Scope(value="request")
public class MyBean {
@Autowired
private MySpringBeanClass mySpringBean;
}
使用 @Service 注释您的 MySpringBeanClass
另见:
关于java - JSF 2 使用 @ManagedProperty 注入(inject) Spring bean/service 而没有 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8925170/