簡體   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