2013-02-21 158 views
0

我新与注释,我想JS(2.0)弹簧(3.1)集成,我可以整合这两个framewoks,但我没有JSF的viewScope因为它不是'可用。我想使用注释在jsf managedBean中自动注入spring bean,但是我不能因为Spring只支持会话并请求范围bean。指定弹簧豆与注释

我使用一个Util类来检索bean。 “SprigUtil.getBean( '豆')”。当我需要的时候手动撤销弹簧豆。

我想要做一些这样的

@CustomAnnotation( 'beanTest') 私人豆豆;

因此,bean Atributte将被设置为beanTest bean。

我的目标(姑且弹簧)也知道该怎么做了一些类似这样的

@assing(“家”) 私人字符串的地方;

当我调用getMethod获取“House”时。 instance.getPlace()返回'House'

注意: 我知道@Autowired,但我不能使用它,因为ViewScope在spring-jsf集成中不可用。 我读过关于手动实现视图范围,但想尝试不同的解决方案。

编辑:

我FacesConfig:

我FacesConfig:

<?xml version="1.0" encoding="UTF-8"?> 
<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_1.xsd" 
    version="2.1"> 
    <application> 
     <el-resolver>    org.springframework.web.jsf.el.SpringBeanFacesELResolver 
     </el-resolver> 
    </application> 
</faces-config> 

而且我appContext

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 

    <context:component-scan base-package="*" /> 

</beans> 

我的Spring bean

@Component 
public class ProductService{ 

} 

我的Managed Bean的

@Component 
@Scope("request")//I need to use @Scope("view") but it doesn't exist 
public ProductBean implements Serializable{ 
@Autowired 
ProductService productoService; 

} 

如果我使用JSF的注解@ManagedBean和@ViewScoped productService没有注入(为null)

+0

能否请您展示您的spring applicationContext? – shevchyk 2013-02-21 22:53:30

回答

0

可以Spring Bean注入到视图作用域确定使用@ManagedProperty

管理豆

对于弹簧组件称为b

@ManagedProperty("#{B}") 
private B b; 

public void setB(B b) { 
this.b = b; 
} 

应该工作。

至于你已经发布的代码,组件是一个春天的注释,使用ViewScoped必须使用ManagedBean注解注释类:

@ManagedBean 
@ViewScoped 
public ProductBean implements Serializable { 
@ManagedProperty("#{productService}") 
private ProductService productService; 

public void setProductService(ProductService productService) { 
    this.productService = productService; 
    } 
} 

你可能想看看下面的链接,以便更好地理解JSF 2.0中的示波器Communication in JSF 2.0

+0

我会试试....但是当我在bean中使用@ViewScoped时,不要注入任何东西 – user1655510 2013-02-22 12:28:42

+0

如果使用其他作用域,注入是否工作? – 2013-02-22 12:44:47

+0

是的,在jsf 2弹簧集成 支持5种类型的bean作用域:单例,原型,请求,会话和globalSession。我需要ViewScope,但是我不能使用它,如果我使用@ViewScoped注释,那么每个mb中的Autowired都不起作用。 – user1655510 2013-02-22 14:43:24