Vaadin + Spring集成:空指针异常 [英] Vaadin + Spring Integration: Null Pointer exception

查看:125
本文介绍了Vaadin + Spring集成:空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @SuppressWarnings(serial)
我有一个Vaadin web应用程序@Theme(mytheme)
公共类LogsUI扩展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);




$ b $ p
$ b你可以看到我添加使用 setContent 添加LogsView类,它是一个视图 - 它是一个声明的片段:

  @SuppressWarnings(serial)
公共类LogsView扩展CustomComponent implements视图{

//一些变量,按钮,组件等
ProcessDao processDao;


//示例方法
void sampleMethod(){
processDao = new ProcesDao;
processDao.getAllprocesses(); //只是示例,不管逻辑如何
}

}

My ProcessDao class:

  public class ProcessDao {


@Autowired
ApplicationConfiguration applicationConfiguration;

public ProcessDao(){
}


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

//实体管理器和其他东西

return processList;


$ / code $ / pre

你可以看到我做了code > 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扩展WebMvcConfigurerAdapter {

@Value($ {javax.persistence.jdbc。驱动程序})
私人字符串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方言;

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

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

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


$Be
@PersistenceContext
LocalContainerEntityManagerFactoryBean entityManagerFactory(){
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource());
entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
entityManagerFactoryBean.setJpaProperties(hibernateJPAProperties());
entityManagerFactoryBean.setPackagesToScan(com.sample.project);
返回entityManagerFactoryBean;


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

$ b $公共属性hibernateJPAProperties(){
属性properties = new Properties();
properties.put(hibernate.dialect,方言);
properties.put(hibernate.show_sql,showSQL);
properties.put(hibernate.hbm2ddl.auto,hbm2ddlAuto);
返回属性;


$Be
public JpaTransactionManager transactionManager(){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory()。getObject());
返回transactionManager;


an
public FinancialProcessEntityDao financialProcessEntityDao(){
FinancialProcessEntityDao dao = new FinancialProcessEntityDao();
返回dao;


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

}

我缺少什么吗?如何正确整合我的Vaadin应用程序与Spring?

解决方案

您应该试试这个:

  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

这篇关于Vaadin + Spring集成:空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