2014-02-19 57 views
0

當我試圖使用mathUtil實例,因爲它沒有被注入時,我得到空指針異常。我認爲創造就是成功,但不注射?春天不注入我的課

下面

是我的課得到的錯誤:

package com.cts.mms.process.service 
    @Component 
    public class MocalMathVerifier implements MathVerifer { 

     private static final Logger log = LoggerFactory.getLogger(MocalMathVerifier .class); 

     @Autowired 
     MathUtil mathUtil; 

     @Autowired 
     mathLogger mathLogger; 

public verifyMaths(){ 
     List<MathObject> mathObjList = mathUtil.getctList(mathList); 

} 
} 

MY context.xml的是如下

<beans default-autowire="no" 
    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.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-2.5.xsd"> 

    <context:property-placeholder location="classpath*:mmsogenmerfier.properties" /> 
    <context:annotation-config /> 
    <context:component-scan base-package="com.cts.mms.process"></context:component-scan> 

    <bean id="log4jInitialization" 
     class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> 
     <property name="targetClass" value="org.springframework.util.Log4jConfigurer" /> 
     <property name="targetMethod" value="initLogging" /> 
     <property name="arguments"> 
      <list> 
       <value>classpath:log4j.properties</value> 
      </list> 
     </property> 
    </bean> 

    <bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> 
     <property name="classesToBeBound"> 
      <array> 
       <value>com.cts.mms.process.oxm.metagen</value> 
      </array> 
     </property> 
    </bean> 

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> 
     <property name="host" value="localhost" /> 
     <property name="port" value="25" /> 
     <property name="username" value="" /> 
     <property name="password" value="" /> 
     <property name="javaMailProperties"> 
      <props> 
       <prop key="mail.transport.protocol">smtp</prop> 
       <!-- <prop key="mail.smtp.auth">true</prop> --> 
       <!-- <prop key="mail.smtp.starttls.enable">true</prop> <prop key="mail.smtp.startssl.enable">true</prop> --> 
       <!-- <prop key="mail.debug">true</prop> --> 
      </props> 
     </property> 
    </bean> 

    <bean id="simpleMailMessage" class="org.springframework.mail.SimpleMailMessage"> 
     <property name="from" value="${smtp.email.from}" /> 
     <property name="to" value="${smtp.email.to}" /> 
     <property name="subject" value="${smtp.email.subject}" /> 
     <!-- <property name="text" value ="${stmp.email.bodyText}"/> --> 

     <property name="text"> 
     <value> 
     <![CDATA[ 
      Dear %s, 
      Mail Content : %s 
     ]]> 
    </value> 
     </property> 
    </bean> 



</beans> 

下面是MathUtil類定義。在帖子中刪除了這裏認爲沒有必要的內容。

package com.cts.mms.process.common; 

@Component("mathUtil") 
public class MathUtil { 

public List<MathObject> getctList(List<Maths> mathList) { 


} 

下面是MocalMathVerifier是如何被訪問

package com.cts.mms.process.service; 
@Component 
public class MathVerifierFactory { 

public MathVerifer getMathVerifier(Short mathtypeId){ 

    if(mathtypeId==100) 
      return new MocalMathVerifier(); 

    } 
} 



package com.cts.mms.process.service; 
@Component("verifier") 
public class Verifier { 

    private static final Logger log = LoggerFactory.getLogger(Verifier.class); 
    @Autowired 
    private MathVerifierFactory mathVerifierFactory; 

public void verifyMathInfo() { 

mathVerifierFactory.getMathVerifier(mathId).verifyPaths(mathList); 

} 
+1

我看到一堆沒有出現相關的東西,基本上沒有什麼是。 SSCCE請。 –

+0

是的,因爲我有18個類和SMTP的東西,我不能在這裏發佈所有東西 – mahesh

+2

請問您可以添加MathUtils的定義和如何獲得MocalMathVerifier的實例? –

回答

0

的問題是在此代碼:

if(mathtypeId==100) 
     return new MocalMathVerifier(); 

} 

的Spring IoC將能夠注入的依賴將使用自動裝配只有當一個對象對象是使用Spring IoC本身創建的。如果你正在創建一個對象,只需使用

new MocalMathVerifier(); 

Spring沒有辦法發現是時候在新創建的對象中注入依賴關係了。

您可以更改下列方式類:

@Component("verifier") 
public class Verifier { 

