1

我有一个 Maven 控制器 Spring Web 应用程序,它在命令行上运行良好,mvn clean tomcat:run但我无法让它与运行/调试配置一起工作。我得到一长串自动装配依赖项失败的结尾:

... bean 的实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 bean 类 [com.mycompany.config.DataSourceConfig$$EnhancerByCGLIB$$543b87de]:构造函数抛出异常;嵌套异常是 java.lang.NumberFormatException: null

这是有问题的课程:

import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.beans.PropertyVetoException;
import java.net.URISyntaxException;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;

@Configuration
public class DataSourceConfig {


    //change PACKAGE_TO_SCAN
    private static final String PACKAGE_TO_SCAN = "com.mycompany"; 
    private static final int MODE_DEV = 0;
    private static final int MODE_STG = 1;
    private static final int MODE_PROD = 2;


    private Logger log = LoggerFactory.getLogger(this.getClass());

    private int environment = Integer.parseInt(System.getenv("ENVIRONMENT"));

    private String dburl = System.getenv("UMWORKFLOW_DATABASE_URL");
    private String dbuser = System.getenv("UMWORKFLOW_DATABASE_USER");
    private String dbpass = System.getenv("UMWORKFLOW_DATABASE_PASSWORD");



    public DataSourceConfig(){

    }

    @Bean(destroyMethod="close")
    public ComboPooledDataSource dataSource() throws URISyntaxException, PropertyVetoException {

        ComboPooledDataSource ds = new ComboPooledDataSource();



        ds.setDriverClass("org.postgresql.Driver");
        ds.setMinPoolSize(1);
        ds.setMaxPoolSize(10);
        ds.setAcquireIncrement(1);
        ds.setIdleConnectionTestPeriod(300);
        ds.setMaxStatements(0);
        ds.setCheckoutTimeout(100);

        ds.setJdbcUrl(dburl);
        ds.setUser(dbuser);
        ds.setPassword(dbpass);

        return ds;

    }

    @Bean
    public AnnotationSessionFactoryBean sessionFactory() throws URISyntaxException, PropertyVetoException {

        AnnotationSessionFactoryBean sf = new AnnotationSessionFactoryBean();
        sf.setDataSource(dataSource());
        String[] packageToScan = new String[1];
        packageToScan[0] = PACKAGE_TO_SCAN;
        sf.setPackagesToScan(packageToScan);
        Properties hibProp = new Properties();
        hibProp.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");


        //modes create, create-drop, update, validate
        if( environment == MODE_DEV) {

            hibProp.put("hibernate.hbm2ddl.auto", "update");

        } else if ( environment == MODE_STG) {

            hibProp.put("hibernate.hbm2ddl.auto", "update");
        } else {

            hibProp.put("hibernate.hbm2ddl.auto", "update");
        }


        sf.setHibernateProperties(hibProp);
        return sf;
    }

}
4

1 回答 1

1

系统属性ENVIRONMENT似乎是自定义的。

private int environment = Integer.parseInt(System.getenv("ENVIRONMENT"));

System.getenv()返回并null导致. IllegalArgumentException您需要在run/debug配置中设置该属性。

于 2013-10-15T18:10:22.940 回答