質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
XML

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

servlet

Servletとは、Webページの動的な生成やデータ処理などをサーバ上で実行するために、Javaで作成されたプログラムです。 ショッピングサイトやオンラインバンキングといった、動的なウェブサイトの構築に用いられています。

Q&A

解決済

1回答

1706閲覧

JAVA SPRINGFRAMEWORK ComponentScanを成功させたい

sakahyu

総合スコア9

XML

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

servlet

Servletとは、Webページの動的な生成やデータ処理などをサーバ上で実行するために、Javaで作成されたプログラムです。 ショッピングサイトやオンラインバンキングといった、動的なウェブサイトの構築に用いられています。

0グッド

1クリップ

投稿2019/10/05 16:14

編集2019/10/06 04:08

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定義だけ書いた方法では成功しました。

よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

情報がいくつか不足しているので、ある程度補足した状態で動作確認させた結果ですが

web.xml

xml

1<?xml version="1.0" encoding="UTF-8"?> 2<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://xmlns.jcp.org/xml/ns/javaee" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 5 id="WebApp_ID" version="3.1"> 6 <display-name>springMVC</display-name> 7 8 <listener> 9 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 10 </listener> 11 <servlet> 12 <servlet-name>dispatcherServlet</servlet-name> 13 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 14 <init-param> 15 <param-name>contextConfigLocation</param-name> 16 <param-value></param-value> 17 </init-param> 18 <load-on-startup>1</load-on-startup> 19 </servlet> 20 <servlet-mapping> 21 <servlet-name>dispatcherServlet</servlet-name> 22 <url-pattern>/</url-pattern> 23 </servlet-mapping> 24 25</web-app>

applicationContext.xml ( web.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>

MySampleServlet ( サープレットパスをアノテーションで指定 )

java

1package jp.co.spring.websample1; 2 3import java.io.IOException; 4 5import javax.servlet.ServletConfig; 6import javax.servlet.ServletException; 7import javax.servlet.annotation.WebServlet; 8import javax.servlet.http.HttpServlet; 9import javax.servlet.http.HttpServletRequest; 10import javax.servlet.http.HttpServletResponse; 11 12import org.springframework.beans.factory.annotation.Autowired; 13import org.springframework.web.context.support.SpringBeanAutowiringSupport; 14 15import jp.co.spring.websample2.MyBean; 16 17@WebServlet("/MySampleServlet") 18public class MySampleServlet extends HttpServlet { 19 @Autowired 20 private MyBean mybean1; 21 22 public void init(ServletConfig config) throws ServletException { 23 SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this); 24 } 25 26 protected void doGet(HttpServletRequest request, HttpServletResponse response) 27 throws ServletException, IOException { 28 request.setAttribute("mybean", mybean1); 29 request.getRequestDispatcher("/index.jsp").forward(request, response); 30 } 31 32 protected void doPost(HttpServletRequest request, HttpServletResponse response) 33 throws ServletException, IOException { 34 String message = request.getParameter("message"); 35 mybean1.addMessage(message); 36 response.sendRedirect("sample"); 37 } 38 39}

MyBeanは変更なし

利用したspring-webmvcのバージョン : 5.2.0
(関連して利用するjarファイル:spring-aop, spring-beans, spring-context, spring-core, spring-jcl, spring-expression, spring-web いずれも 5.2.0)

以上の設定やjarファイルで、Tomcat8.5で動作確認ができています。
web.xmlを確認された方が良いかと思います。

蛇足ですが…
ソースコードや設定ファイルは、コードブロック ( ``` ) で囲むと、回答者にも閲覧者にもやさしくなり、よりよい回答がつきやすくなります。

投稿2019/10/06 02:01

A-pZ

総合スコア12011

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sakahyu

2019/10/06 02:38

ありがとうございます。 今、外に出ているので後で確認して、動くかどうか確かめてみます。 ソースコードも、コードブロックで囲みました。 次の質問からコードブロックで囲むようにします。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問