    private static final Logger log = LoggerFactory.getLogger(Verifier.class); 
    @Autowired 
    private MocalMathVerifier mathVerifier; 

public void verifyMathInfo() { 

mathVerifier.verifyPaths(mathList); 

} 

正如你所看到的,現在你問Spring構建你的依賴(mathVerifier)。當Spring創建(注入)它時,它也會注入它的依賴關係(mathUtil,mathLogger)。

如果工廠包含選擇/創建MathVerifier實例的邏輯,您仍然有很多選項。您可以在驗證程序類和MathVerifierFactory中自動裝配工廠自動裝載所有可能的MathVerifiers實例。這將是這樣的:

class MathVerifierFactory { 
    @Autowired 
    MocalMathVerifier mocalMathVerifier; 

    @Autowired 
    SomeOtherMathVerifier someOtherMathVerifier; 

    public MathVerifer getMathVerifier(Short mathtypeId){ 

     if(mathtypeId==100) 
      return mocalMathVerifier;  
     } else { 
      return someOtherMathVerifier; 
     } 
    } 
} 

但要注意,在這種情況下,你可能最終會共享不同clints之間MathVerifiers的相同的情況下,可以創建一個共享狀態和併發問題。

另一種選擇將是注入MathUtil和MathLogger到工廠創建MathVerifier情況下,手動:

class MathVerifierFactory { 
    @Autowired 
    MathUtil mathUtil; 

    @Autowired 
    MathLogger mathLogger; 

    public MathVerifer getMathVerifier(Short mathtypeId){ 

     if(mathtypeId==100) 
      return new MocalMathVerifier(mathUtil, mathLogger);  
     } else { 
      return new SomeOtherMathVerifier(mathLogger); 
     } 
    } 
} 

與上次方法的問題是,MathVerifier的不同子類可以有不同的依賴性,則可能需要所有這些都在MathVerifierFactory中,這可能不是一個好的設計決策。你可以做的是爲MathVerifier的每個子類型創建一個單獨的工廠類。它會是這樣的:

class MocalMathVerifierFactory { 
    @Autowired 
    MathUtils mathUtils; 

    @Autowired 
    MathLogger mathLogger; 

    MocalMathVerifier createMathVerifier() { 
     return new MocalMathVerifier(mathUtils, mathLogger); 
    } 
} 

class SomeOtherMathVeriferFactory { 
    @Autowired 
    MathLogger mathLogger; 

    MocalMathVerifier createMathVerifier() { 
     return new MocalMathVerifier(mathLogger); 
    } 
} 

class MathVerifierFactory { 
    @Autowired 
    MocalMathVerifierFactory mocalMathVerifierFactory; 

    @Autowired 
    SomeOtherMathVerifierFactory someOtherMathVerifierFactory; 

    public MathVerifer getMathVerifier(Short mathtypeId){ 

     if(mathtypeId==100) 
      return mocalMathVerifierFactory.createMathVerifier();  
     } else { 
      return someOtherMathVerifier.createMathVerifier(); 
     } 
    } 
} 
+0

謝謝IvanMushketyk。但是在這裏我不知道哪個類將被調用,因爲MathVerifierFactory工廠類有更多類似於MocalMathVerifier的7個類。我正在根據傳遞給工廠方法的mathId迴應正確的類實例。我想我可能需要使用Spring自動裝配來爲所有在MathVerifierFactory類中實例化的類自行裝入? – mahesh

+0

@mahesh我已經更新了我的答案,爲您的案例提供了一些食譜。 –

+0

謝謝@ IvanMushketyk,我明白你的觀點。我不確定是否在MathVerifierFactory類中使用@autowired聲明所有8個類是好的做法?沒有或沒有任何其他方式。 – mahesh