javaのspringframeworkの勉強をしています。
使っている環境は、eclipseの2018−2019年版です。
コンポーネットスキャンをXMLファイルで有効にするために、
XMLファイルに、<context:component-scan base-package="対象パッケージ">
と記述し,実行しましたがうまくいきません。
発生している問題・エラーメッセージ
tomcatエラーメッセージ
情報: Server version: Apache Tomcat/8.5.34 [Sun Oct 06 01:08:26 JST 2019] 情報: Server built: Sep 4 2018 22:28:22 UTC [Sun Oct 06 01:08:26 JST 2019] 情報: Server number: 8.5.34.0 [Sun Oct 06 01:08:26 JST 2019] 情報: OS Name: Mac OS X [Sun Oct 06 01:08:26 JST 2019] 情報: OS Version: 10.13.6 [Sun Oct 06 01:08:26 JST 2019] 情報: Architecture: x86_64 [Sun Oct 06 01:08:26 JST 2019] 情報: Java Home: /Applications/Eclipse_2018-09.app/Contents/java/8/Home/jre [Sun Oct 06 01:08:26 JST 2019] 情報: JVM Version: 1.8.0_181-b13 [Sun Oct 06 01:08:26 JST 2019] 情報: JVM Vendor: Oracle Corporation [Sun Oct 06 01:08:26 JST 2019] 情報: CATALINA_BASE: /Applications/Eclipse_2018-09.app/Contents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0 [Sun Oct 06 01:08:26 JST 2019] 情報: CATALINA_HOME: /Applications/Eclipse_2018-09.app/Contents/tomcat/8 [Sun Oct 06 01:08:26 JST 2019] 情報: Command line argument: -Djava.ext.dirs=/Applications/Eclipse_2018-09.app/Contents/java/8/Home/jre/lib/ext [Sun Oct 06 01:08:26 JST 2019] 情報: Command line argument: -Dcatalina.base=/Applications/Eclipse_2018-09.app/Contents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0 [Sun Oct 06 01:08:26 JST 2019] 情報: Command line argument: -Dcatalina.home=/Applications/Eclipse_2018-09.app/Contents/tomcat/8 [Sun Oct 06 01:08:26 JST 2019] 情報: Command line argument: -Dwtp.deploy=/Applications/Eclipse_2018-09.app/Contents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps [Sun Oct 06 01:08:26 JST 2019] 情報: Command line argument: -Djava.endorsed.dirs=/Applications/Eclipse_2018-09.app/Contents/tomcat/8/endorsed [Sun Oct 06 01:08:26 JST 2019] 情報: Command line argument: -Dfile.encoding=UTF-8 [Sun Oct 06 01:08:26 JST 2019] 情報: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/hyuma/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.] [Sun Oct 06 01:08:26 JST 2019] 情報: Initializing ProtocolHandler ["http-nio-8080"] [Sun Oct 06 01:08:26 JST 2019] 情報: Using a shared selector for servlet write/read [Sun Oct 06 01:08:26 JST 2019] 情報: Initializing ProtocolHandler ["ajp-nio-8009"] [Sun Oct 06 01:08:26 JST 2019] 情報: Using a shared selector for servlet write/read [Sun Oct 06 01:08:26 JST 2019] 情報: Initialization processed in 944 ms [Sun Oct 06 01:08:26 JST 2019] 情報: サービス [Catalina] を起動します [Sun Oct 06 01:08:26 JST 2019] 情報: Starting Servlet Engine: Apache Tomcat/8.5.34 [Sun Oct 06 01:08:27 JST 2019] 情報: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. [Sun Oct 06 01:08:31 JST 2019] 情報: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. [Sun Oct 06 01:08:37 JST 2019] 情報: No Spring WebApplicationInitializer types detected on classpath [Sun Oct 06 01:08:37 JST 2019] 情報: Initializing Spring root WebApplicationContext [Sun Oct 06 01:08:37 JST 2019] 情報: Root WebApplicationContext: initialization started [Sun Oct 06 01:08:37 JST 2019] 情報: Refreshing Root WebApplicationContext: startup date [Sun Oct 06 01:08:37 JST 2019]; root of context hierarchy [Sun Oct 06 01:08:37 JST 2019] 情報: Loading XML bean definitions from class path resource [spring/application-config.xml] [Sun Oct 06 01:08:38 JST 2019] 情報: JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning [Sun Oct 06 01:08:38 JST 2019] 重大: Context initialization failed [Sun Oct 06 01:08:38 JST 2019] 重大: クラス [org.springframework.web.context.ContextLoaderListener] のリスナインスタンスにコンテキスト初期化イベントを送信中の例外です [Sun Oct 06 01:08:38 JST 2019] 重大: One or more listeners failed to start. Full details will be found in the appropriate container log file [Sun Oct 06 01:08:38 JST 2019] 重大: 以前のエラーのためにコンテキストの起動が失敗しました [/MySampleWebApp1] [Sun Oct 06 01:08:38 JST 2019] 情報: Closing Spring root WebApplicationContext [Sun Oct 06 01:08:38 JST 2019] 情報: Closing Root WebApplicationContext: startup date [Sun Oct 06 01:08:37 JST 2019]; root of context hierarchy [Sun Oct 06 01:08:38 JST 2019] 警告: Exception thrown from ApplicationListener handling ContextClosedEvent [Sun Oct 06 01:08:38 JST 2019] 警告: Exception thrown from LifecycleProcessor on context close [Sun Oct 06 01:08:38 JST 2019] 重大: クラス [org.springframework.web.context.ContextLoaderListener] のリスナインスタンスにコンテキスト破棄イベントを送信中の例外です [Sun Oct 06 01:08:38 JST 2019] 情報: Starting ProtocolHandler ["http-nio-8080"] [Sun Oct 06 01:08:38 JST 2019] 情報: Starting ProtocolHandler ["ajp-nio-8009"] [Sun Oct 06 01:08:38 JST 2019] 情報: Server startup in 11490 ms [Sun Oct 06 01:08:38 JST 2019]
servletのソース
package jp.co.spring.websample1; import java.io.IOException; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.context.support.SpringBeanAutowiringSupport; import jp.co.spring.websample2.MyBean; /** * Servlet implementation class MySampleServlet */ public class MySampleServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Autowired private MyBean mybean1; /** * @see Servlet#init(ServletConfig) */ public void init(ServletConfig config) throws ServletException { SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setAttribute("mybean", mybean1); request.getRequestDispatcher("/index.jsp").forward(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String message = request.getParameter("message"); mybean1.addMessage(message); response.sendRedirect("sample"); } }
javaクラス
package jp.co.spring.websample2; import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Component; @Component public class MyBean { private List<String> messages = new ArrayList<String>(); public MyBean() { messages.add("this is Bran sample"); } public void addMessage(String message) { messages.add(message); } public String getMessage(int n) { return messages.get(n); } public void setMessage(int n, String message) { messages.set(n, message); } public List<String> getMessages(){ return messages; } public void setMessages(List<String> messages) { this.messages = messages; } public String toString() { String result = "MyBean[\n"; for(String message : messages){ result += "\t'" + message + "\n"; } result += "]"; return result; } }
application-config.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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config /> <context:component-scan base-package="jp.co.spring.websample2" /> </beans>
コンポーネットスキャンを使わずに、XMLファイルに直接BEAN定義だけ書いた方法では成功しました。
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/10/06 02:38