물에 살고싶은 개발자

@Controller 에 의존주입이 안될때 본문

Spring Framework

@Controller 에 의존주입이 안될때

돼지사랑 2016. 7. 8. 14:04

사수와 나의 삽질시간을 합치면 거진 12시간쯤 되는 삽질을 하게되었다.


이클립스에서 생성한 프로젝트 초기상태에서 몇가지 클래스만 추가하고


pom에 DB,json관련 어펜덴시 몇개 추가하고 context.xml에 의존주입만 딱 넣었는데


세상에나마상에나 500에러가 뜨더라


이게 뭔일이지 뭐지 하고 에러메시지랑 이것저것해서 한참을 삽질을 했다.


원인은 결국 밝히지못했지만 문제가 되는 구간은 발견!


그것이 Controller 클래스였는데,


Controller클래스에 생성자 방식으로 의존주입을 받았더니 에러가 나더라...


그래서 방식을 설정클래스 방식으로 바꾸려 했지만 어째선지 xml녀석이 너임마 니 클래스에


setter가 없어!! 하고 에러를 내뱉더라..난 분명 set메서드(아규먼트) 로 메서드를 만들었는데..


아무튼 그렇게 (둘이합해)12시간가까이를 릴레이로 삽질을 하다 결국 질문글을 올렸는데


삽질한시간이 억울할정도로 쉽게 해결됏다.


@Autowired를 사용하니 그냥 되더라...


아무튼 삽질한시간이 아까워 하소연을 했지만 지금부터가 본문이다.



에러메시지와 코드가 상당히 길다.

결론만 알고싶은사람은 맨밑에 결론이 있다.(사실 결론은 위에 빨간글씨다..)




이것이 에러 전문

HTTP Status 500 - Servlet.init() for servlet appServlet threw exception


type Exception report

message Servlet.init() for servlet appServlet threw exception

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Servlet.init() for servlet appServlet threw exception
	org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
	org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
	org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436)
	org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
	org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
	org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
	java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	java.lang.Thread.run(Unknown Source)

root cause

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getController' defined in file [C:\Users\MJ\Desktop\Dev\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp2\wtpwebapps\ShareTime\WEB-INF\classes\com\olink\sharetime\Controller\GetController.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.olink.sharetime.Controller.GetController]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.olink.sharetime.Controller.GetController.<init>()
	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1007)
	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:953)
	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:487)
	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
	org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
	org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
	org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
	org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
	org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
	org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
	org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
	org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:651)
	org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:599)
	org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665)
	org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:518)
	org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:459)
	org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
	javax.servlet.GenericServlet.init(GenericServlet.java:158)
	org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
	org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
	org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436)
	org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
	org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
	org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
	java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	java.lang.Thread.run(Unknown Source)

root cause

org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.olink.sharetime.Controller.GetController]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.olink.sharetime.Controller.GetController.<init>()
	org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:83)
	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1000)
	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:953)
	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:487)
	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
	org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
	org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
	org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
	org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
	org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
	org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
	org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
	org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:651)
	org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:599)
	org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665)
	org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:518)
	org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:459)
	org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
	javax.servlet.GenericServlet.init(GenericServlet.java:158)
	org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
	org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
	org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436)
	org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
	org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
	org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
	java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	java.lang.Thread.run(Unknown Source)

root cause

java.lang.NoSuchMethodException: com.olink.sharetime.Controller.GetController.<init>()
	java.lang.Class.getConstructor0(Unknown Source)
	java.lang.Class.getDeclaredConstructor(Unknown Source)
	org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:78)
	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1000)
	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:953)
	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:487)
	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
	org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
	org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
	org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
	org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
	org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
	org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
	org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
	org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:651)
	org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:599)
	org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665)
	org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:518)
	org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:459)
	org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
	javax.servlet.GenericServlet.init(GenericServlet.java:158)
	org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
	org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
	org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436)
	org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
	org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
	org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
	java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	java.lang.Thread.run(Unknown Source)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.69 logs.


어마어마하게 길다 진짜 길다 뭔개소린지도 모르겠는게 오지게 길다..


그리고..

Controller 클래스

package com.olink.sharetime.Controller;
import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/request/get" )
public class GetController 
{
	private JdbcTemplate jdbc;
	
	public GetController(DataSource tr)
	{
		this.jdbc = new JdbcTemplate(tr);
	}
	
	@RequestMapping(value = "/home", method = RequestMethod.GET)
	public @ResponseBody String home()
	{		 		
		return "Fucking Test";
	}	
}



대충보면 뭐가문젠지 알수가 없다. 책이나 구글링해서 나오는대로 고대로 코딩한 

아주 순수한 형태다. 어디 잘못될껀덕지조차 없다.

(원래는 dao클래스에서 jdbc를 의존주입받은다음 컨트롤러에서 dao를 의존주입받았었는데

에러를 벗어나지못하고 이렇게저렇게 발버둥치다가 dao도 재끼고 컨트롤러에서 다처리하면 될까나..?해서 해놧던 코드다)


그냥 얌전히 Fucking Test를 뱉으면 되는데 home()메서드에 진입도 못하고 에러가 난다.


root-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
		
	<!-- DAO를 위한 dataSource 의존주입 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="driverClass" value="com.mysql.jdbc.Driver" />
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/MySQL57?characterEncoding=utf8" />
		<property name="user" value="root" />
		<property name="password" value="ppost" />
	</bean>	
	
	<bean id="mInitDAO" class="com.olink.sharetime.DAO.GetDAO">
		<constructor-arg ref="dataSource" />
	</bean>
	<bean id="mRuntimeDAO" class="com.olink.sharetime.DAO.UpdateDAO">
		<constructor-arg ref="dataSource" />
	</bean>	
	<bean id="mInsertDAO" class="com.olink.sharetime.DAO.InsertDAO">
		<constructor-arg ref="dataSource" />
	</bean>
	
	<bean id="mShareTrans" class="com.olink.sharetime.Controller.ShareTrans">
		<constructor-arg ref="mInsertDAO" />
	</bean>
	
	<bean id="mGetController" class="com.olink.sharetime.Controller.GetController">
		<constructor-arg ref="dataSource" />
	</bean>	
 
	<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
</beans>


다시한번 둘러본 이곳조차 아무 문제가 없어보인다. 평범한 의존주입설정xml파일이다..




결론은 

쓰다보니 뭔 질문글처럼 됏는데 

어쨋든 결론은 다 재끼고 @Autowired 쓰면된다.

나를 6시간(사수도 6시간)이나 뺑이치게 만든 빌어먹을 

컨스트럭쳐 태그는 싹 지워버리고 빈만 남겨둔채 의존주입받으려는 필드나 생성자에다가 

냅다 @Autowired를 달자

Comments