【问题标题】:Spring: mvn clean tomcat:run works on command line but not IntelliJSpring: mvn clean tomcat:run 在命令行上工作,但不是 IntelliJ
【发布时间】:2013-10-23 15:53:46
【问题描述】:

我有一个 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;
    }

}

【问题讨论】:

    标签: java spring maven tomcat intellij-idea


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-01
      • 1970-01-01
      • 2019-03-13
      • 2023-03-17
      • 2021-04-23
      • 1970-01-01
      相关资源
      最近更新 更多