春天不是在注入我的班级

时间:2014-02-19 15:29:08

标签: spring

当我尝试使用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 个答案:

答案 0 :(得分:0)

问题在于此代码:

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

}

只有在使用Spring IoC本身创建对象时,Spring IoC才能使用Autowire将依赖注入对象。如果您只是使用

创建对象
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实例的逻辑,那么您仍有很多选项。您可以在Verifier类和MathVerifierFactory中自动装配工厂自动装配所有可能的MathVerifiers实例。它将类似于:

class MathVerifierFactory {
    @Autowired
    MocalMathVerifier mocalMathVerifier;

    @Autowired
    SomeOtherMathVerifier someOtherMathVerifier;

    public MathVerifer getMathVerifier(Short mathtypeId){

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

但请注意,在这种情况下,您最终可能会在不同的clints之间共享MathVerifier的相同实例,这可能会产生共享状态和并发性问题。

另一种选择是将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();
         }
     }
}