Search code examples
javaspringjerseyspring-annotationsspring-config

Spring @Configuration annotation @Autowired service is null


I am using Spring Configuration and Jersey Configuration.

This is my spring 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;
}

}

This is my Service class and interface:

public interface DAOService {
    User register(User user)
}


@Service
public class DAOServiceImpl implements DAOService {

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

 }

This is my jersey resource:

Inside ResourceSecurityService class I want to use @Autowire for DAOService class, but I get null pointer exception, daoService is always null.

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

I am using spring version 4.3.8.RELEASE with the next dependencies:

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

Solution

  • It could be better if you'd provide a link to your project. Based on information I see:

    • make sure you have jersey-spring4 dependency
    • make sure your Jersey version compatible with integration dependency
    • check out your project configuration

    I advice you to use Spring Boot with auto configurations.