0
votes

I'm new with annotations, I'm trying the jsf(2.0) spring (3.1) integration, I could integrate the two frameworks but I don't have the viewScope in JSF because it isn't available. I want to use annotations to inject automatically spring beans in the jsf managedBean, but I can't because Spring only supports sessions and request scope beans.

I use a Util class who retrieves the bean. "SprigUtil.getBean('bean')". And manually retrieve spring beans when I need.

I want to do some like this

@CustomAnnotation('beanTest')
private Bean bean;

So, the bean attributte will be set with the beanTest bean.

My objective (leaving aside spring) is know how to do some like this

@assign('House')
private String place;

and when I call the getMethod obtain "House". instance.getPlace(), return 'House'

Note: I Know about the @Autowired but I can't use that because ViewScope is not available in spring-jsf integration.

I read about implement the view scope manually but a want to try a different solution.

Edit:

My 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>

And my 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>

My Spring bean

@Component
public class ProductService{

}

My Managed Bean

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

}

If I use the jsf annotations @ManagedBean and @ViewScoped productService is not injected (is null)

1
could you please show your spring applicationContext?Sergii Shevchyk

1 Answers

0
votes

You can inject spring beans into view scoped managed beans by using @ManagedProperty

For a spring component named B

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

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

should work.

As for the code you have posted, Component is a Spring annotation, to use ViewScoped you must annotate your class with the ManagedBean annotation:

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

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

You might want to check out the following link to better understand scopes in JSF 2.0 Communication in JSF 2.0