我有一个Maven控制器Spring Web应用程序,它在mvn clean tomcat:run
的命令行上运行正常但我无法使用运行/调试配置。我得到一长串的autowire依赖关系失败,以:
... 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;
}
}
答案 0 :(得分:1)
系统属性ENVIRONMENT
似乎是自定义的。
private int environment = Integer.parseInt(System.getenv("ENVIRONMENT"));
因此System.getenv()
正在返回null
并导致IllegalArgumentException
。您需要在run/debug
配置中设置该属性。