我尝试了很多东西,但似乎是我的搜索服务"在我的球衣ressource中随机实例化。 SearchResource中的SearchService几乎总是为空。
正如你所看到的,我加入了春季球衣依赖。
这是我的代码: SearchResource(jersey,尝试instantiante searchService)
@Path("search")
@Component
public class SearchResource {
private static final Logger LOGGER = Logger.getLogger(SearchResource.class);
@Context
private UriInfo context;
@Autowired
private SearchService searchService;
/**
* Creates a new instance of SearchResource
*/
public SearchResource() {
}
/**
* Retrieves representation of an instance of com.gehc.mpse.backend.ws.SearchResource
* @param search
* @param <error>
* @param authors
* @return an instance of java.lang.String
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response search(@QueryParam("query") String search, @QueryParam("authors") List<String> authors) {
System.out.println("searchservice :" + searchService);
Response res = searchService.searchQuery(search);
return res;
}
public SearchService getSearchService() {
return searchService;
}
public void setSearchService(SearchService searchService) {
this.searchService = searchService;
}
我的班级searchService:
@Component
public class SearchService {
private static final Logger LOGGER = Logger.getLogger(SearchService.class);
@Autowired
private IPublicationRepository repository;
@Autowired
private Neo4jTemplate template;
private static final Integer PAGE_SIZE = 20;
public SearchService() {
LOGGER.debug("search service initialzed " + this);
}
public Response searchQuery(String query) {
PageRequest request
= new PageRequest(1, PAGE_SIZE);
LOGGER.debug("repo : " + repository);
Page<Publication> res = repository.findPublicationByTitle(query, request);
//repository.query(query, null);
return Response.ok().entity(res.getContent()).build();
}
public IPublicationRepository getRepository() {
return repository;
}
public void setRepository(IPublicationRepository repository) {
this.repository = repository;
}
public Neo4jTemplate getTemplate() {
return template;
}
public void setTemplate(Neo4jTemplate template) {
this.template = template;
}
}
我的pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j</artifactId>
<version>3.2.0.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.neo4j</groupId>
<artifactId>neo4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-cypher</artifactId>
</exclusion>
<exclusion>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-kernel</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Spring [End] -->
<!--neo4j -->
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j</artifactId>
<version>2.1.5</version>
</dependency>
<dependency>
<groupId>org.neo4j.app</groupId>
<artifactId>neo4j-server</artifactId>
<version>2.1.5</version>
</dependency>
<!-- end neo4j -->
<dependency>
<groupId>org.glassfish.metro</groupId>
<artifactId>webservices-rt</artifactId>
<version>2.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-java2wsdl</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.ws.commons.axiom</groupId>
<artifactId>axiom-api</artifactId>
<version>1.2.14</version>
</dependency>
<dependency>
<groupId>org.apache.ws.commons.axiom</groupId>
<artifactId>axiom-impl</artifactId>
<version>1.2.14</version>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.9-RC1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>2.13</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
</exclusion>
<exclusion>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.13</version>
<scope>provided</scope>
</dependency>
</dependencies>
我的web.xml:
<web-app version="3.0" 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">
<display-name>PMSE</display-name>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/context.xml</param-value>
</context-param>
</web-app>
答案 0 :(得分:2)
这是我的代码:SearchResource(jersey,尝试instantiante searchService)
请注意,Jersey不会实例化您的SearchService。因为资源上有@Component
注释,所以SearchService应该由Spring连接到SearchResource,而Jersey应该从Spring检索SearchResource实例。
你的Spring上下文是创建两次(一次是Jersey,一次是Spring的ContextLoaderListener)?在Jersey开始之前是Spring上下文fully initialized吗?泽西寻找一个Spring上下文然后尝试create one on its own(第98行),如果它找不到。
泽西岛的Spring整合有点困难。最好的办法是在SpringComponentProvider和AutowiredInjectResolver中设置一些断点,然后在调试器中逐步执行。调试时,您可以设置suspend=y
,以便在实际连接调试器之前不会发生任何事情。有关调试设置的详细信息,请参阅this answer。
我还会查看Jersey Jira的Spring related issues,看看是否有任何未解决的问题适用于您。