0

I'm using Vaadin 7, Spring Data JPA 1.9.4.RELEASE, and Vaadin-Spring 1.0.0 and I have some DI problemes.

I choose not to use Spring Boot because it will automatically do too many things that I cannot "see" and I have encountered some problemes that spent me too much time to understand and find the reason, so I prefer no boot.

The probleme that I encounter is that DI works at a root UI but not for a sub-window of the root UI.

RootUI.java

@SpringUI(path = "/")
public class RootUI extends UI {
    @Autowired
    private EntityManagerFactory entityManagerFactory; // this one works, but I cannot get EntityManager directly

    @Autowired
    private ClassService classService;   // this one works


    @Override
    protected void init(VaadinRequest request) {
        ...

        PersonForm form = new PersonForm();
        CssLayout layout = new CssLayout();
        layout.addComponent(form);
        Window subWindow = new Window();
        subWindow.setContent(layout);
        ...
    }
}

PersonForm.java

public class PersonForm {

    @Autowired
    private ClassService classService; // this doesnot work, 

    public PersonForm(ClassService classService) {
        classService.findByName();// since the @Autowired dosenot work, I have to pass the one from rootUI.

    }

    init() {
        classService.findByName();   // null exception
    }
}

DBConfig.java

@Configuration
@EnableVaadin
@EnableJpaRepositories(basePackages = {"com.example.person.repository"})
@EnableTransactionManagement
public class DBConfig {
    @Bean
    public DataSource dataSource() {
        HikariConfig config = new HikariConfig();
        config.setDriverClassName("com.mysql.jdbc.Driver");
        config.setJdbcUrl("jdbc:mysql://localhost:3306/test?autoReconnect=true&useSSL=false");
        config.setUsername("root");
        config.setPassword("root");
        config.setMaximumPoolSize(20);
        HikariDataSource dataSource = new HikariDataSource(config);
        return dataSource;
    }

    @Bean
    public EntityManagerFactory entityManagerFactory() {

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(true);

        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);

        factory.setDataSource(dataSource());
        factory.setPackagesToScan("com.example.person");
        factory.setPersistenceProviderClass(HibernatePersistenceProvider.class);

        Properties jpaProperties = new Properties();
        jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect");
        jpaProperties.put("hibernate.hbm2ddl.auto", "update");
        factory.setJpaProperties(jpaProperties);

        factory.afterPropertiesSet();
        return factory.getObject();
    }
}
Morfic
  • 15,178
  • 3
  • 51
  • 61
mingzhao.pro
  • 709
  • 1
  • 6
  • 20
  • Spring can autowire fields for components it manages. If you instantiate yourself those object, Spring won't know so the automagic won't happen. What you want to do, is tell Spring that `PersonForm` is one of its components, and request an instance from its initialized context. – Morfic Mar 05 '16 at 21:37

1 Answers1

0

Try to annotate your PersonForm with some Spring annotation like @Component. Or maybe better try to use annotation from vaadin-spring @SpringView.

bilak
  • 4,526
  • 3
  • 35
  • 75