0

I'm new for Java Spring boot and I'm developing REST API by using JPA. So, I want to access JpaRepository outside the @RestController. And I use @Component annotation top of the class header and use @Autowired annotation for declaring my repository instance. But, repository instance always getting null.


Application class

@SpringBootApplication
@EnableJpaAuditing
public class PriceHandlerServiceApplication
{
    public static void main( String[] args )
    {
        SpringApplication.run( PriceHandlerServiceApplication.class, args );
    }
}

Repository

@Repository
public interface ConfigRepository extends JpaRepository<ServiceConfiguration,Long>
{
}

Model Class

@Entity
@Table( name = "service_configuration" )
@EntityListeners( AuditingEntityListener.class )
public class ServiceConfiguration
{
    @Id
    @GeneratedValue( strategy = GenerationType.IDENTITY )
    @JsonProperty("id")
    private Long id;

    @NotBlank
    @JsonProperty( "name" )
    private String name;

    @JsonProperty( "value" )
    private Double value;

    public ServiceConfiguration()
    {}
 }

Service Class

@Component
public class ConfigurationHandler
{
    @Autowired
    private static ConfigRepository configRepository;
    
    public static List<ServiceConfiguration> getAllConfigurations(){
        return configRepository.findAll();
    }
}

And I call ConfigurationHandler.getAllConfigurations() by another class. But configRepository always getting null.

[UPDATED] Remove static keyword but still not working. still configRepository repository getting null

@Component
public class ConfigurationHandler
{
    @Autowired
    private ConfigRepository configRepository;

    public ConfigurationHandler()
    { }

    public List<ServiceConfiguration> getAllConfigurations()
    {
        return configRepository.findAll();
    }

}

darkz
  • 550
  • 4
  • 9
  • 1
    You cannot autowire static fields in Spring. Can you please try removing static and try? – Govind Jul 03 '20 at 04:21
  • @Govind If I removing static, then how I call 'getAllConfigurations()' method form another class. I want to access these instances from another class. Can I create new instance and call this method? – darkz Jul 03 '20 at 04:41
  • Show where you're using `ConfigurationHandler`. More generally, using constructor injection is better than field injection, and this kind of bug is a major reason why. – chrylis -cautiouslyoptimistic- Jul 03 '20 at 05:12
  • `List list = new ConfigurationHandler().getAllConfigurations();` I used `ConfigurationHandler` like this. – darkz Jul 03 '20 at 05:22
  • 1
    There you are creating a new instance of ConfigurationHandler. You should use ConfigurationHandler from Spring context that will have configRepository autowired. Use @Autowired instance from an instance field or from constructor parameter. – Riku J.K. Jul 03 '20 at 05:34
  • @RikuJ.K.Sorry I can't understand what are you saying, because I'm a very new guy for spring boot. Can you add changes to `ConfigurationHandler` class – darkz Jul 03 '20 at 05:43
  • post the stacktrace with the exception details that you are facing. – Gundamaiah Jul 03 '20 at 05:44
  • 1
    @darkz `ConfigurationHandler` class looks good to me. The problem is where you are using it like this `List list = new ConfigurationHandler().getAllConfigurations();`. You should use it like this `List list = configurationHandler.getAllConfigurations();`, where `configurationHandler` is a instance field autowired by Spring. – Riku J.K. Jul 03 '20 at 05:49
  • 1
    @darkz, You can either autowire ConfigurationHandler class and invoke getAllConfigurations() method or autowire ConfigRepository class and invoke findAll () method into another classes. – Govind Jul 03 '20 at 06:05
  • Yeah, Thanks all. The problem was solved. previously, I used a new instance and that why getting null. After I autowire ConfigRepository and invoke findAll() method. Then no need to ConfigurationHandler class. Thanks all again. – darkz Jul 03 '20 at 06:21

2 Answers2

1

A few things to check :

  1. Remove the static keyword from ConfigRepository use it like :

    @Autowired private ConfigRepository configRepository;

  2. Ensure that your repository interface and entity classes are under the main Application.java package or ensure that your Application.java is at the root of your package so that springboot auto configuration will automatically take up the repository , otherwise use the @EnableJpaRepositories(basePackages="your.package").

  3. Ensure that the component class that you are trying to auto-wire the repository is actually managed by spring when you are using it. That is DO NOT use the new keyword if you want autowiring to work in your component class. So, when you want to use ConfigurationHandler use it like :

    @Autowire private ConfigurationHandler configurationHandler;

Ananthapadmanabhan
  • 5,706
  • 6
  • 22
  • 39
  • 1
    Thanks, @Ananthapadmanabhan.. The problem is I using a new instance for access methods. .`@Autowired private ConfigurationHandler configurationHandler;` and `configurationHandler.getAllConfigurations()` working fine. Thanks again for support. – darkz Jul 03 '20 at 06:25
0

Try adding, into PriceHandlerServiceApplication class

@EnableJpaRepositories("repository.package")

also remove the static keyword

private static ConfigRepository configRepository;
tino89
  • 142
  • 5
  • I remove `static` keyword added public constructor. (I want to access this class from another class) And also added `@EnableJpaRepositories` annotation for `PriceHandlerServiceApplication`. But still not working. Still, repository getting null value. – darkz Jul 03 '20 at 04:59
  • sure, you can access using autowired in any class you want, did you replace "repository.package" by the package where you are putting your repositories? – tino89 Jul 03 '20 at 05:07
  • yes, I added.. `@EnableJpaRepositories("com.assignment.priceHandlerService.repository")` but not working. – darkz Jul 03 '20 at 05:10
  • try it please, @EnableJpaRepositories(basePackages = "com.assignment.priceHandlerService.repository") – tino89 Jul 03 '20 at 05:15
  • @EnableJpaRepositories(basePackages = "com.assignment.priceHandlerService.repository") this also tried. but still getting null value. – darkz Jul 03 '20 at 05:26