使用注释指定spring bean(Assign spring beans with annotations)

编程入门 行业动态 更新时间:2024-10-26 03:33:09
使用注释指定spring bean(Assign spring beans with annotations)

我是新的注释,我正在尝试jsf(2.0)spring(3.1)集成,我可以集成两个framewoks但我没有JSF中的viewScope,因为它不可用。 我想使用注释在jsf managedBean中自动注入spring bean,但我不能因为Spring只支持会话并请求范围bean。

我使用一个Util类来检索bean。 “SprigUtil.getBean( '豆')”。 并在我需要时手动退回春豆。

我想做这样的事

@CustomAnnotation('beanTest')私有Bean bean;

因此,将使用beanTest bean设置bean Atributte。

我的目标(抛开春天)知道如何做这样的事情

@assing('House')私人弦乐场所;

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

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

编辑:

我的面孔配置:

我的面孔配置:

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

我的春豆

@Component public class ProductService{ }

我的托管豆

@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)

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

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

I want to do some like this

@CustomAnnotation('beanTest') private Bean bean;

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

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

@assing('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 avaliable in spring-jsf integration. I read about implement the view scope manually but a want to try a different solution.

Edit:

My FacesConfig:

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)

最满意答案

您可以使用@ManagedProperty将spring bean注入视图范围的托管bean

对于名为B的弹簧组件

@ManagedProperty("#{B}") private B b; public void setB(B b) { this.b = b; }

应该管用。

至于你发布的代码,Component是一个Spring注释,要使用ViewScoped,你必须用ManagedBean注释注释你的类:

@ManagedBean @ViewScoped public ProductBean implements Serializable { @ManagedProperty("#{productService}") private ProductService productService; public void setProductService(ProductService productService) { this.productService = productService; } }

您可能需要查看以下链接以更好地了解JSF 2.0中JSF 2.0 通信的范围

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

更多推荐

bean,jsf,spring,使用,电脑培训,计算机培训,IT培训"/> <meta name="descriptio

本文发布于:2023-07-31 19:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1347291.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:注释   spring   bean   annotations   beans

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!