Spring_mvc ioc/DI 控制反转与依赖注入

主要步骤:
1.   提交页面:一个查看商品的链接
     结果页面:商品列表,显示三件商品的具体信息
2.   实体类:商品名称,价格
3.   后台控制类Servlet和配置
4.   添加spring开发包
5.   实体类改进:Spring IoC配置

      控制类改进:Spring IoC程序



提交页面一个查看商品的链接:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'index.jsp' starting page
	
	
	    
	
	
	
  
  
  
    This is my JSP page. 
查看已购买的商品



实体类Product

注意:实体勒种必须要有无参构造函数,否则在applicationContext.xml配置文件中将会报错


No constructor with 0 arguments defined in class 'Spring_modle.Product' applicationContext.xml /Spring_Ioc_DI/src line 7 Spring Beans Problem

package Spring_modle;

public class Product {
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the pirce
	 */
	public double getPirce() {
		return pirce;
	}
	/**
	 * @param pirce the pirce to set
	 */
	public void setPirce(double pirce) {
		this.pirce = pirce;
	}
	private String name;
	private double pirce;
	/**
	 * 有参构造
	 * @param name
	 * @param pirce
	 */
	public Product(String name, double pirce) {
		super();
		this.name = name;
		this.pirce = pirce;
	}
	
	/**
	 * @author  tianjie
	 *Spring注入必须要有无参构造,否则 applicationContext.xml将会报错
	 * 无参构造
	 * */
	public Product() {
		
	}
}
后台控制类servlet

package Spring_Control;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import Spring_modle.Product;

public class domain extends HttpServlet {

	


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/**
		 * 静态获取
		 * */
//		ApplicationContext con=
//				new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
//		
//		Proudect p1,p2,p3;
//		p1=con.getBean("p1",Proudect.class);
//		p2=con.getBean("p2",Proudect.class);
//		p3=con.getBean("p3",Proudect.class);
//		
//		List list=new ArrayList();
//		list.add(p1);
//		list.add(p2);
//		list.add(p3);
		/**
		 * 动态获取
		 * */
		ApplicationContext con=
				new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
		String[] p=con.getBeanNamesForType(Product.class);
		List list=new ArrayList();
		Product proudect;
		for(int i=0;i

applicationContext.xml配置文件




	
	



	
	



	
	



结果页面 details.jsp

<%@page import="java.util.List"%>
<%@page import="Spring_modle.Proudect"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


	
<% out.write("输出商品:
"); List list=(List)session.getAttribute("list"); for(int i=0;i"); out.write("商品名称:"+list.get(i).getPirce()+"
"); } %>




你可能感兴趣的:(Spring_mvc ioc/DI 控制反转与依赖注入)