且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Vaadin + Spring集成:空指针异常

更新时间:2023-01-24 22:50:49

您应该试试这个:

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

你可以在这里找到一些描述:链接


I have a Vaadin web application with UI class like this:

@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);
}
}

As u can see i add use setContent to add LogsView class which is a view - it is a fragment of declaration:

@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
}

}

My ProcessDao class:

public class ProcessDao {


@Autowired
ApplicationConfiguration applicationConfiguration;

public ProcessDao() {
}


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

    //entity manager and other stuffs

     return processList;
}
}

As u can see i did System.out.println() to check if im getting applicationConfiguration object. Im getting null. This is main problem.

this is my ApplicationConfiguration class:

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

}

Am i something missing? How to properly integrate my Vaadin app with Spring?

You should try this:

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

You can find some description here: link