Vaadin + Spring Integration:空指针异常

时间:2014-10-13 07:37:54

标签: java spring hibernate jpa vaadin

我有一个像这样的UI类的Vaadin Web应用程序:

@SuppressWarnings("serial")
@Theme("mytheme")
public class LogsUI extends UI {

LogsView logsViewer = new LogsView();

@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = LogsUI.class)
public static class Servlet extends VaadinServlet {
}

@Override
protected void init(VaadinRequest request) {
    Panel panel = new Panel();
    panel.setContent(logsViewer);
    panel.setSizeFull();
    setContent(panel);
}
}

你可以看到我添加use setContent来添加一个视图的LogsView类 - 它是一个声明的片段:

@SuppressWarnings("serial")
public class LogsView extends CustomComponent implements View {

//some variables, buttons, components etc
ProcessDao processDao;


//sample method
void sampleMethod(){
      processDao = new ProcesDao;
      processDao.getAllprocesses(); //just sample, no matter about logic
}

}

我的ProcessDao课程:

public class ProcessDao {


@Autowired
ApplicationConfiguration applicationConfiguration;

public ProcessDao() {
}


public List<ProcessEntity> getAllProcess(){
    System.out.println("TEST:" + applicationConfiguration);

    //entity manager and other stuffs

     return processList;
}
}

你可以看到我System.out.println()检查我是否得到applicationConfiguration对象。我变得空了。这是主要问题。

这是我的ApplicationConfiguration类:

@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages = {"com.sample.project"})
@PropertySource({"classpath:jpa.postgresql.properties", "classpath:hibernate.properties"})
@EnableJpaRepositories(basePackages = {"com.sample.project.repo"})
public class ApplicationConfiguration extends WebMvcConfigurerAdapter {

@Value("${javax.persistence.jdbc.driver}")
private String driverClassName;

@Value("${javax.persistence.jdbc.url}")
private String databaseUrl;

@Value("${javax.persistence.jdbc.user}")
private String databaseUser;

@Value("${javax.persistence.jdbc.password}")
private String databasePassword;

@Value("${hibernate.dialect}")
private String dialect;

@Value("${hibernate.show_sql}")
private boolean showSQL;

@Value("${hibernate.hbm2ddl.auto}")
private String hbm2ddlAuto;

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}

@Bean
@PersistenceContext
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
    entityManagerFactoryBean.setDataSource(dataSource());
    entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
    entityManagerFactoryBean.setJpaProperties(hibernateJPAProperties());
    entityManagerFactoryBean.setPackagesToScan("com.sample.project");
    return entityManagerFactoryBean;
}

@Bean
public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(driverClassName);
    dataSource.setUrl(databaseUrl);
    dataSource.setUsername(databaseUser);
    dataSource.setPassword(databasePassword);
    return dataSource;
}

public Properties hibernateJPAProperties() {
    Properties properties = new Properties();
    properties.put("hibernate.dialect", dialect);
    properties.put("hibernate.show_sql", showSQL);
    properties.put("hibernate.hbm2ddl.auto", hbm2ddlAuto);
    return properties;
}

@Bean
public JpaTransactionManager transactionManager() {
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
    return transactionManager;
}

@Bean
public FinancialProcessEntityDao financialProcessEntityDao() {
    FinancialProcessEntityDao dao = new FinancialProcessEntityDao();
    return dao;
}

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
}

}

我缺少什么?如何将我的Vaadin应用程序与Spring正确集成?

1 个答案:

答案 0 :(得分:0)

你应该试试这个:

processDao = WebApplicationContextUtils.getRequiredWebApplicationContext(
    VaadinServlet.getCurrent().getServletContext()).getBean(IProcessDao.class);

您可以在此处找到一些说明:link