У меня очень простая настройка проекта. Spring 4,14 Джерси 2,15Spring @autowired не загружает bean-throws NULL pointer
web.xml
<servlet>
<servlet-name>Jersey Web Application</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.talentera</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
По какой-то причине, @Autowired не работает или бобы не сканируется. Ниже мой проект установки:
Ниже конфигурация пружины:
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd">
<context:annotation-config />
<context:component-scan base-package="com.talentera">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Component" />
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Service" />
</context:component-scan>
Ниже использование:
@Component
@Path("/user")
@Scope("request")
public class UserRestService extends BaseAPIService{
@Autowired
UserManagementService userManagementService;
protected static final Log logger = LogFactory.getLog(UserRestService.class.getName());
EmailFormatValidator emailValidator = new EmailFormatValidator();
public UserManagementService getUserManagementService() {
return userManagementService;
}
public void setUserManagementService(UserManagementService userManagementService) {
this.userManagementService = userManagementService;
}
@GET
@Path("/get/{userId}")
@Produces(MediaType.APPLICATION_JSON)
public String getUser(@PathParam("userId") Integer userId)
{
System.out.println(getSerRequest().getSession().getId());
User u = userManagementService.getUserById(userId);
response.setData(u);
return jsonStringifyResponse();
}
package com.talentera.common.service.impl;
imports,,,,
@Service("userManagementService")
@Transactional(rollbackFor = Exception.class)
@SuppressWarnings("unchecked")
public class UserManagementServiceImpl extends AbstractService<Identifiable> implements UserManagementService
{
Это бросает исключение указатель NULL, поскольку userManagementService является NULL.
Пожалуйста, помогите мне с вашими выводами. Заранее спасибо.
как вы загружаете свой весенний контекст? web.xml? Зачем использовать xml? Почему бы просто не использовать JavaConfig? – dres
@dres, Спасибо за ответ, хорошо, что я раньше делал с XML и следил за тем же, и все же я загружаю свой весенний контекст через web.xml, он распознает @ Компоненты в порядке, но не @ Service, вероятно. – Deepak
Я нигде не вижу, что вы включаете модуль интеграции Jersey-Spring. Ваша услуга - фасоль Джерси, и просто добавление аннотаций Spring не делает ее весной. – chrylis