#現状
『Spring Framework5-プログラミング入門-』掌田津耶乃 を用いてSpringを学習中なのですが、Bean構成クラスを作成し、web.xmlを変更してページを開いたところ404がでました。
ご教授よろしくお願いします。
###サーブレット
ackage mysample1; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.web.context.support.SpringBeanAutowiringSupport; @WebServlet("/sample") public class MySampleServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Autowired private MyBean mybean1; public void init() throws ServletException { super.init(); SpringBeanAutowiringSupport. processInjectionBasedOnCurrentContext(this); } protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { request.setAttribute("mybean",mybean1); request.getRequestDispatcher("/index.jsp").forward(request,response); } protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { String message = request.getParameter("message"); mybean1.addMessage(message); response.sendRedirect("sample"); } }
###Bean構成ファイル
package mysample1; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan(basePackages = "mysample1") public class MyBeanConfig { @Bean public MyBean myBean() { return new MyBean(); } }
###web.xml変更前
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/application-config.xml</param-value> </context-param>
###web.xml変更後
<context-param> <param-name>contextClass</param-name> <param-value> org.springframework.web.context.support.AnnotationConfigWebApplicationContext </param-value> </context-param> <context-param> <param-name>contextConfigLocation</param-name> <param-value>mysample1.MyBeanConfig</param-value> </context-param>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。