我用hibernate和spring开发一个项目。 我为所有人使用注释。 我试图在JBOSS AS 7中部署OpenShift。 我一直遇到DataSource bean的错误,我不知道我做错了什么。
在日志中显示:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [clanwar/config/MainConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource clanwar.config.MainConfig.dataSource()] threw exception; nested exception is java.lang.NullPointerException
您可以在此处查看完整堆栈跟踪:http://clanwaropenshift-stel.rhcloud.com/
我的DataSource bean:
@Bean
public DataSource dataSource() {
SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
Driver driver = null;
try {
driver = new com.mysql.jdbc.Driver();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dataSource.setDriver(driver);
dataSource.setUrl(env.getProperty(JDBC_URL));
dataSource.setUsername(env.getProperty(JDBC_USER));
dataSource.setPassword(env.getProperty(JDBC_PASSWORD));
return dataSource;
}
我也尝试使用DriverManagerDataSource
和org.apache.commons.dbcp.BasicDataSource
,但我遇到同样的错误。
提前致谢。
1编辑
当应用程序部署时,记录器显示创建两次dataSource。
首先显示:
env - > StandardServletEnvironment {activeProfiles=[], defaultProfiles=[default], propertySources=[servletConfigInitParams,servletContextInitParams,jndiProperties,systemProperties,systemEnvironment,class path resource [application.properties]]}
datasource - > com.mysql.jdbc.Driver@b8dec7
但第二次显示:
env - > null
datasource - > com.mysql.jdbc.Driver@1133cea
日志的一部分:
014/12/22 11:39:42,848 INFO [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] (MSC service thread 1-3) Refreshing Root WebApplicationContext: startup date [Mon Dec 22 11:39:42 EST 2014]; root of context hierarchy
2014/12/22 11:39:43,035 INFO [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] (MSC service thread 1-3) Registering annotated classes: [class clanwar.config.MainConfig,class clanwar.config.MvcConfig,class clanwar.config.SecurityConfig]
2014/12/22 11:39:43,673 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] (MSC service thread 1-3) Overriding bean definition for bean 'propertyPlaceHolderConfigurer': replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=mainConfig; factoryMethodName=propertyPlaceHolderConfigurer; initMethodName=null; destroyMethodName=(inferred); defined in class clanwar.config.MainConfig] with [Root bean: class [clanwar.config.MvcConfig]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=propertyPlaceHolderConfigurer; initMethodName=null; destroyMethodName=(inferred); defined in class clanwar.config.MvcConfig]
2014/12/22 11:39:44,443 INFO [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] (MSC service thread 1-3) JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2014/12/22 11:39:45,385 INFO [clanwar.config.MainConfig] (MSC service thread 1-3) ___________________________________________________________
2014/12/22 11:39:45,394 INFO [clanwar.config.MainConfig] (MSC service thread 1-3) ___________________________________________________________
2014/12/22 11:39:45,394 INFO [clanwar.config.MainConfig] (MSC service thread 1-3) ___________________________________________________________
2014/12/22 11:39:45,400 INFO [clanwar.config.MainConfig] (MSC service thread 1-3) StandardServletEnvironment {activeProfiles=[], defaultProfiles=[default], propertySources=[servletConfigInitParams,servletContextInitParams,jndiProperties,systemProperties,systemEnvironment,class path resource [application.properties]]}
2014/12/22 11:39:45,401 INFO [clanwar.config.MainConfig] (MSC service thread 1-3) com.mysql.jdbc.Driver@b8dec7
2014/12/22 11:39:45,407 INFO [clanwar.config.MainConfig] (MSC service thread 1-3) ___________________________________________________________
2014/12/22 11:39:45,407 INFO [clanwar.config.MainConfig] (MSC service thread 1-3) ___________________________________________________________
2014/12/22 11:39:45,409 INFO [clanwar.config.MainConfig] (MSC service thread 1-3) ___________________________________________________________
2014/12/22 11:39:45,641 INFO [org.hibernate.annotations.common.Version] (MSC service thread 1-3) HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
2014/12/22 11:39:45,648 INFO [org.hibernate.Version] (MSC service thread 1-3) HHH000412: Hibernate Core {4.2.0.Final}
2014/12/22 11:39:45,652 INFO [org.hibernate.cfg.Environment] (MSC service thread 1-3) HHH000206: hibernate.properties not found
2014/12/22 11:39:45,655 INFO [org.hibernate.cfg.Environment] (MSC service thread 1-3) HHH000021: Bytecode provider name : javassist
2014/12/22 11:39:46,467 WARN [org.hibernate.mapping.RootClass] (MSC service thread 1-3) HHH000038: Composite-id class does not override equals(): clanwar.model.ClanMember
2014/12/22 11:39:46,467 WARN [org.hibernate.mapping.RootClass] (MSC service thread 1-3) HHH000039: Composite-id class does not override hashCode(): clanwar.model.ClanMember
2014/12/22 11:39:46,469 WARN [org.hibernate.mapping.RootClass] (MSC service thread 1-3) HHH000038: Composite-id class does not override equals(): clanwar.model.WarPlayer
2014/12/22 11:39:46,469 WARN [org.hibernate.mapping.RootClass] (MSC service thread 1-3) HHH000039: Composite-id class does not override hashCode(): clanwar.model.WarPlayer
2014/12/22 11:39:46,621 WARN [org.hibernate.engine.jdbc.internal.JdbcServicesImpl] (MSC service thread 1-3) HHH000342: Could not obtain connection to query metadata : Cannot load connection class because of underlying exception: 'java.lang.NumberFormatException: For input string: "$OPENSHIFT_MYSQL_DB_PORT"'.
2014/12/22 11:39:46,630 INFO [org.hibernate.dialect.Dialect] (MSC service thread 1-3) HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2014/12/22 11:39:46,666 INFO [org.hibernate.engine.jdbc.internal.LobCreatorBuilder] (MSC service thread 1-3) HHH000422: Disabling contextual LOB creation as connection was null
2014/12/22 11:39:46,735 INFO [org.hibernate.engine.transaction.internal.TransactionFactoryInitiator] (MSC service thread 1-3) HHH000399: Using default transaction strategy (direct JDBC transactions)
2014/12/22 11:39:46,758 INFO [org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory] (MSC service thread 1-3) HHH000397: Using ASTQueryTranslatorFactory
2014/12/22 11:39:46,950 INFO [org.hibernate.validator.util.Version] (MSC service thread 1-3) Hibernate Validator 4.2.0.Final
2014/12/22 11:39:48,873 INFO [org.springframework.orm.hibernate4.HibernateTransactionManager] (MSC service thread 1-3) Using DataSource [org.springframework.jdbc.datasource.SimpleDriverDataSource@1828bd6] of Hibernate SessionFactory for HibernateTransactionManager
2014/12/22 11:39:49,639 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter] (MSC service thread 1-3) Looking for @ControllerAdvice: Root WebApplicationContext: startup date [Mon Dec 22 11:39:42 EST 2014]; root of context hierarchy
2014/12/22 11:39:49,861 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-3) Mapped "{[/war/],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView clanwar.controller.WarController.warHandler()
2014/12/22 11:39:49,866 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-3) Mapped "{[/war/create],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView clanwar.controller.WarController.createWarHandler()
2014/12/22 11:39:49,877 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-3) Mapped "{[/war/new],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView clanwar.controller.WarController.newWarHandler(java.util.List<java.lang.String>)
2014/12/22 11:39:49,878 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-3) Mapped "{[/war/search],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView clanwar.controller.WarController.searchWarHandler(int)
2014/12/22 11:39:49,879 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-3) Mapped "{[/war/saveplayer],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView clanwar.controller.WarController.savePlayerInWarHandler(clanwar.model.WarPlayer)
2014/12/22 11:39:49,888 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-3) Mapped "{[/createPinpinelas],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public void clanwar.controller.HelperController.playerSaveHandler()
2014/12/22 11:39:49,893 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-3) Mapped "{[/json/test],methods=[],params=[],headers=[],consumes=[],produces=[application/json],custom=[]}" onto public clanwar.model.WarPlayer clanwar.controller.HelperController.jsonTest()
2014/12/22 11:39:49,893 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-3) Mapped "{[/test],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView clanwar.controller.HelperController.mvcTest()
2014/12/22 11:39:49,901 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-3) Mapped "{[/login],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView clanwar.controller.LoginController.playerLoginHandler(java.lang.String)
2014/12/22 11:39:49,914 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-3) Mapped "{[/signup],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView clanwar.controller.LoginController.signUpViewHandler()
2014/12/22 11:39:49,920 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-3) Mapped "{[/signup],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView clanwar.controller.LoginController.signUpHandler(clanwar.model.Player)
2014/12/22 11:39:49,922 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-3) Mapped "{[/clan/choose],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView clanwar.controller.ClanController.clanChooseHandler()
2014/12/22 11:39:49,932 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-3) Mapped "{[/clan/new],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView clanwar.controller.ClanController.clanNewViewHandler()
2014/12/22 11:39:49,933 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-3) Mapped "{[/clan/new],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView clanwar.controller.ClanController.clanNewHandler(clanwar.model.Clan,java.lang.String)
2014/12/22 11:39:49,933 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-3) Mapped "{[/clan/select],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView clanwar.controller.ClanController.clanSelectHandler()
2014/12/22 11:39:49,934 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-3) Mapped "{[/clan/view],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView clanwar.controller.ClanController.clanViewHandler()
2014/12/22 11:39:49,934 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-3) Mapped "{[/clan/join],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView clanwar.controller.ClanController.clanJoinHandler(clanwar.model.ClanMember)
2014/12/22 11:39:49,935 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-3) Mapped "{[/clan/leave],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView clanwar.controller.ClanController.clanLeaveHandler(clanwar.model.ClanMember)
2014/12/22 11:39:49,939 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-3) Mapped "{[/],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView clanwar.controller.MainController.homeHandler(java.security.Principal)
2014/12/22 11:39:49,940 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-3) Mapped "{[/denied],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView clanwar.controller.MainController.deniedHandler()
2014/12/22 11:39:49,941 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-3) Mapped "{[/json/war],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public clanwar.model.War clanwar.controller.JsonController.jsonWarHandler(int)
2014/12/22 11:39:49,941 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-3) Mapped "{[/json/wars],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.util.List<clanwar.model.War> clanwar.controller.JsonController.jsonWarsHandler()
2014/12/22 11:39:49,943 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-3) Mapped "{[/player/update],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public void clanwar.controller.PlayerController.playerUdapteHandler(clanwar.model.Player)
2014/12/22 11:39:49,950 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-3) Mapped "{[/player/profile],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView clanwar.controller.PlayerController.profileHandler()
2014/12/22 11:39:49,950 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-3) Mapped "{[/player/password],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView clanwar.controller.PlayerController.passwordHandler()
2014/12/22 11:39:49,998 INFO [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] (MSC service thread 1-3) Mapped URL path [/web/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014/12/22 11:39:51,306 INFO [org.springframework.security.web.DefaultSecurityFilterChain] (MSC service thread 1-3) Creating filter chain: Ant [pattern='/web/**'], []
2014/12/22 11:39:51,447 INFO [org.springframework.security.web.DefaultSecurityFilterChain] (MSC service thread 1-3) Creating filter chain: org.springframework.security.web.util.matcher.AnyRequestMatcher@1, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@1c1d169, org.springframework.security.web.context.SecurityContextPersistenceFilter@128953, org.springframework.security.web.header.HeaderWriterFilter@10bcae, org.springframework.security.web.csrf.CsrfFilter@5e8f18, org.springframework.security.web.authentication.logout.LogoutFilter@c1924a, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@ac9750, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@29470e, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@baae53, org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter@37eaff, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@19b7bc4, org.springframework.security.web.session.SessionManagementFilter@f6d8e6, org.springframework.security.web.access.ExceptionTranslationFilter@137a0cf, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@629d2e]
2014/12/22 11:39:51,573 INFO [org.springframework.web.context.ContextLoader] (MSC service thread 1-3) Root WebApplicationContext: initialization completed in 8731 ms
2014/12/22 11:39:51,636 INFO [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/]] (MSC service thread 1-3) Initializing Spring FrameworkServlet 'spring/spring-context'
2014/12/22 11:39:51,649 INFO [org.springframework.web.servlet.DispatcherServlet] (MSC service thread 1-3) FrameworkServlet 'spring/spring-context': initialization started
2014/12/22 11:39:51,688 INFO [org.springframework.web.context.support.XmlWebApplicationContext] (MSC service thread 1-3) Refreshing WebApplicationContext for namespace 'spring/spring-context-servlet': startup date [Mon Dec 22 11:39:51 EST 2014]; parent: Root WebApplicationContext
2014/12/22 11:39:51,742 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] (MSC service thread 1-3) Loading XML bean definitions from ServletContext resource [/WEB-INF/spring/spring-context-servlet.xml]
2014/12/22 11:39:52,976 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] (MSC service thread 1-3) Overriding bean definition for bean 'propertyPlaceHolderConfigurer': replacing [Root bean: class [clanwar.config.MvcConfig]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=propertyPlaceHolderConfigurer; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [clanwar/config/MvcConfig.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=mainConfig; factoryMethodName=propertyPlaceHolderConfigurer; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [clanwar/config/MainConfig.class]]
2014/12/22 11:39:53,116 WARN [org.springframework.context.annotation.ConfigurationClassEnhancer] (MSC service thread 1-3) @Bean method MainConfig.propertyPlaceHolderConfigurer is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring @Configuration class. Add the 'static' modifier to this method to avoid these container lifecycle issues; see @Bean javadoc for complete details
2014/12/22 11:39:53,193 INFO [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] (MSC service thread 1-3) JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2014/12/22 11:39:53,489 INFO [clanwar.config.MainConfig] (MSC service thread 1-3) ___________________________________________________________
2014/12/22 11:39:53,495 INFO [clanwar.config.MainConfig] (MSC service thread 1-3) ___________________________________________________________
2014/12/22 11:39:53,497 INFO [clanwar.config.MainConfig] (MSC service thread 1-3) ___________________________________________________________
2014/12/22 11:39:53,497 INFO [clanwar.config.MainConfig] (MSC service thread 1-3) null
2014/12/22 11:39:53,497 INFO [clanwar.config.MainConfig] (MSC service thread 1-3) com.mysql.jdbc.Driver@1133cea
2014/12/22 11:39:53,497 INFO [clanwar.config.MainConfig] (MSC service thread 1-3) ___________________________________________________________
2014/12/22 11:39:53,497 INFO [clanwar.config.MainConfig] (MSC service thread 1-3) ___________________________________________________________
2014/12/22 11:39:53,498 INFO [clanwar.config.MainConfig] (MSC service thread 1-3) ___________________________________________________________
答案 0 :(得分:0)
我的问题是在web.xml和spring-mvc-dispatcher.xml上,我有<component-scan ...>
,那些创建配置两次(我不知道为什么)。删除标签<component-scan ...>
我解决了所有问题。