1. 程式人生 > 實用技巧 >Spring之拆分spring配置檔案,整合web專案

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>