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>