복붙노트

[SPRING] @Autowired 객체가 null 인 이유는 무엇입니까?

SPRING

@Autowired 객체가 null 인 이유는 무엇입니까?

나는 아래의 JSF 관리 빈을 가지고있다 :

package com.example;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@ManagedBean
public class Bean implements Serializable {

    @Autowired
    private SomeService someService;

    public void update() {
        someService.update(someEntity);
    }

    // ...
}

그리고 아래 봄 서비스 :

@Transactional
@Repository
public class SomeService {

    @Autowired
    private SessionFactory sessionFactory;

    // ...
}

Spring은 다음과 같이 구성됩니다.

<?xml version="1.0" encoding="UTF-8"?>
<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"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <description>Example configuration to get you started.</description>

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

    <!-- To add exception translation to a template-less Hibernate DAO -->
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

    <context:annotation-config />

    <!-- Database Configuration -->
    <import resource="database/DataSource.xml" />
    <import resource="database/Hibernate.xml" />
    <tx:annotation-driven />
 </beans>

JSF 관리 bean update () 메소드를 호출하면 NullPointerException이 발생합니다.

javax.faces.FacesException: #{bean.update()}: java.lang.NullPointerException
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:117)
    at org.springframework.faces.webflow.FlowActionListener.processAction(FlowActionListener.java:71)
    at org.springframework.faces.model.SelectionTrackingActionListener.processAction(SelectionTrackingActionListener.java:55)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:786)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1251)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.ocpsoft.rewrite.servlet.RewriteFilter.doFilter(RewriteFilter.java:191)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
Caused by: javax.faces.el.EvaluationException: java.lang.NullPointerException
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:101)
    ... 28 more
Caused by: java.lang.NullPointerException
    at com.example.Bean.update(Bean.java:45)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:278)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
    ... 29 more

즉 someService는 null입니다. 나는 왜 내 인생에 대해 생각할 수 없다. 나는 콩의 맨 위에 @Component를 선언했다. 그래서 @Autowired가 그냥 일하기를 기대합니다. 어떤 도움이 될 것입니다!

해결법

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

    1.여기에서 시도하는 것처럼 주석과 컨텍스트를 함께 사용할 수 없습니다.

    여기에서 시도하는 것처럼 주석과 컨텍스트를 함께 사용할 수 없습니다.

    먼저 Spring 빈 (또는 실제로 모든 빈)을 JSF 관리 빈에 삽입하려면 @ManagedProperty 주석을 사용한다. 그래서, 다른 모든 것이 올바르게 설정 되었다면 (Spring Faces EL resolver를 포함하여 Spring-wise), 다음을 가져야합니다 :

    @ManagedProperty("#{someService}")
    private SomeService someService;
    

    클래스를 @ManagedBean으로 선언하면 Spring의 컨텍스트 외부에있게됩니다. 결과적으로 @Autowired는 여기에 아무런 힘이 없습니다. 봄은 문맥 밖의 어떤 것에도 아무것도 삽입 할 수 없습니다.

    @Component와 @Autowired의 쌍을 사용하려면 JSF 관리 빈 대신 Spring 관리 빈이되도록 @ManagedBean을 제거해야한다.

  2. from https://stackoverflow.com/questions/23463358/why-is-my-autowired-object-null by cc-by-sa and MIT license