当前位置: 首页 > 知识库问答 >
问题:

GWT+Spring Boot@Autowired不工作

相德宇
2023-03-14

我在GWT+Spring Boot中创建了一个应用程序,当我尝试使用@autowired-时,我得到NullPointerException。显然@autowired不起作用,没有将bean插入正确的位置。我怎样才能解决这种情况?

分派传入RPC调用时发生异常

ExpectiontedException:服务方法“public abstract java.util.list com.mygwt.springbootapp.client.userservice.list()”引发意外异常:java.lang.NullPointerException

文件web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <servlet>
        <servlet-name>userService</servlet-name>
        <servlet-class>com.myGWT.springbootapp.server.UserServiceImpl</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>userService</servlet-name>
        <url-pattern>/gwtApp/service</url-pattern>
    </servlet-mapping>


    <welcome-file-list>
        <welcome-file>GWTApp.html</welcome-file>
    </welcome-file-list>
</web-app>

pom.xml

<name>spring-boot-app</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->

    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <gwt.version>2.7.0</gwt.version>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.8</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.8</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.logging.log4j</groupId>
                    <artifactId>log4j-api</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>


        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.4</version>
        </dependency>

        <!-- GWT -->
        <dependency>
            <groupId>com.google.gwt</groupId>
            <artifactId>gwt-user</artifactId>
            <version>${gwt.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.google.gwt</groupId>
            <artifactId>gwt-servlet</artifactId>
            <version>${gwt.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.google.gwt</groupId>
            <artifactId>gwt-codeserver</artifactId>
            <version>${gwt.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.google.gwt</groupId>
            <artifactId>gwt-dev</artifactId>
            <version>${gwt.version}</version>
            <scope>provided</scope>
            <exclusions>
            <exclusion>
                <artifactId>apache-jsp</artifactId>
                <groupId>org.eclipse.jetty</groupId>
            </exclusion>
            </exclusions>
        </dependency>
    </dependencies>



    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>gwt-maven-plugin</artifactId>
                    <version>${gwt.version}</version>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.5</version>
                </plugin>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>2.4.1</version>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <configuration>
                    <filesets>
                        <fileset>
                            <directory>target</directory>
                        </fileset>
                        <fileset>
                            <directory>builds</directory>
                            <includes>
                                <include>**/*.*</include>
                            </includes>
                        </fileset>
                    </filesets>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.8.2</version>
                <executions>
                    <execution>
                        <id>deploy</id>
                        <phase>deploy</phase>
                        <goals><goal>deploy</goal></goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

文件UserServiceImpl.java

package com.myGWT.springbootapp.server;

import com.myGWT.springbootapp.client.UserService;
import com.myGWT.springbootapp.entities.User;
import com.myGWT.springbootapp.repositories.UserRepository;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl extends RemoteServiceServlet implements UserService {

AnnotationConfigWebApplicationContext();

    @Autowired
    private UserRepository repository ;

    @Override
    public List<User> list() {
        return repository.findAll();
    }
}
package com.myGWT.springbootapp.repositories;


import com.myGWT.springbootapp.entities.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public interface UserRepository extends JpaRepository<User, Long> {

}
package com.myGWT.springbootapp.client;

import com.myGWT.springbootapp.entities.User;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;


import java.util.List;

@RemoteServiceRelativePath("service")
public interface UserService extends RemoteService {

  List<User> list();
}
package com.myGWT.springbootapp.client;

import com.myGWT.springbootapp.entities.User;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.view.client.ListDataProvider;

import java.util.List;

public class GWTApp implements EntryPoint {

    private  UserServiceAsync userService = GWT.create(UserService.class);

    private ListDataProvider<User> createTable (CellTable<User> table){
        TextColumn<User> idColumn =new TextColumn<User>() {
            @Override
            public String getValue(User object) {
                return object.getId().toString();
            }
        };
        TextColumn<User> loginColumn =new TextColumn<User>() {
            @Override
            public String getValue(User object) {
                return object.getLogin();
            }
        };
        table.addColumn(idColumn, "Id");
        table.addColumn(loginColumn, "Login");
        final ListDataProvider<User> dataProvider = new ListDataProvider<>();
        dataProvider.addDataDisplay(table);
        this.userService.list(new AsyncCallback<List<User>>() {
            @Override
            public void onFailure(Throwable caught) {
                Window.alert("Error: " + caught.getMessage());
            }

            @Override
            public void onSuccess(List<User> result) {
                dataProvider.getList().addAll(result);

            }
        });
        return dataProvider;
    }

    public void onModuleLoad() {
        CellTable<User> table = new CellTable<>();
        ListDataProvider<User> dataProvider = createTable(table);

        RootPanel.get().add(table);
    }
}

共有1个答案

晋坚
2023-03-14

简短的回答是:Spring没有实例化您的GWT服务;因此,它不能把任何东西带入其中。

Jetty容器实际上负责创建和映射UserServiceImpl的实例。这就是您的web.xml所描述的内容。

您的@service注释确实像您所期望的那样在Spring上下文中创建了一个Servlet实例,但Jetty并不知道这一点。过去,我从基于RemoteServiceServlet的服务中删除了@service注释,并添加了以下方法来提供@autowire支持:

public class UserServiceImpl extends RemoteServiceServlet implements UserService {

...

     @Override
     public void init() throws ServletException {
          super.init();
          SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, getServletContext());
     }

...

}

来自SpringBeanAutowiringSupportJavadoc:

基于存储在ServletContext中的当前根web应用程序上下文,对给定目标对象进行Process@Autowired注入。

在Servlet创建和boom过程中,Jetty将调用init()。这是在事情的黑客的一面,因为它桥梁两个不相关的过程,但它是为我目前的生产工作。

使用符合Servlet3.0+的容器,您可能会找到一种方法,在映射Servlet之前使用WebApplicationInitializer(javadoc)通过AnnotationConfigWebApplicationContext(javadoc)启动Spring上下文,这将允许您从那里使用实例(这将取代web.xml)。

编辑:

由于您使用的是Spring Boot,您可以浏览一下这个线程,它展示了如何通过ServletRegistrationBean映射servlet:我如何用Spring Boot注册辅助servlet?(参见Checketts的回答)

 类似资料:
  • 我正在尝试使用springboot访问数据库,但是spring应用程序在下面抛出了一个异常。 创建名为“welcome controller”的bean时出错:注入autowired依赖项失败;嵌套异常为org.springframework.beans.factory.beancreationexception:无法自动连接字段:private org.springframework.jdbc.

  • 我有一个Springboot应用程序,我的实体模型与作为依赖项包含的主应用程序分开。 我的Application.java位于此包中 我的实体模型位于这个包com.a.b中的另一个项目中 但是我得到一个错误:由:java.lang.IllegalArgumentException:不是托管类型:类引起

  • 我问了一个类似的问题,以为得到了回答。然而,昨天它停止工作,所以我要再问一遍,但我的修改类。我希望这是正确的协议.... 我有一个spring启动网站,它在启动时抛出一个空指针,因为其中一个类没有自动连线。以下是我的设置: 在子包中,我有:package com.company.product.configuration@configuration@componentScan()public cl

  • 我正在学习Spring Boot来构建应用程序。我试图用不同包中的控制器作为应用程序来构建我的第一个Spring Boot应用程序。Tomcat实例出现,但请求没有到达为URI注册的RestController。 以下是控制器类: 以下是应用程序类: Pom.xml tomcat启动时的日志: 我添加了基本包扫描,甚至尝试了注释,但当我点击URL(http://localhost:8080/abc

  • 我已经将spring boot嵌入式tomcat服务器中的保持活动超时设置为30秒。因此,我在应用程序中使用下面的内容。JAVA 然后我从我的Rest控制器Hibernate一个请求线程40秒。但是当我通过postman发出请求时,它成功返回HTTP状态代码200,而不是返回网关超时错误。 我尝试setConnectionTimeout和setKeepAliveTimeout,但它不起作用。 我错

  • 我是一个新的springboot和我正在考虑它为一个新的项目。在测试其功能时,我使用@Transactional注释总是失败。 我创建了一个小的MySql数据库,并将其连接到该数据库,设置此application.properties文件: 为什么?