Q:

How to autowire repository in Swing application

I am attempting to create a Swing application using Spring Boot. In the root of my packages I have the main class:
@SpringBootApplication
public class Application
{
    public static void main(String[] args)
    {
        ConfigurableApplicationContext ctx = new SpringApplicationBuilder(Application.class)
                .headless(false)
                .run(args);

        System.out.println("Contains personRepository: " + ctx.containsBeanDefinition("personRepository"));

        EventQueue.invokeLater(() ->
        {
            ApplicationFrame appFrame = ctx.getBean(ApplicationFrame.class);
            appFrame.setVisible(true);
        });
    }
}

As I understand, @SpringBootApplication should find any spring components in packages under it. When Application is run, it starts ApplicationFrame:
@Component
public class ApplicationFrame extends javax.swing.JFrame
{
    @Autowired PersonRepository personRepository;

    /**
    * Creates new form ApplicationFrame
    */
    public ApplicationFrame()
    {
        initComponents();
        System.out.println("personRepository = " + personRepository);
    }
}

PersonRepository is in a package under the root and is annotated with @Repository. 
@Repository
public interface PersonRepository extends JpaRepository<Person, Long> {}

When I run this, it returns:
Contains personRepository: true
personRepository = null

So Application can find the repository as a bean, but ApplicationFrame cannot autowire it. Do I create ApplicationFrame the wrong way or is there something else that I'm missing here?
Thanks for any help!

A:

First of all, you have to remove @Repository annotation from repository class since this is a redundant annotation. Instead of this you should annotate Application class with @EnableJpaRepositories - it's more convenient way of working with Spring Data.
About your output: you see personRepository = null in the log because this log statement is located within a constructor. At this moment, the field value is null.
In your case you use field injection - this means that Spring autowires beans after object constructing. So nothing wrong with ApplicationFrame class, only with an output.
To see the correct output you can use constructor-based injection:
@Component
public class ApplicationFrame extends javax.swing.JFrame
{
    private final PersonRepository personRepository;

    /**
    * Creates new form ApplicationFrame
    */
    public ApplicationFrame(final PersonRepository pr)
    {
        personRepository = pr;

        initComponents();
        System.out.println("personRepository = " + personRepository);
    }
}

