Java Spring@Configuration annotation@Autowired服务为空
我使用的是Spring配置和Jersey配置 这是我的spring配置:Java Spring@Configuration annotation@Autowired服务为空,java,spring,jersey,spring-annotations,spring-config,Java,Spring,Jersey,Spring Annotations,Spring Config,我使用的是Spring配置和Jersey配置 这是我的spring配置: @Component @Configuration @EnableScheduling @EnableAspectJAutoProxy @EnableTransactionManagement @ComponentScan(basePackages = { "my.packages" }) public class SpringConfig { private static final String MESSAGE_S
@Component
@Configuration
@EnableScheduling
@EnableAspectJAutoProxy
@EnableTransactionManagement
@ComponentScan(basePackages = { "my.packages" })
public class SpringConfig {
private static final String MESSAGE_SOURCE_BASE_NAME = "i18n/messages";
@Profile(Profiles.APPLICATION)
@Configuration
@PropertySource("classpath:application.properties")
static class ApplicationProperties {
}
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder(11);
}
@Bean
public TaskScheduler taskScheduler() {
return new ConcurrentTaskScheduler();
}
@Bean
MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename(MESSAGE_SOURCE_BASE_NAME);
messageSource.setUseCodeAsDefaultMessage(true);
return messageSource;
}
}
这是我的服务类和接口:
public interface DAOService {
User register(User user)
}
@Service
public class DAOServiceImpl implements DAOService {
@Override
User register(User user){
return null;
}
}
这是我的球衣资源:
在ResourceSecurityService类中,我想将@Autowire用于DAOService
类,但我得到空指针异常,daoService始终为空
@Component
@Path("/security")
@Produces({ MediaType.APPLICATION_JSON })
public class ResourceDAOService {
@Autowired
private DAOService daoService ; //<--- here daoService is null;
@POST
@Path("/register")
@Consumes({ MediaType.APPLICATION_JSON })
@Transactional
public Response register(User user, @Context HttpServletRequest request,
@Context HttpServletResponse response) throws AppException {
return Response.status(Response.Status.CREATED).entity(securityService.register(user)).build();
}
@组件
@路径(“/security”)
@产生({MediaType.APPLICATION_JSON})
公共类资源管理服务{
@自动连线
private DAOService DAOService;//如果您提供一个指向项目的链接,效果会更好。
根据我看到的信息:
- 确保您有
jersey-spring4
依赖项
- 确保您的Jersey版本与集成依赖项兼容
- 检查您的项目配置
我建议您将Spring Boot与自动配置一起使用。您可以共享您的项目结构吗?自动连接的依赖项不能是null
,您的应用程序将中断。只有当您或其他框架创建bean实例时,它才能是null
。在这种情况下,您使用的是Jersey,并且没有设置正确的Jersey-Spring集成。查看将Spring与Jersey集成所需的所有属性配置和依赖项(不使用Spring引导)
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>