Manual configuration of Spring Data

Asked

Viewed 202 times

3

I’m trying to use Springdata on a Jface/SWT project. Because the Crudrepository self implementation feature is something fantastic and very advantageous.

For this, I include here in the classpath the necessary things of Springdata: Spring Data Commons, Spring Data JPA, Spring Bean, Spring Context.

For me, it would only be 4 steps to do this manually:

1 - Include jars in classpath (no classpath error) 2 - Note in the application the information for the Spring exit revoking the classes:

@Configuration
@ComponentScan(basePackages = "org.wender.foobar")
public class ControleGadoApp extends ApplicationWindow {

3 - Create an interface that extends Crudrepository:

public interface Clienterepository extends Crudrepository

4 - Create a class attribute and annotate it with @Autowired

public class TelaCliente extends AbstractTela {

    @Autowired
        private ClienteRepository repository;

Those four steps weren’t enough, @Autowired didn’t work and the class variable is coming null.

Is there any more configuration?

  • How are you instantiating the TelaCliente? You must have a Spring container that controls the injection of dependencies. Reference: https://docs.spring.io/spring/docs/3.0.x/reference/beans.html

  • I discovered the problem, I needed to boot my application as Commandline Application (spring boot)

  • Beauty! Put your code here as an answer, to help other people.

1 answer

2


The problem is that the application was not being initialized as a Spring application. For me this was not necessary for desktop application, it was a thing of web application. Below are the two details (1) annotate @Springbootapplication and (2) implement Commandlinerunner

@SpringBootApplication
public class App extends ApplicationWindow implements CommandLineRunner  {

@Override
public void run(String... args) throws Exception {
   //Aqui eu coloquei o que antes estava no método main 
}

public static void main(String args[]) {
    SpringApplication.run(App.class, args);
}

Browser other questions tagged

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