[英]Spring: mvn clean tomcat:run works on command line but not IntelliJ
我有一個Maven控制器Spring Web應用程序,它可以在命令行上使用mvn clean tomcat:run
但是我無法使其與運行/調試配置一起使用。 我得到了一長串自動裝配依賴項失敗,這些失敗以:
... bean的實例化失敗; 嵌套的異常是org.springframework.beans.BeanInstantiationException:無法實例化bean類[com.mycompany.config.DataSourceConfig $$ EnhancerByCGLIB $$ 543b87de]:構造方法拋出了異常; 嵌套異常是java.lang.NumberFormatException:空
這是令人反感的課程:
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;
}
}
系統屬性“ ENVIRONMENT
似乎是自定義的。
private int environment = Integer.parseInt(System.getenv("ENVIRONMENT"));
因此System.getenv()
返回null
並導致IllegalArgumentException
。 您將需要在run/debug
配置中設置該屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.