Spring mvc(Spring Bootではない)で、mavenを使ってwebアプリケーションを新規で作っています。
ですが、ビルドし、実行するときにエラーがでてしまい、実行できません。
バージョン
- Spring mvc 3.2.3
- maven 2.6
mainメソッドファイル
App.java
1package com.example.demo; 2 3import org.springframework.ui.ModelMap; 4 5public class App { 6 public static void main(String[] args) { 7 8 TestSpringMVCController tsc = new TestSpringMVCController(); 9 ModelMap model = new ModelMap(); 10 tsc.testSpringMVCController(model); 11 } 12} 13
Controllerファイル
TestSpringMVCController
1package com.example.demo; 2 3import java.io.File; 4import java.io.FileWriter; 5import java.io.IOException; 6import java.util.List; 7import java.util.Map; 8 9import javax.sql.DataSource; 10 11import org.apache.commons.logging.Log; 12import org.apache.commons.logging.LogFactory; 13import org.springframework.beans.factory.annotation.Autowired; 14import org.springframework.context.ApplicationContext; 15import org.springframework.context.annotation.Scope; 16import org.springframework.context.support.FileSystemXmlApplicationContext; 17import org.springframework.jdbc.core.JdbcTemplate; 18import org.springframework.stereotype.Component; 19import org.springframework.stereotype.Controller; 20import org.springframework.stereotype.Repository; 21import org.springframework.ui.ModelMap; 22import org.springframework.web.bind.annotation.RequestMapping; 23 24@Controller 25@Component 26@Scope("singleton") 27@Repository 28public class TestSpringMVCController { 29 30 static ApplicationContext context = new FileSystemXmlApplicationContext("file:dataSource.xml"); 31 32 @Autowired 33 protected JdbcTemplate jdbcTemplate = new JdbcTemplate(); 34 protected DataSource dataSource = (DataSource)context.getBean("dataSource"); 35 36 public DataSource getDataSource() { 37 return dataSource; 38 } 39 40 public void setDataSource(DataSource dataSource) { 41 this.dataSource = dataSource; 42 jdbcTemplate.setDataSource(getDataSource()); 43 } 44 45 @Autowired 46 @RequestMapping("/") 47 public String testSpringMVCController(ModelMap model) { 48 49 Log log = LogFactory.getLog(TestSpringMVCController.class); 50 } 51}
設定ファイル
dataSource.xml
1<?xml version="1.0" encoding="UTF-8"?> 2<beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 6 http://www.springframework.org/schema/context 7 http://www.springframework.org/schema/context/spring-context-4.2.xsd"> 8 9 <context:component-scan base-package="com.example.demo" /> 10 11 <bean id="testSpringMVCController" class="com.example.demo.TestSpringMVCController"> 12 <property name="dataSource" ref="dataSource"/> 13 </bean> 14 15 <bean id="dataSource" 16 class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 17 <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 18 <property name="url" value="jdbc:mysql://XXXXXXXXXXXXX:3306/table" /> 19 <property name="username" value="XXX" /> 20 <property name="password" value="YYY" /> 21 </bean> 22 23 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 24 <property name="dataSource" ref="dataSource"/> 25 <constructor-arg ref="dataSource" /> 26 </bean> 27 28 <bean id="main" class="com.example.demo.App" /> 29</beans>
尚、設定ファイルは別ファイルとして、実行ファイルと同じ階層に配置しています。
ビルドまではできるのですが、jarファイルを実行するときに下記のエラーが出力されます。
10 31, 2018 1:28:08 午後 org.springframework.context.support.AbstractApplicationContext prepareRefresh 情報: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@66a29884: startup date [Wed Oct 31 13:28:08 JST 2018]; root of context hierarchy 10 31, 2018 1:28:09 午後 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 情報: Loading XML bean definitions from URL [file:dataSource.xml] 10 31, 2018 1:28:14 午後 org.springframework.beans.factory.support.DefaultListableBeanFactory registerBeanDefinition 情報: Overriding bean definition for bean 'testSpringMVCController': replacing [Generic bean: class [com.example.demo.TestSpringMVCController]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in URL [jar:file:/root/mvc/testspringmvc-0.0.1-SNAPSHOT.jar!/com/example/demo/TestSpringMVCController.class]] with [Generic bean: class [com.example.demo.TestSpringMVCController]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in URL [file:dataSource.xml]] 10 31, 2018 1:28:15 午後 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons 情報: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@f6c48ac: defining beans [testSpringMVCController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,dataSource,jdbcTemplate,main,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy 10 31, 2018 1:28:16 午後 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons 情報: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@f6c48ac: defining beans [testSpringMVCController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,dataSource,jdbcTemplate,main,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy Exception in thread "main" java.lang.ExceptionInInitializerError at com.example.demo.App.main(App.java:11) Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testSpringMVCController' defined in URL [file:dataSource.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.example.demo.TestSpringMVCController]: Constructor threw exception; nested exception is java.lang.NullPointerException at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1007) (略) at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:140) at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:84) at com.example.demo.TestSpringMVCController.<clinit>(TestSpringMVCController.java:30) ... 1 more Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.example.demo.TestSpringMVCController]: 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:87) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1000) ... 14 more Caused by: java.lang.NullPointerException at com.example.demo.TestSpringMVCController.<init>(TestSpringMVCController.java:34) (略) ... 16 more
恐らくdataSource.xmlファイルの中身に問題があると思い、調べて修正したのですが、原因がわかりませんでした。
どなたかわかる方、ご教授いただきたいです。よろしくお願いいたします。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/31 12:12
2018/11/01 00:29