繁体   English   中英

Jboss RestEasy应用程序中的@Autowired空指针异常

[英]@Autowired null pointer exception in Jboss RestEasy Application

我正在将我的应用程序从Jersey更改为RestEasy应用程序。 在Wildfly 8.2中部署时,它与Web.xml中的Jersey配置无关。

但是在将配置从Jersey更改为RestEasy之后,它仍然被部署,但是在尝试访问@Autowired对象的particualr方法时出现空指针异常。 更清楚地说,自动Autowired不会在启动时创建对象,这会导致此错误。 我试图清除此错误已超过一天。 任何帮助,将不胜感激

在web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
    <display-name>Web Application</display-name>
    <distributable />
    <listener>
        <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
    </listener>

    <listener>
        <listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener</listener-class>
    </listener>

    <!-- Context Configuration locations for Spring XML files -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:/applicationContext.xml
            classpath:/applicationContext-resources.xml
            classpath:/applicationContext-dao.xml
            classpath:/applicationContext-service.xml
            classpath*:/applicationContext.xml
            /WEB-INF/applicationContext*.xml
        </param-value>
    </context-param>

    <context-param>
        <param-name>resteasy.scan</param-name>
        <param-value>false</param-value>
    </context-param>

    <context-param>
        <param-name>resteasy.servlet.mapping.prefix</param-name>
        <param-value>/api</param-value>
    </context-param>

    <filter>
        <filter-name>SessionFilter</filter-name>
        <filter-class>com.promarvel.filter.SessionFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>SessionFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <servlet>
        <servlet-name>resteasy-servlet</servlet-name>
        <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>resteasy-servlet</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>login.html</welcome-file>
    </welcome-file-list>

</web-app>

applicationContext.xml中

<?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"
    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.xsd"
    default-lazy-init="true">

    <!-- Activates scanning of @Autowired -->
    <context:annotation-config />

    <!-- Activates scanning of @Repository and @Service -->
    <context:component-scan base-package="com.myPackage" />

    <!-- Add new DAOs here -->

    <!-- Add new Managers here -->
</beans>

如果我尝试随时访问自动装配对象,则会收到错误消息。 例如,这是我的SessionFilter,它尝试访问UserService.java

package com.myPackage.filter;

import java.util.Date;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.myPackage.model.SessionDetails;
import com.myPackage.service.UserService;
import com.myPackage.util.DateUtil;

@Component
public class SessionFilter implements Filter {

    @Autowired
    private UserService userService;

    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) {

        try {

            HttpServletRequest request = (HttpServletRequest) req;
            HttpServletResponse response = (HttpServletResponse) res;
            String url = request.getRequestURI();
            String sessionId = request.getParameter("sessionId");

            SessionDetails sessionDetails = new SessionDetails();
            sessionDetails.setBrowserDetails(request.getHeader("User-Agent"));
            sessionDetails.setIpAddress("192.168.1.1");
            sessionDetails.setLoginTime(new Date());
            sessionDetails.setLoginUserName("UNKNOWN USER");
            sessionDetails.setActive(false);
            sessionDetails.setLoginStatus("INVALID SESSIONID");
            sessionDetails.setLastReplicationTime(new Date());

            //System.out.println(userService);   => Null Pointer Exception
            userService.saveLoginUserDetails(sessionDetails);    => Null Pointer Exception
            chain.doFilter(req, response);
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

}

我尝试完成许多帖子中建议的每种解决方案,即使我添加了一个空的beans.xml文件,它仍然无法正常工作。

的build.gradle

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse-wtp'
apply plugin: 'eclipse'

// Uses JDK 8
sourceCompatibility = 1.8
targetCompatibility = 1.8

// 1. Get dependencies from Maven local repository
// 2. Get dependencies from Maven central repository
repositories {
    mavenLocal()
    mavenCentral()
    maven  {
        url "http://repo1.maven.org/maven2"
    }
}

task explodedWar(type: Copy) {
    into "$buildDir/libs/myPackage"
    with war
}

configurations {
    provided
}
  sourceSets {
    main { compileClasspath += configurations.provided }
}

//Project dependencies
dependencies {
    //JUnit testing framework
    compile 'junit:junit:4.4'

    //Spring framework core
    compile 'org.springframework:spring-web:4.1.4.RELEASE'
    compile 'org.springframework:spring-core:4.1.4.RELEASE'
    compile 'org.springframework:spring-context:4.1.4.RELEASE'
    compile 'org.springframework:spring-context-support:4.1.4.RELEASE'
    compile 'org.springframework:spring-orm:4.1.4.RELEASE'

    compile 'org.springframework.security:spring-security-core:4.0.0.RELEASE'

    //jersyclient for REST API
    //compile 'com.sun.jersey.contribs:jersey-spring:1.18.3'
    //compile 'com.sun.jersey:jersey-server:1.18.3'

    //MySQL database driver
    compile 'mysql:mysql-connector-java:5.1.34'

    //Hibernate framework 
    compile 'org.hibernate:hibernate-core:4.3.8.Final'
    compile 'commons-dbcp:commons-dbcp:1.2.2'

    //Servlet API
    compile 'javax.servlet:servlet-api:2.5'

    //Base-64 Apache commons
    compile 'commons-codec:commons-codec:1.10'

    //log4j
    compile 'log4j:log4j:1.2.17'
    compile 'org.slf4j:slf4j-simple:1.7.10'

    //XmlBeans Equity Valuation
    compile 'org.apache.xmlbeans:xmlbeans:2.6.0'

    //Poi Equity Valuation
    compile 'org.apache.poi:poi:3.10.1'

    //Poi ooxml Equity Valuation
    compile 'org.apache.poi:poi-ooxml:3.10.1'

    //Poi ooxml Schemas Equity Valuation
    compile 'org.apache.poi:poi-ooxml-schemas:3.10.1'

    //Jacob Equity Valuation
    compile 'jacob:jacob:1.18-M2'

    //Google gson
    compile 'com.google.code.gson:gson:2.3.1'

    //compile 'com.sun.jersey:jersey-json:1.18.3'

    provided 'org.jboss.resteasy:resteasy-jaxrs:3.0.11.Final'

    war.dependsOn explodedWar

} 

尝试将<tx:annotation-driven/>放入application.xml中,并将<context:component-scan base-package="com.myPackage" /><context:component-scan base-package="com.myPackage.*" />

@Autowire的空指针结果主要是由于两个原因:

  1. 该类均未标记为Spring Component。
  2. 应用程序上下文无法识别bean。

因此,请确保根据您的用例,用@Component或子注释@ Controller,@ Service,@ Repository注释UserService类。 另外,您正在扫描applicationContext.xml中的包。

<mvc:annotation-driven></mvc:annotation-driven>
    <context:component-scan base-package="your.package.with.UserService"></context:component-scan>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM