Autowired spring boot in Jframe

Asked

Viewed 564 times

2

In the development of a Spring Boot application for Desktop, it is possible to inject a @Repository in a class JFrame?

If yes how? Some alternative?

Code examples:

@Repository
public interface ItemRepository extends CrudRepository<Item, Long> {}

public class ItemFrame extends JFrame {

    @Autowired
    private ItemRepository repository;

}

That code gives NullPointerException, I tried to write down the class with @Component and @Controller but successful.


Below is an example of the main class.

@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class);
        new ItemFrame().setVisible(true);
    }
}
  • For the autowired to work the class has to be instantiated by Spring. Where the Itemframe class is being instantiated in its code?

  • 1

    I am manually instantiating in the main class, as I edited the question.

1 answer

1

Spring will not manage instances created with new! Spring itself must create the class instance for the injections to work. The right one would be to inject the itemFrame object by Spring as well, but as the main method is Static we cannot inject with @Autowired. An alternative is to get the instance created by Spring through the Applicationcontext as follows:

@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
    ApplicationContext context = new SpringApplicationBuilder(MyApplication.class)
    .headless(false).run(args);
    ItemFrame a = context.getBean(ItemFrame.class);
    a.setVisible(true);
    }
}

When you annotate a class with @Component, @Service , @Repository or @Controller Spring creates an instance of that class by calling your constructor without parameters or the constructor annotated with @Autowired if it exists. When creating it, it injects all its dependencies correctly and the instance is in your Applicationcontext. Classes created with new will not be managed by Spring unless you place them in the application context manually.

Also, to run with Spring Boot you must put the option . headless(false). And also the Itemframe class must be annotated with @Component in order to be injected.

@Component
public class ItemFrame extends JFrame{
    ...
}
  • I’ll test when I get home, then I’ll put the result here.

  • After testing. Such code (using ItemFrame as static for the call) does not work. Generates a Nullpointer Exception.

  • Where’s that coming from DemoApplication.class? Is it the name of the main class of my own application? You need to note down the Jframe class(s)?

  • I tested but unsuccessfully yet. It creates the frame but does not inject items with @Autowired within it.

  • Besides yours I added .web(false) in front of .headless(false). The exception that occurs is Nullpointerexception on the line I call some method of repository. The window is injected and opened correctly if I do not put repository within it.

  • I put the note @Compoment in ItemFrame. ItemRepository is an interface. The "magic" I want from Spring is precisely by extending CrudRepository. In the web environment it brings me a complete repository automatically, I want it on the desktop.

  • Well, you can inject a Repository into any class, including one that extends Jframe. Sorry, I thought that was the question! When I saw Repository I thought about an interface being implemented by some class, but really for example Itemrepository is using Spring Data, it makes sense. What you want then is how to use Spring Data in a desktop app is this?

  • This is also the idea. Spring Data JPA works great on the Web, but I can’t use it on desktop. It would also be injecting other classes as a service/utility if needed in Jframes. A lot of people use Spring, but not for desktop (it seems).

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.