java web.xml 拆分_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可以以功能对配置文件进行拆分

fbc77880a0bd8dc9cd7b404774d86927.png

创建多个配置文件:

43c85c0c7bdfb5bbe21a09e62935b9be.png

在web.xml中进行配置:

contextConfigLocation

classpath:applicationContext.xml,

classpath:applicationContext-Service.xml,

classpath:applicationContext-Dao.xml,

classpath:applicationContext-Controller.xml

或者:

contextConfigLocation

classpath:applicationContext.xml,

classpath:applicationContext-*.xml

3ee9118ab935350486c3ca864b46d26a.png

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:

applicationContext-Dao.xml:

applicationContext-Service.xml:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值