Java Bean被初始化,但注入抛出npe
我正在使用配置文件创建一个bean。在服务器启动期间,bean被初始化,因为我在其中有一个调试点,它被触发。当restApi调用handler方法时,它抛出NPE,其中coinResponse为null。请让我知道我做错了什么 配置文件Java Bean被初始化,但注入抛出npe,java,spring,dependency-injection,jax-rs,javabeans,Java,Spring,Dependency Injection,Jax Rs,Javabeans,我正在使用配置文件创建一个bean。在服务器启动期间,bean被初始化,因为我在其中有一个调试点,它被触发。当restApi调用handler方法时,它抛出NPE,其中coinResponse为null。请让我知道我做错了什么 配置文件 package com.foo.service.handler; @Configuration @ComponentScan("com.foo.service") public class CacheConfiguration { @Bean p
package com.foo.service.handler;
@Configuration
@ComponentScan("com.foo.service")
public class CacheConfiguration {
@Bean
public CoinResponse getCoinResponse() {
return new CoinResponse();
}
}
手锉
package com.foo.service.handler;
public class DetailsHandler {
@Inject
CoinResponse getCoinResponse;
public List<CoinResponse> getCoinDetails() {
System.out.println(getCoinResponse.getClass());
}
}
package com.foo.service.handler;
公共类详细信息处理程序{
@注入
CoinResponse获取CoinResponse;
公共列表getCoinDetails(){
System.out.println(getCoinResponse.getClass());
}
}
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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>foosvc</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.XmlWebApplicationContext</param-value>
</context-param>
<servlet>
<servlet-name>jersey-serlvet</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.foo.service</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
</web-app>
foosvc
上下文配置位置
/WEB-INF/applicationContext.xml
org.springframework.web.context.ContextLoaderListener
上下文类
org.springframework.web.context.support.XmlWebApplicationContext
泽西塞尔维特酒店
org.glassfish.jersey.servlet.ServletContainer
jersey.config.server.provider.packages
com.foo.service
1.
泽西塞尔维特酒店
/webapi/*
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:c="http://www.springframework.org/schema/c" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<context:annotation-config />
<context:component-scan base-package="com.fomo.service" />
</beans>
很可能
DetailsHandler
在Spring中没有定义
依赖项注入(@Inject
)只能发生在Spring使用的类内部。
请尝试在DetailsHandler
类中创建一个无操作构造函数,并在那里放置一个断点
根据您的spring配置,您可能应该使用@组件
或其原型(例如@Controller
)对其进行注释