Spring @Configuration批注@Autowired服务为空

时间:2018-07-23 06:37:05

标签: java spring jersey spring-annotations spring-config

我正在使用Spring Configuration和Jersey Configuration。

这是我的春季配置:

@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;
}

}

这是我的Service类和接口:

public interface DAOService {
    User register(User user)
}


@Service
public class DAOServiceImpl implements DAOService {

   @Override
   User register(User user){
      return null;
   }

 }

这是我的球衣资源:

内部ResourceSecurityService类我想对DAOService使用@Autowire  类,但出现空指针异常,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();
}

我正在使用具有以下依赖项的Spring 4.3.8.RELEASE版本:

    <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>

1 个答案:

答案 0 :(得分:0)

如果提供项目链接可能会更好。 根据信息,我看到:

  • 确保您具有jersey-spring4依赖项
  • 确保您的Jersey版本与集成依赖项兼容
  • 检查您的项目配置

我建议您在自动配置中使用Spring Boot。