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();
    }
}
4

1 回答 1

0

尝试使用 @Component 之类的 Spring 注释来注释您的 PersonForm。或者最好尝试使用来自 vaadin-spring @SpringView的注释。

于 2016-03-05T18:43:20.203 回答