복붙노트

[SPRING] JSF 2는 @ManagedProperty 및 xml이없는 Spring bean / service를 주입합니다.

SPRING

JSF 2는 @ManagedProperty 및 xml이없는 Spring bean / service를 주입합니다.

jsf 주석과 스프링을 사용하고 싶습니다. 주석을 사용하여 jsf 관리 빈에 스프링 빈 / 서비스를 주입합니다. (jsf bean에서만 jsf annotation을 사용하기를 원한다) @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
    }
}

xml을 사용하지 않고 이런 일이 가능합니까? 예를 들어, 나는 뭔가를 쓰고 싶지 않다.

FacesContextUtils.getWebApplicationContext(context).getBean("MySpringBean");

또는 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>

주석과 함께 위와 같은 것이 가능합니다. 모든 jsf beans / properties와 spring beans / properties를 정의합니다. config xml 파일의 모든 bean?

해결법

  1. ==============================

    1.Spring 컨테이너가 이미 있다면 @Autowired 주석을 사용하지 않을까요? 이를 위해 Boni가 제안한대로 faces-config.xml을 업데이트하십시오. 그런 다음이 리스너를 web.xml에 추가하십시오.

    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>
    

    그런 다음 applicationContext.xml에 추가합니다.

    <context:component-scan base-package="com.examples" />
    

    이제 Spring 어노테이션을 사용할 수 있으며 bean은 다음과 같이 될 것이다.

    package com.examples;
    @Component
    @Scope(value="request")
    public class MyBean {
        @Autowired
        private MySpringBeanClass mySpringBean;
    }
    

    @Service로 MySpringBeanClass에 주석을 추가하십시오.

    참조 :

  2. ==============================

    2.이 코드를 faces-config.xml에 넣으십시오.

    이 코드를 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>
    

    그런 다음 Managed Bean Constructor 호출에서;

    WebApplicationContext ctx =  FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
    mySpringBean = ctx.getBean(MySpringBean.class);
    

    MySpringBean은 Spring Bean 클래스를 의미합니다.

  3. ==============================

    3.web.xml과 applicationContext.xml에서 Spring을 올바르게 설정했다고 가정합니다. faces-config.xml에 다음 항목을 만듭니다.

    web.xml과 applicationContext.xml에서 Spring을 올바르게 설정했다고 가정합니다. faces-config.xml에 다음 항목을 만듭니다.

    <application>
         <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    </application>
    

    위에 주어진 샘플 코드는 괜찮아 보입니다. 위의 항목은 Managed Property가 JSF가 관리하는 Bean에서 처음 발견 될 경우 찾을 수없는 경우 Spring에서 관리하는 Bean에서 검색됩니다. Spring bean은 적절한 주석을 표시해야하고 @ManagedProperty에 주어진 이름은 bean에 주어진 default / name과 일치해야합니다.

    @Boni에 언급 된 것처럼 자동 주입은 필요하지 않습니다. 원하는대로 설정을 사용했습니다.

    사이드 노트 : 뷰 범위를 선택 했으므로이 링크를 살펴보십시오. @ViewScoped의 이점과 함정

  4. from https://stackoverflow.com/questions/8925170/jsf-2-inject-spring-bean-service-with-managedproperty-and-no-xml by cc-by-sa and MIT license