Spring MVC | @Inject注释给出NullPointerException

时间:2017-11-20 12:26:24

标签: spring spring-mvc nullpointerexception

我有春天的MVC项目以及流口水。

该项目有一个控制器如下:

package com.kiespring.mvc.controller;

import org.kie.spring.beans.Person;
import org.kie.spring.KieSpringUtil;
import org.springframework.context.ApplicationContext;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.inject.Inject;

@Controller
@RequestMapping("/welcome")
public class HelloController {

    @Inject
   private KieSpringUtil rulesExecutor;

    @RequestMapping(method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {

        System.out.println("Inside the Method Firstttt::");
        Person person = new Person("HAL");
        System.out.println("Inside the Method Firstttt:: 1");

        //Error while executing below line
        rulesExecutor.executeRules(person);

        System.out.println("Inside the Method Firstttt:: 2");
        model.addAttribute("isHappy", person.isHappy());
        model.addAttribute("message", "Spring 3 MVC Hello World");
        return "hello";

    }

}

KieSpringUtil类位于::

之下
package org.kie.spring;

import org.kie.api.KieBase;
import org.kie.api.runtime.StatelessKieSession;
import org.kie.spring.beans.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import javax.inject.Inject;

@Service
public class KieSpringUtil {

    @Inject
    ApplicationContext context;

    public void executeRules(Person person){
        System.out.println("Inside executeRules");
        KieBase kbase = (KieBase) context.getBean("kbase1");
        System.out.println("Inside executeRules1");
        StatelessKieSession statelessKieSession =  kbase.newStatelessKieSession();
        System.out.println("Inside executeRules2");
        statelessKieSession.execute(person);
    }

}

mvc-dispatcher-servlet.xml如下:

<beans
 xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:kie="http://drools.org/schema/kie-spring"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://drools.org/schema/kie-spring file:\C:\Users\677410\files\kie-spring.xsd">

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

    <context:component-scan base-package="com.kiespring.mvc.controller" />
    <context:component-scan base-package="org.kie.spring.beans" /> 
  <bean id="kieUtil" class="org.kie.spring.KieSpringUtil"/>

  <kie:kmodule id="sample_module">
    <kie:kbase name="kbase1" packages="org.drools.spring.sample"/>
  </kie:kmodule>

  <bean id="kiePostProcessor" class="org.kie.spring.KModuleBeanFactoryPostProcessor"/>
</beans>

当它尝试执行KieSpringUtil类的rulesExecutor方法时,会给出NullPointerException(NPE)。 KieSpringUtil已经使用@Inject进行了注释,因此它应该可用。为什么它呈现为null?

Info :: Spring版本:3.0.5.RELEASE        Tomcat:7.0.1

1 个答案:

答案 0 :(得分:-1)

我猜你应该使用Spring的@Autowired代替@Inject