0

I am using spring 5.2.8

I am using @Autowired to get create the Bean. But i am getting NullPointer when I used this bean

configuration

@Configuration
@Primary
@ComponentScan(basePackages = {"com.try.appli"})
@EnableJpaRepositories(basePackages = "com.tryappli.persistence")
@EnableTransactionManagement
public class AppliConfiguration {

...

My component in package com.try.appli

@Component
@Primary
public class Mycomponent extends MycomponentAbstract {


    @Autowired
    MyService service 

     public handle()
        String data ="{data to update}";
        service.update(data);

service :

@Service
public MyService {

    @Autowired
    private RestOperations resoperation;

    public void update(dataToUpdate) {
        restOperations.put("url", dataToUpdate);
    }
....

I also tried to put the annotation on the implementation but without success

In web.xml

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
Florence
  • 1,641
  • 3
  • 13
  • 23
  • Scattering random snippets is not helpful, here, and we can't reproduce this. Could you show us how you create a context? also, what you mean in "i am getting NullPointer when I used this bean"? you can't fetch that bean from the context? or you can't inject IN it? or it doesn't get injected in some @Service layer class? please provide more information. – Giorgi Tsiklauri Jan 22 '21 at 16:10
  • Your bean must be in Spring Context, otherwise it is null, please add your Context creation – Joe Taras Jan 22 '21 at 16:10
  • @configuration = context creation no ? – Florence Jan 22 '21 at 16:15
  • Does this answer your question? [Why is my Spring @Autowired field null?](https://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null) – Roland Weisleder Jan 23 '21 at 12:12
  • @Roland Weisleder I don't use new service; – Florence Jan 25 '21 at 07:12

0 Answers0