2013-02-21 157 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