V14 with Spring beans

Hi,

I and using the latest V14 with Springboot for some application. I declared some Spring beans in configuration. I want to know whether I can autowire those beans to the fields of my Vaadin components, instead to the constructor’s parameters. I hope to have something like:

public class SomeView extends Div {

	@Autowired
	private SomeBean somebean;

Instead of:

public class SomeView extends Div {

	public SomeView( @Autowired SomeBean somebean) {
	.....
	}

Right now I found that the later case might not work. Any advices of how to have it.

Best regards,
Joey

Sorry, the later case works, but not the former. Joey

This is standard Spring (and also CDI) behavior (not related to Vaadin). Autowired (/injected) fields are not available in constructor, since injection is executed after constructor is being executed. That is why these systems have option for constructor injection as well.

In case you need to inject fields (which is necessary sometimes), you can refactor you bean setup so that you have init method annotated by @PostConstruct annotation, which designates it to be executed after injection and constructor. Injected fields thus can be used there.

Thanks a lot.

Both ways are possible, the field-injected beans are just not available yet in constructor like Tatu says.

You say you want to autowire those beans to the fields of my Vaadin components. An important information that was not mentioned yet here is that you can only inject/autowire in Vaadin Views, i.e. classes that have a @Route annotation. You can not inject/autowire into components that you add to your views. You will have to inject into the view, then pass the injected bean into the constructor of that lower level component.