How to autowire an object in spring in an object created with new

前端 未结 5 2059
广开言路
广开言路 2021-02-07 12:35

all I want to do is autowire the field backgroundGray in the NotesPanel class, but all I get is the exception below.

So, question is, how to autowire it correctly ? It r

5条回答
  •  误落风尘
    2021-02-07 13:16

    The problem is here:

    frame.add(new NotesPanel(), BorderLayout.CENTER);

    you are creating a new object for class NotesPanel in the constructor of class Notepad.

    The constructor is called before method main, so Spring context has not been loaded yet.

    When instantiating the object for NotesPanel, it can't auto wire BackgroundGray because Spring context doesn't exists at that moment.

提交回复
热议问题