spring-----MVC结合IOC控制反转结合的流程

本文介绍了如何在Spring框架中利用IoC控制反转,通过配置文件管理外部资源,实现在Servlet、Service和DAO层的自动实例化。Spring通过XML配置或注解方式,让Spring容器负责对象的创建,简化了代码中对象获取过程。
摘要由CSDN通过智能技术生成

IOC控制反转

  • Spring反向控制应用程序所需要使用的外部资源
  • Spring控制的资源全部放置在Spring容器中,该容器称为IoC容器
  • 由spring进行对象的创建,而不是主动在代码通过new创建对象
  • 主要通过spring的配置文件,交给spring框架进行管理

流程

1.maven引入spring框架
 <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.1.9.RELEASE</version>
        </dependency>
2.创建servlet层,建立servlet类
package com.spring.servlet;

import com.spring.service.UserService;
import com.spring.service.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

@WebServlet("/user")
public class UserServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置字符编码
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        // ApplicationContext ,加载配置文件
        ApplicationContext appletContext = new ClassPathXmlApplicationContext("spring.xml");
        //获取资源
        UserService service = (UserServiceImpl)appletContext.getBean("bean1");
        String s = service.find();
        resp.getWriter().print(s);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}

3.创建service层,在里面编写UserService接口类及其实现类
package com.spring.service;

public interface UserService {
    String find();
}

package com.spring.service;

import com.spring.dao.UserDAO;
import com.spring.dao.UserDAOImpl;
import com.spring.dao.UserDAOImpl2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserServiceImpl implements UserService{
    @Override
    public String find() {
        // ApplicationContext ,加载配置文件
        ApplicationContext appletContext = new ClassPathXmlApplicationContext("spring.xml");
        //获取资源
        UserDAO user = (UserDAOImpl2)appletContext.getBean("bean2");
        //根据spring代理生成的对象,调用其方法
        String one = user.one();
        return one;
    }
}
4.建立DAO层,编写DAO.class和其实现类
package com.spring.dao;

public interface UserDAO {
    String one();
}
package com.spring.dao;

public class UserDAOImpl implements UserDAO{
    @Override
    public String one() {
        return "李白";
    }
}
package com.spring.dao;

public class UserDAOImpl2 implements UserDAO{
    @Override
    public String one() {
        return "李清照";
    }
}
5.创建spring的配置文件,并通过ApplicationContext加载
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="bean1" class="com.spring.service.UserServiceImpl"></bean>
    <bean id="bean2" class="com.spring.dao.UserDAOImpl2"></bean>
</beans>
6.在servlet调用Service,service调用DAO实现类
  • 结果
    在这里插入图片描述
总结
  • 要生成不同的对象,只需在spring.xml里面配置不同的<bean>,传入不同的类,根据其bean的id即可加载和生成不同的类对象
  • 通过ApplicationContext ,加载配置文件,再通过其对象,获取spring.xml配置文件的bean,获得其对应的类对象,从而可以调用其方法
 // ApplicationContext ,加载配置文件
        ApplicationContext appletContext = new ClassPathXmlApplicationContext("spring.xml");
         //获取资源
        UserService service = (UserServiceImpl)appletContext.getBean("bean1");
  • 要对properties文件的支取,只需在spring.xml添启context命名空间支持
xmlns:context="http://www.springframework.org/schema/context"
加载指定的properties文件
<context:property-placeholder location="classpath:filename.properties">
4.使用加载的数据
<property name="propertyName" value="${propertiesName}"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值