0

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>
Andrew
  • 13
  • 3
  • can you share your project structure? – Kathirvel Subramanian Jul 23 '18 at 07:09
  • An autowired dependency cannot be `null` your application would break. It can only be `null` if you or another framework creates instances of the beans. In this case you are using Jersey and haven't setup proper Jersey - Spring integration. – M. Deinum Jul 23 '18 at 07:30
  • Take a look at [this example project](https://github.com/psamsotha/jersey-spring-example) for all the property configurations and dependencies required to integrate Spring with Jersey (without using Spring Boot) – Paul Samsotha Jul 23 '18 at 15:43
  • [See also](https://stackoverflow.com/a/32357991/2587435) – Paul Samsotha Jul 23 '18 at 17:07

1 Answers1

0

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.

Lev Khruschev
  • 1,576
  • 2
  • 14
  • 18