當我試圖使用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);
}
我看到一堆沒有出現相關的東西,基本上沒有什麼是。 SSCCE請。 –
是的,因爲我有18個類和SMTP的東西,我不能在這裏發佈所有東西 – mahesh
請問您可以添加MathUtils的定義和如何獲得MocalMathVerifier的實例? –