zoukankan      html  css  js  c++  java
  • Spring之拆分spring配置文件,整合web项目

    1、java项目直接

    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext1.xml");

    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext2.xml");

    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext3.xml");即可进行初始化

    2、javaweb可以以功能对配置文件进行拆分

     创建多个配置文件:

     在web.xml中进行配置:

    <context-param>
      <!-- 监听器的父类ContextLoader中有一个属性contextConfigLocation -->
      	<param-name>contextConfigLocation</param-name>
      	<param-value>
      				classpath:applicationContext.xml,
      		  		classpath:applicationContext-Service.xml,
      		
      		  		classpath:applicationContext-Dao.xml,
      		
      		  		classpath:applicationContext-Controller.xml
      		
      	</param-value>
      </context-param>
    

      或者:

    <context-param>
      <!-- 监听器的父类ContextLoader中有一个属性contextConfigLocation -->
      	<param-name>contextConfigLocation</param-name>
      	<param-value>
      				classpath:applicationContext.xml,
      		  		classpath:applicationContext-*.xml
      		
      	</param-value>
      </context-param>
    

      

     IStudenDao.java:

    package org.ruangong.dao;
    
    public interface IStudentDao {
    	String querystudentById();
    }
    

     StudentDaoImpl.java:

    package org.ruangong.dao.impl;
    
    import org.ruangong.dao.IStudentDao;
    
    public class StudentDaoImpl implements IStudentDao{
    
    	@Override
    	public String querystudentById() {
    		// TODO Auto-generated method stub
    		System.out.println("张三23岁");
    		return "张三";
    	}
    
    }
    

      IStudentService.java:

    package org.ruangong.service;
    
    public interface IStudentService {
    	String querystudentById();
    }
    

      IStudentServiceImpl.java:

    package org.ruangong.service.impl;
    
    import org.ruangong.dao.IStudentDao;
    import org.ruangong.dao.impl.StudentDaoImpl;
    import org.ruangong.service.IStudentService;
    
    public class StudentServiceImpl implements IStudentService{
    	IStudentDao studentDao;
    	
    	public void setStudentDao(IStudentDao studentDao) {
    		this.studentDao = studentDao;
    	}
    
    	@Override
    	public String querystudentById() {
    		// TODO Auto-generated method stub
    		return studentDao.querystudentById();
    	}
    
    }
    

      QueryStudentByIdServlet3.java:

    package org.ruangong.servlet;

    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.ruangong.service.IStudentService;
    import org.ruangong.service.impl.StudentServiceImpl;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.web.bind.WebDataBinder;
    import org.springframework.web.bind.annotation.InitBinder;

    /**
    * Servlet implementation class QueryStudentByIdServlet3
    */
    public class QueryStudentByIdServlet3 extends HttpServlet {
    private static final long serialVersionUID = 1L;
    IStudentService studentService;//通过SpringIOC容器将service输入给servlet
    /**
    * Default constructor.
    */
    //Servlet初始化方法,在初始化是获取SpringIOC容器中的Bean对象
    @Override
    public void init() throws ServletException {
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    //在当前servlet容器中,通过getBean获取IOC容器的bean对象
    IStudentService studentService = (IStudentService)context.getBean("studentService");
    }


    public void setStudentService(IStudentService studentService) {
    this.studentService = studentService;
    }

    /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // IStudentService studentService = new StudentServiceImpl();
    String name = studentService.querystudentById();
    request.setAttribute("name",name);
    request.getRequestDispatcher("result.jsp").forward(request, response);
    }

    /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
    }

    }

    在当前servlet容器中,通过getBean获取IOC容器的bean对象,使用init()初始化servlet时拿到ioc容器的bean。

    @Override
    public void init() throws ServletException {
    // ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-Service.xml");
    ApplicationContext context =WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    //在当前servlet容器中,通过getBean获取IOC容器的bean对象
    studentService = (IStudentService)context.getBean("studentService");
    }

    applicationContext-Controller.xml:

    <bean id="studentServlet" class="org.ruangong.servlet.QueryStudentByIdServlet3">
    		<property name="studentService" ref="studentService"></property>
    	</bean>
    

    applicationContext-Dao.xml:

    <bean id="studentDao" class="org.ruangong.dao.impl.StudentDaoImpl"></bean>
    

    applicationContext-Service.xml:

    <bean id="studentService" class="org.ruangong.service.impl.StudentServiceImpl">
    		<property name="studentDao" ref="studentDao"></property>
    	</bean>
    

      

  • 相关阅读:
    IntelliJ IDEA java项目导入jar包,打jar包
    intelliJ idea运行新的test功能时,报错:class not found "....." empty test suite
    WEBSERVICE之JDK开发webservice
    Linux uname 命令 打印系统信息
    IDEA中 @override报错的解决方法
    使用VMware搭建3台一模一样的Linux虚拟机
    java使用httpclient封装post请求和get的请求
    在Centos中yum安装和卸载软件的使用方法
    JAVA面试题:69道Spring面试题和答案
    JAVA面试题:equals()方法和== 区别
  • 原文地址:https://www.cnblogs.com/jccjcc/p/14037546.html
Copyright © 2011-2022 走看看