- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我已经完成了@Autowired注释,并且@Component注释正在调用并调用类。尽管如此,我在“ServiceResource.java”中遇到空指针异常 -> serviceValidator 值为空。
请帮我知道我哪里出了问题?
ServiceResouce.java
package com.resolvebug.app.controller;
@Component
@Path("v1")
@Produces(APPLICATION_JSON)
public class ServiceResource {
@Autowired
private ServiceValidator serviceValidator;
@POST
@Path("/post/json")
public ServiceManager employeeLogin(ServiceManager serviceManager) {
return serviceValidator.hello(serviceManager);
}
}
ServiceValidator.java
package com.resolvebug.app.validator;
@Component
public class ServiceValidator {
@Autowired
private ServiceManager sm;
public ServiceManager hello(ServiceManager serviceManager) {
sm.setAge(serviceManager.getAge());
sm.setName(serviceManager.getName());
return sm;
}
}
ServiceManager.java
package com.resolvebug.app.handler;
@Component
public class ServiceManager {
public String name;
public int age;
public ServiceManager() {
}
public ServiceManager(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>REST Web Application using Jersey Web Application</display-name>
<servlet>
<servlet-name>rest-server</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.resolvebug.app.controller</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest-server</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
应用程序上下文
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">
<context:spring-configured />
<context:annotation-config />
<context:component-scan
base-package="com.resolvebug.app.controller" />
<mvc:annotation-driven />
</beans>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.resolvebug.app</groupId>
<artifactId>bk-services</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>bk-services</name>
<properties>
<jdk_version>1.8</jdk_version>
<jersey_version>2.22.1</jersey_version>
<servlet_version>3.1.0</servlet_version>
<junit_version>3.8.1</junit_version>
<springbeans_version>3.0.5.RELEASE</springbeans_version>
<springcontext_version>3.0.2.RELEASE</springcontext_version>
<springweb_version>5.0.5.RELEASE</springweb_version>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet_version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit_version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${springbeans_version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springcontext_version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springweb_version}</version>
</dependency>
<!-- Jersey -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey_version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey_version}</version>
</dependency>
</dependencies>
<build>
<finalName>bk-services</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${jdk_version}</source>
<target>${jdk_version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
最佳答案
你的项目配置怎么样? Spring 和 Jersey 之间的集成不能开箱即用,因为 Jersey 资源端点生命周期不知道 Spring bean 实例的创建和配置。
默认情况下,Jersey 在每次请求时都会创建端点资源的新实例,而不与 Spring bean 生命周期和作用域实例管理进行任何集成,因此 Spring 不会以任何方式处理 @Autowired
注释。
您必须在 Jersey 中设置 Spring 集成和支持,为此,您可以将 Jersey Spring 扩展依赖项添加到项目配置中。
如果您使用 Maven,对于 Jersey 最高版本 2.25,您必须添加以下依赖项:
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>2.25</version>
</dependency>
对于 Jersey 版本 2.26 或更高版本,可以使用 jersey-spring4
工件。
这样,Jersey 端点将作为 Spring bean 进行管理,从而通过 @Autowired
注释启用依赖项注入(inject)。
还要确保 Spring 配置(包括应用程序上下文初始化和 bean 注册阶段)正确设置。
我认为面对这种配置问题的最佳方法是使用 Spring Boot,并且在 Holon Platform 中,我管理的开源项目,我们提供了许多自动配置工具来将 Spring 框架与 Jersey 结合使用(例如,Spring JAX-RS beans 自动注册为 Jersey 资源,从而摆脱了 jersey.config.server.provider.packages
等配置参数)。
您可以查看this example on GitHub了解如何仅使用 Spring Boot starter 来设置带有 Spring 集成的 Jersey 服务器。
关于java - @Autowired 给出空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50037266/
这更像是一个最佳实践类型的问题。 我听过很多次: a) 在 Spring 中 Autowiring 时,最佳做法是 Autowiring 接口(interface)“而不是”实现。 和.. b) 我还
我正在查看工作区中的一些旧示例。我看不出怎么样由于没有 @Autowired, Autowiring 完成。 Spring boot + facebook 默认配置。 @Controller @Req
事实似乎并非如此。我曾经认为 XML 配置是为了覆盖注释。但是当我在XML配置中设置autowire =“no”时,bean的@Autowired注释属性仍然有效。我不再确定 XML autowire
为什么需要 Autowiring ? Autowiring 概念的解释是什么?@autowired Spring Framework 中的注释. 最佳答案 不需要 Autowiring ,只是方便。
来自this Spring documentation我知道当我使用@Bean时,默认值已经相当于: @Bean(autowire = Autowire.NO) (Default) No autowi
遇到了一个奇怪的要求。我需要将唯一的错误 ID 附加到 log4j 消息并将该消息 ID 返回给接口(interface)。所以,我虽然让我们创建一个 spring 服务,就像这样 public cl
这个问题已经有答案了: @Autowire failing with @Repository (3 个回答) 已关闭 4 年前。 我有一个类“ReportEverythingForm”,它拒绝通过自动
我是 Spring 的新手。我正面临 Spring-Boot 的问题。我正在尝试将一个字段从外部配置文件 Autowiring 到一个 Autowiring 的 bean 中。我有以下类(class)
我有一个带有存储库的 Spring Boot 应用程序。 我还使用@Service并扩展其中的存储库。 当我尝试 @Autowired 我拥有的服务时: Caused by: org.springfr
我有一个接口(interface)C,想要访问另外两个类中的getClassName()。访问 a.getClassName() 时,method1() 中出现异常。 public interface
我遇到了一个奇怪的问题,其中注入(inject)了 @Autowire 的 Component 在一个类中可用,但在另一个类中不可用。 我在Account和Agreement类的属性network中使
考虑以下示例代码: public class SmallCar { private CarEngine carEngine; @Autowired public SmallCa
autowire = "no"和 autowire = "default"有什么区别?如果它们相同,那么为什么我们有这 2 个选项。 最佳答案 Beans The default is "defaul
我已将项目更改为使用注释而不是 xml 文件,但这会增加应用程序部署时间。现在我正在寻找减少它的方法。 按类型 Autowiring 和按名称 Autowiring 之间有性能差异吗? 热烈欢迎任何其
我有一个与 Web 插件一起使用的 spring boot 应用程序。 在一节课中我有: package com.test.company @Component @RestController pub
我有一个可以执行某些操作的系统。该系统使用以下方法为每个对象创建一个单独的线程: stp.scheduleWithFixedDelay((EditSite) ctx.getBean("EditSite
我正在尝试自动连接存储库,但它无法工作。我已经为此苦苦挣扎了一个星期,但我似乎无法弄清楚。有趣的是,当我注释掉人员存储库的 Autowiring 时,程序可以正常工作并正确编译,但是一旦我尝试 Aut
意味着如果具有所需类型的 bean 不超过 1 个,bean 的所有字段将自动注入(inject)依赖项。 问题是当使用注解时它是如何工作的,它到底能不能工作。 我的测试表明即使我使用 @Resou
我有一个 Autowiring 其他 bean 的组件: @Component public class MyComponent { @Autowired private Enviro
这是我的类代码,其中有 @Autowired 字段: 测试A @ContextConfiguration("classpath:spring.xml") public abstract class T
我是一名优秀的程序员,十分优秀!