0

I have a question. Why is this repository not null here.

@Component
public class InitialStartUp implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private DigitalsRepository digitalsRepository; // <--- NOT NULL

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {

    }

}

But here it will become null.

@Getter
public class DigitalsView extends HorizontalLayout{


    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Autowired
    private DigitalsRepository digitalsRepository; // <-- This is null

    public DigitalsView() {

    }
}

I have created the repository here

@Repository
public interface DigitalsRepository extends JpaRepository<Digitals, Long> {
    Digitals findByid(int i);
}

So why does this happen? I can use digitalsRepository at the class InitialStartUp but not at the DigitalsView class. Why?

Update:

Here is a real working example where @Autowired becomes null, and not null. It's the main view where it takes time for Spring to auto wire a field so it won't become null. From the beginning, it will become null. But if I use the listener below, it will show that it will be not null.

@Route("")
@Viewport("width=device-width, minimum-scale=1, initial-scale=1, user-scalable=yes, viewport-fit=cover")
@PWA(name = "My Application", shortName = "My App")
public class MainView extends AppLayout {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private LineChart lineChart = new LineChart();
    private DigitalOutputs digitalOutputs = new DigitalOutputs();
    private ControlPanel controlPanel = new ControlPanel(lineChart);
    private Image pinMap;

    @Autowired
    private DigitalsRepository digitalsRepository;

    public MainView() {

        System.out.println(null == digitalsRepository); // THIS IS NULL

        // Bar
        Image img = new Image("img/JLogger.png", "JLogger Logo");
        img.setHeight("100px");
        addToNavbar(img);

        // Pin map
        pinMap = new Image("img/Pinmap.png", "Pinmap");

        // Tabs
        Tabs tabs = new Tabs(new Tab("Plot"), new Tab("Digitals"), new Tab("Pin map"), new Tab("Logout"));
        tabs.setOrientation(Tabs.Orientation.VERTICAL);

        HorizontalLayout contents = new HorizontalLayout();
        contents.add(lineChart, digitalOutputs.getGrid(), controlPanel.getLayout(), pinMap);

        // Include
        addToDrawer(tabs);
        setContent(contents);

        // Listener for the tabs
        tabs.addSelectedChangeListener(click -> {
            // Show a selection
            int selectedIndex = tabs.getSelectedIndex();
            switch(selectedIndex) {
            case 0: // Plot
                hideAll();
                lineChart.getChart().setVisible(true);
                controlPanel.getLayout().setVisible(true);
                System.out.println(null == digitalsRepository); // NOT NULL
                break;
            case 1: // Digitals
                hideAll();
                digitalOutputs.getGrid().setVisible(true);
                break;
            case 2: // Pin map
                hideAll();
                pinMap.setVisible(true);
                break;
            case 3: // Logout
                UI.getCurrent().navigate(SecurityConfiguration.LOGOUT_URL.replace("/", ""));
                break;
            }
        });

    }
euraad
  • 2,467
  • 5
  • 30
  • 51
  • 2
    Because the first is a Spring `@Component`, so it is a Spring-managed Bean, and hence `@Autowired` fields are populated by Spring. The second is a POJO, which you instantiate by calling `new DigitalsView()` somewhere, so it is not a Spring-managed Bean. – Andreas Jan 01 '20 at 20:34
  • @Andreas I have tried this solution https://vaadin.com/forum/thread/15717777/vaadin-spring-boot-interference-with-autowired but without success. – euraad Jan 01 '20 at 21:02
  • Simply add @Component to DigitalsView – Simon Martinelli Jan 02 '20 at 10:18
  • @SimonMartinelli I tried that, but it did not work. – euraad Jan 02 '20 at 12:21
  • @DanielMårtensson is your `DigitalsView` in a directory scanned by Spring? – anasmi Jan 03 '20 at 14:03
  • @anasmi Yes! Also I have updated my question. There is a real working example of a weird issue. – euraad Jan 03 '20 at 14:14
  • @Andreas I don't appreciate this question to be closed and refered to another question that does not have the same issue. Please open this question again. – euraad Jan 03 '20 at 14:14
  • 2
    Fields autowiring happens after constructor has run. You should either do `constructor injection` or use a`@PostConstruct` annotated method :) [Spring dependency injection](https://stackoverflow.com/questions/40620000/spring-autowire-on-properties-vs-constructor) – anasmi Jan 03 '20 at 14:39
  • @DanielMårtensson Your update changed the question, so I'm leaving the original link to cover the first half of the question, and adding a second link to cover the update. – Andreas Jan 03 '20 at 15:57
  • @anasmi Than you! That actually solved my problem! :) Now I understand why many using PostConstruct. – euraad Jan 03 '20 at 15:59

0 Answers0