Pelos meus testes, o ideal é o seu JFrame
não ser um bean.
Vc vai declarar as dependências dele no construtor e vai instanciá-lo manualmente. Por exemplo:
class Window extends JFrame {
public Window(PokemonService pokemonService) {
super.setLayout(new BoxLayout(super.getContentPane(), BoxLayout.Y_AXIS));
pokemonService.getAll().forEach(pokemon -> super.add(new JLabel(pokemon.getName())));
super.setSize(300, 300);
super.setLocationRelativeTo(null);
super.setDefaultCloseOperation(EXIT_ON_CLOSE);
super.setVisible(true);
}
}
E a sua classe principal estaria assim:
@SpringBootApplication
class Application {
public static void main(String... args) {
new SpringApplicationBuilder(Application.class).headless(false).run(args);
}
@Bean
CommandLineRunner runner1(PokemonRepository pokemonRepository) {
return args -> pokemonRepository.saveAll(
List.of(
new Pokemon(1, "Bulbasaur"),
new Pokemon(4, "Chamander"),
new Pokemon(7, "Squirtle"),
new Pokemon(25, "Pikachu")
)
);
}
@Bean
CommandLineRunner runner2(PokemonService pokemonService) {
return args -> SwingUtilities.invokeLater(() -> new Window(pokemonService));
}
}
Note que usei SpringApplicationBuilder
com headless(false)
para tudo funcionar corretamente.
Este aqui é o código completo que usei para testar
Meu build.gradle
:
plugins {
id 'java'
id 'org.springframework.boot' version '3.1.0'
id 'io.spring.dependency-management' version '1.1.0'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'com.h2database:h2'
}
Meu application.properties
está vazio.
Meu código Java:
package com.example;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
@Entity
class Pokemon {
@Id
private long number;
private String name;
protected Pokemon() {
}
public Pokemon(long number, String name) {
this.number = number;
this.name = name;
}
public long getNumber() {
return this.number;
}
public String getName() {
return this.name;
}
}
interface PokemonRepository extends JpaRepository<Pokemon, Long> {
}
@Service
class PokemonService {
@Autowired
private PokemonRepository pokemonRepository;
public List<Pokemon> getAll() {
return this.pokemonRepository.findAll();
}
}
class Window extends JFrame {
public Window(PokemonService pokemonService) {
super.setLayout(new BoxLayout(super.getContentPane(), BoxLayout.Y_AXIS));
pokemonService.getAll().forEach(pokemon -> super.add(new JLabel(pokemon.getName())));
super.setSize(300, 300);
super.setLocationRelativeTo(null);
super.setDefaultCloseOperation(EXIT_ON_CLOSE);
super.setVisible(true);
}
}
@SpringBootApplication
class Application {
public static void main(String... args) {
new SpringApplicationBuilder(Application.class).headless(false).run(args);
}
@Bean
CommandLineRunner runner1(PokemonRepository pokemonRepository) {
return args -> pokemonRepository.saveAll(
List.of(
new Pokemon(1, "Bulbasaur"),
new Pokemon(4, "Chamander"),
new Pokemon(7, "Squirtle"),
new Pokemon(25, "Pikachu")
)
);
}
@Bean
CommandLineRunner runner2(PokemonService pokemonService) {
return args -> SwingUtilities.invokeLater(() -> new Window(pokemonService));
}
}