- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在运行一个基于注释的配置项目,例如:
@Configuration
public class HibernateConfig {
private Map<String, String> map;
public HibernateConfig(){
init();
}
private void init(){
Properties props;
InputStream input = null;
try {
props = new Properties();
input = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
props.load(input);
Enumeration<?> e = props.propertyNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
String value = props.getProperty(key);
map.put(key, value);
}
} catch (IOException e1) {
e1.printStackTrace();
} finally{
if(input!=null){
try {
input.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
}
@Bean(name="dataSource", destroyMethod = "close")
public DataSource getDataSource(){
ComboPooledDataSource dataSource = new ComboPooledDataSource();
try {
dataSource.setDriverClass(map.get("db.driverClass"));
dataSource.setJdbcUrl(map.get("db.jdbcUrl"));
dataSource.setUser(map.get("db.user"));
dataSource.setPassword(map.get("db.password"));
dataSource.setMaxPoolSize(50);
dataSource.setMinPoolSize(5);
dataSource.setMaxConnectionAge(1800);
dataSource.setMaxIdleTime(1800);
dataSource.setAutoCommitOnClose(false);
dataSource.setInitialPoolSize(5);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
return dataSource;
}
@Bean(name="sessionFactory")
@Scope("singleton")
public FactoryBean<SessionFactory> getSessionFactory(@Qualifier("dataSource") javax.sql.DataSource dataSource){
LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.dialect", map.get("db.dialect"));
hibernateProperties.setProperty("hibernate.jdbc.batch_size", "0");
hibernateProperties.setProperty("c3p0.acquire_increment", "1");
localSessionFactoryBean.setHibernateProperties(hibernateProperties);
try {
localSessionFactoryBean.afterPropertiesSet();
} catch (IOException e) {
e.printStackTrace();
}
return localSessionFactoryBean;
}
@Bean(name="transactionManager")
public HibernateTransactionManager getTransactionManager(@Qualifier("sessionFactory") SessionFactory sessionFactory){
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
return transactionManager;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
}
堆栈竞赛的输出给出了以下异常:
ERROR ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com..web.config.HibernateConfig#0' defined in class path resource [META-INF/spring/daoContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com..web.config.HibernateConfig$$EnhancerBySpringCGLIB$$49b2842b]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1105)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1050)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com..web.config.HibernateConfig$$EnhancerBySpringCGLIB$$49b2842b]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:163)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1098)
... 22 more
Caused by: java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:434)
at java.util.Properties.load0(Properties.java:353)
at java.util.Properties.load(Properties.java:341)
at com..web.config.HibernateConfig.init(HibernateConfig.java:35)
at com..web.config.HibernateConfig.<init>(HibernateConfig.java:26)
at com..web.config.HibernateConfig$$EnhancerBySpringCGLIB$$49b2842b.<init>(<generated>)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
... 24 more
Jun 16, 2017 2:55:14 PM org.apache.catalina.core.StandardContext listenerStart
SCHWERWIEGEND: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com..web.config.HibernateConfig#0' defined in class path resource [META-INF/spring/daoContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com..web.config.HibernateConfig$$EnhancerBySpringCGLIB$$49b2842b]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1105)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1050)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com..web.config.HibernateConfig$$EnhancerBySpringCGLIB$$49b2842b]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:163)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1098)
... 22 more
Caused by: java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:434)
at java.util.Properties.load0(Properties.java:353)
at java.util.Properties.load(Properties.java:341)
at com..web.config.HibernateConfig.init(HibernateConfig.java:35)
at com..web.config.HibernateConfig.<init>(HibernateConfig.java:26)
at com..web.config.HibernateConfig$$EnhancerBySpringCGLIB$$49b2842b.<init>(<generated>)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
... 24 more
INFO SessionListener - # flyway migration...
Jun 16, 2017 2:55:14 PM org.apache.catalina.core.StandardContext listenerStart
SCHWERWIEGEND: Exception sending context initialized event to listener instance of class com..web.listener.FlywayListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com..web.config.HibernateConfig#0' defined in class path resource [META-INF/spring/daoContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com..web.config.HibernateConfig$$EnhancerBySpringCGLIB$$49b2842b]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1105)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1050)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com..web.config.HibernateConfig$$EnhancerBySpringCGLIB$$49b2842b]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:163)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1098)
... 22 more
Caused by: java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:434)
at java.util.Properties.load0(Properties.java:353)
at java.util.Properties.load(Properties.java:341)
at com..web.config.HibernateConfig.init(HibernateConfig.java:35)
at com..web.config.HibernateConfig.<init>(HibernateConfig.java:26)
at com..web.config.HibernateConfig$$EnhancerBySpringCGLIB$$49b2842b.<init>(<generated>)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
... 24 more
我一直在寻找一个讨论相同问题的帖子,但到目前为止我还没有找到。请问有人有想法吗?
更新:添加了堆栈跟踪
最佳答案
你正在使用Spring,那就使用Spring。不要加载您自己的属性,请使用 @PropertySource
来加载。
删除 init
方法并将其替换为 @PropertySource
,然后将 Environment
注入(inject)您的配置并使用它来检索属性。
@Configuration
@PropertySource("classpath:jdbc.properties")
public class HibernateConfig {
@Autowired
private Environment env;
@Bean
public DataSource dataSource(){
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(env.getProperty("db.driverClass"));
dataSource.setJdbcUrl(env.getProperty("db.jdbcUrl"));
dataSource.setUser(env.getProperty("db.user"));
dataSource.setPassword(env.getProperty("db.password"));
dataSource.setMaxPoolSize(50);
dataSource.setMinPoolSize(5);
dataSource.setMaxConnectionAge(1800);
dataSource.setMaxIdleTime(1800);
dataSource.setAutoCommitOnClose(false);
dataSource.setInitialPoolSize(5);
return dataSource;
}
@Bean
public LocalSessionFactoryBean sessionFactory(DataSource dataSource){
LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.dialect", map.get("db.dialect"));
hibernateProperties.setProperty("hibernate.jdbc.batch_size", "0");
localSessionFactoryBean.setHibernateProperties(hibernateProperties);
localSessionFactoryBean.setDataSource(dataSource);
return localSessionFactoryBean;
}
@Bean
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory){
return new HibernateTransactionManager(sessionFactory);
}
}
其他一些相关的事情
异常
,让配置崩溃,否则您的应用程序将处于某种奇怪的状态。 DataSource
注入(inject)到 LocalSessionFactoryBean
中,这使得显式属性设置变得无用。DataSource
的close
方法。 @Qualifier
注释@Bean
方法的返回类型尽可能具体,这是应用正确回调所必需的。afterPropertiesSet
Spring 会为你做这件事。 关于java - 由于 java.lang.NullPointerException 导致的 BeanInstantiationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44589882/
我无法摆脱错误: HTTP Status 500 - Request processing failed; nested exception is org.springframework.bean
我在 Tomcat 7 上尝试执行我的 Spring MVC 应用程序时遇到了问题。 这是我的DSLRServletController: import main.java.com.springap
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'PNLResultDAO
我刚刚开始学习Spring Boot。我的代码中有一个错误,内容为 Error creating bean with name 'alien' defined in file[E:\Programin
我有一个非常基本的程序,但无论我对代码进行什么更改,我总是收到相同的错误消息: Error starting ApplicationContext. To display the auto-confi
我正在运行一个基于注释的配置项目,例如: @Configuration public class HibernateConfig { private Map map; public H
我正在使用 Spring Boot 1.5.4 开发一个 RESTful 服务应用程序。 我的资源类中的一个方法(findAll)采用Pageable参数并使用它来调用服务方法。我在对该方法进行单元测
我的一个 Controller 中的构造函数有一些问题。我尝试在构造函数中调用一项服务。该服务在 AbstractController 中 Autowiring ,但我得到了一个空指针异常。 一个组件
对于我目前正在工作的项目,要求在 spring 无法初始化某些 bean 时禁用要启动的 webapp 上下文(在初始化期间调用 web 服务,因此可能会崩溃)。 然而,当一个 bean 在初始化期间
我是从Spring reference 3.0开始学习spring的,想尝试一下如何实例化inner bean: 这是我的代码: package com.springexample; public c
我使用互联网上的示例,然后尝试从 Spring Boot 1.5.10.RELEASE 升级到 2.0.0.RC1。该应用程序可以与 Spring Boot 配合使用 版本1.5.10.RELEASE
我制作了一个简单的应用程序来测试我在小范围内遇到的这个问题。我有一个ejb: @Local public interface PersonaDAO { public void sayHello(Per
我有以下 Kafka 的 Spring 配置: import org.apache.kafka.clients.producer.ProducerConfig; import org.springfr
我有以下 Kafka 的 Spring 配置: import org.apache.kafka.clients.producer.ProducerConfig; import org.springfr
我是 Spring 新手,请任何人帮忙解决这个异常。 异常: Exception in thread "main" org.springframework.beans.factory.BeanCrea
我正在做 spring 登录应用程序。当我在 WAS 服务器上运行应用程序时,出现了一些异常。错误 org.springframework.web.servlet.FrameworkServlet i
如果我尝试向构造函数注入(inject) Facebook 参数,我将尝试使用 facebook api 使用 spring + thymeleaf + hibernate 创建 Facebook 应
我是一名优秀的程序员,十分优秀!