Dim*_*man 15 java jsf spring annotations jsf-2
我想使用jsf注释和一些spring注释将spring bean/service注入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
}
}
Run Code Online (Sandbox Code Playgroud)
没有使用xml就可以实现这样的功能.例如,我不想使用类似的东西
FacesContextUtils.getWebApplicationContext(context).getBean("MySpringBean");
Run Code Online (Sandbox Code Playgroud)
或者在 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>
Run Code Online (Sandbox Code Playgroud)
是否可以使用注释,并且没有为config xml文件中的每个bean定义所有jsf bean/properties和spring beans/properties?
Rav*_*avi 14
如果您已经有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>
Run Code Online (Sandbox Code Playgroud)
然后将这些添加到applicationContext.xml中
<context:component-scan base-package="com.examples" />
Run Code Online (Sandbox Code Playgroud)
现在你可以使用Spring注释,你的bean将是这样的:
package com.examples;
@Component
@Scope(value="request")
public class MyBean {
@Autowired
private MySpringBeanClass mySpringBean;
}
Run Code Online (Sandbox Code Playgroud)
使用@Service注释MySpringBeanClass
也可以看看:
Bon*_*oni 11
将此代码放在faces-config.xml中
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
</faces-config>
Run Code Online (Sandbox Code Playgroud)
然后在ManageBean Constructor中调用;
WebApplicationContext ctx = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
mySpringBean = ctx.getBean(MySpringBean.class);
Run Code Online (Sandbox Code Playgroud)
MySpringBean意味着你的Spring Bean类
归档时间: |
|
查看次数: |
27971 次 |
最近记录: |