0

I developing an application web with Spring Boot and Vaadin for the application interface.

My problem is can't inyect the controller to view, the application starts OK, but the bean is it null in execution.

My controller:

@Component
public class ViewController {

/** Inyección de Spring para poder acceder a la capa de datos.*/
@Autowired
private CommonSetup commonSetup;

/**
 * This method gets the user from db.
 */
public Temployee getUser(String username, String password) {
    Temployee empl = null;
    // get from db the user
    empl = commonSetup.getUserByUsernameAndPass(username, password);

    // return the employee found.
    return empl;
}

... ...

My view:

@Theme("login")
@SpringUI
public class LoginView extends CustomComponent implements View ,Button.ClickListener {

/** The view controller. */
@Autowired
private ViewController   vContr;

public LoginView() {
    setSizeFull();

   ...
   ...

   // Check if the username and the password are correct.
   Temployee empleado = vContr.getUser(username, password);

In the LoginView the bean ViewController is null.

How I can inyect the bean in the view?

Thanks.

A J
  • 3,970
  • 14
  • 38
  • 53
Daniel
  • 1
  • 1

1 Answers1

0

You cannot access an autowired field in constructor because injection is not yet done at that point. Add a method with a @PostConstruct annotation that will be executed after fields are injected:

@PostConstruct
public void init() {
  // Check if the username and the password are correct.
  Temployee empleado = vContr.getUser(username, password);
}

This is nothing specific to vaadin4spring, this is how Spring works.

Henri Kerola
  • 4,947
  • 1
  • 17
  • 19