• 《Spring入门基础》


    目录

    一、Spring的理解

    二、Spring模块之IOC

            2.1 ioc的特点

            2.2 案例试验

    三、依赖注入的三种方式

    四、Spring与Web的整合


    一、Spring的理解

    1.1 什么是Spring?

    Spring是一个开源框架,它由Rod Johnson创建。

    它是为了解决企业应用开发的复杂性而创建的。
       Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。
       然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。
       目的:解决企业应用开发的复杂性
       功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
       范围:任何Java应用
       简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。


    二、Spring模块之IOC

    Spring有很多模块,今天我们带来的是IOC模块:

           

            2.1 ioc的特点

    IOC的两大特点就是:控制反转以及依赖注入。

     控制反转就是: 将以前由程序员实例化对象/赋值的工作交给了Spring容器处理。

            2.2 案例试验

            UserAction类:

    1. package com.leaf.web;
    2. import java.util.List;
    3. import com.leaf.ioc.biz.UserBiz;
    4. import impl.UserBizImpl2;
    5. /**
    6. * 用户模块
    7. * @author Leaf
    8. *
    9. * 依赖注入的三种方式:
    10. * 1、set注入
    11. * 2、构造注入
    12. * 3、自动装配
    13. * default-autowire="byName"
    14. * byName:是通过Spring管理的bean对象的id进行查找,如果找不到则注入失败,反之成功;
    15. * byType:是通过Spring管理的bean对象接口的实现类进行查找,如果没有、或有2个以上,则注入失败,反之成功。
    16. *
    17. * 2022年8月5日 下午2:40:54
    18. */
    19. public class UserAction {
    20. //private UserBiz ub = new UserBizImpl2();
    21. private UserBiz ub;
    22. public UserBiz getUb() {
    23. return ub;
    24. }
    25. public void setUb(UserBiz ub) {
    26. this.ub = ub;
    27. }
    28. /**
    29. * set注入
    30. */
    31. private String name;
    32. private int age;
    33. private List hobby;
    34. public String getName() {
    35. return name;
    36. }
    37. public void setName(String name) {
    38. this.name = name;
    39. }
    40. public int getAge() {
    41. return age;
    42. }
    43. public void setAge(int age) {
    44. this.age = age;
    45. }
    46. public List getHobby() {
    47. return hobby;
    48. }
    49. public void setHobby(List hobby) {
    50. this.hobby = hobby;
    51. }
    52. //调用查询用户数据的方法
    53. public void list() {
    54. System.out.println(name);
    55. System.out.println(age);
    56. System.out.println(hobby);
    57. ub.list();
    58. }
    59. }

    OrderAction类: 

    1. package com.leaf.web;
    2. import java.util.List;
    3. import com.leaf.ioc.biz.UserBiz;
    4. import impl.UserBizImpl1;
    5. /**
    6. * 订单模块
    7. * @author Leaf
    8. *
    9. * 2022年8月5日 下午2:40:31
    10. */
    11. public class OrderAction {
    12. //private UserBiz ub = new UserBizImpl1();
    13. private UserBiz ub;
    14. public UserBiz getUb() {
    15. return ub;
    16. }
    17. public void setUb(UserBiz ub) {
    18. this.ub = ub;
    19. }
    20. /**
    21. * 构造注入
    22. */
    23. private String name;
    24. private int age;
    25. private List hobby;
    26. public OrderAction() {
    27. }
    28. public OrderAction(String name, int age, List hobby) {
    29. this.name = name;
    30. this.age = age;
    31. this.hobby = hobby;
    32. }
    33. //调用查询用户数据的方法
    34. public void list() {
    35. System.out.println(name);
    36. System.out.println(age);
    37. System.out.println(hobby);
    38. ub.list();
    39. }
    40. }

     还有我们的业务需求,接口类:UserBiz

    1. package com.leaf.ioc.biz;
    2. /**
    3. * 用户业务类
    4. * 需求:
    5. * 同时在用户模块、订单模块拿到所有用户数据
    6. *
    7. * 需求变更1:
    8. * 同时在用户模块、订单模块拿到所有用户数据,
    9. * 并且要求用户数据是已经通过年龄排序了的
    10. * 对应策略:修改userBiz中的list方法,添加排序功能
    11. *
    12. * 需求变更2:
    13. * 同时在用户模块、订单模块拿到所有用户数据,
    14. * 并且要求用户数据是已经通过注册时间点排序了的
    15. * 对应策略:修改userBiz中的list方法,添加排序功能,按照时间点排序
    16. *
    17. * ...
    18. * 总结:
    19. * 最原始:频繁修改业务层Biz层代码
    20. * 多实现:凡是涉及业务层调用的地方,都需要修改代码
    21. *
    22. * @author Leaf
    23. *
    24. * 2022年8月5日 下午2:31:03
    25. */
    26. public interface UserBiz {
    27. /**
    28. * 用户查询
    29. */
    30. public void list();
    31. }

    三、依赖注入的三种方式

    上面代码也就包括了我依赖注入的三种方式:分别在UserAction&&OrderActio里面; 

    接着我们只需要在Spring的配置文件spring-context.xml里面配置好就行啦:

    spring-context.xml

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xmlns:aop="http://www.springframework.org/schema/aop"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    6. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
    7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    8. <bean class="impl.UserBizImpl1" id="userBiz" scope="singleton">bean>
    9. <bean class="com.leaf.web.UserAction" id="userAction">
    10. <property name="ub" ref="userBiz">property>
    11. <property name="name" value="Leaf">property>
    12. <property name="age" value="18">property>
    13. <property name="hobby">
    14. <list>
    15. <value>篮球value>
    16. <value>摄影value>
    17. <value>画画value>
    18. list>
    19. property>
    20. bean>
    21. <bean class="com.leaf.web.OrderAction" id="orderAction">
    22. <property name="ub" ref="userBiz">property>
    23. <constructor-arg name="name" value="癫癫">constructor-arg>
    24. <constructor-arg name="age" value="19">constructor-arg>
    25. <constructor-arg name="hobby">
    26. <list>
    27. <value>篮球value>
    28. <value>摄影value>
    29. <value>画画value>
    30. list>
    31. constructor-arg>
    32. bean>
    33. beans>

    然后我们建立一个测试类:Demo1

    1. package com.leaf.ioc;
    2. import org.springframework.context.support.ClassPathXmlApplicationContext;
    3. import com.leaf.web.OrderAction;
    4. import com.leaf.web.UserAction;
    5. /**
    6. * 建模
    7. * 1、对spring框架的配置文件进行建模,
    8. * 建模之后,spring-context.xml中所有的javabean信息都会加载进spring容器的上下文中
    9. * 2、上下文中就包含了spring-context.xml 中的所有对象
    10. * @author Leaf
    11. *
    12. * IOC的特点:依赖注入 控制反转
    13. * 1、控制反转:指的是将创建对象的权利反转给spring容器来完成
    14. * 2、IOC的主要作用就是管理整个项目的javabean,依靠依赖注入、控制反转的特点进行管理
    15. *
    16. * 2022年8月5日 下午3:05:42
    17. */
    18. public class Demo1 {
    19. public static void main(String[] args) {
    20. //拿到spring的上下文
    21. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
    22. UserAction userAction = (UserAction) context.getBean("userAction");
    23. userAction.list();
    24. //拿到javabean对象
    25. OrderAction orderAction = (OrderAction) context.getBean("orderAction");
    26. //调用方法
    27. orderAction.list();
    28. }
    29. }

     然后运行测试:


    四、Spring与Web的整合 

    最后,我们还需要将Spring与Web端进行一个整合;

    原因和思路都写在下面这个测试类里面了:

    1. package com.leaf.ioc;
    2. import java.io.IOException;
    3. import javax.servlet.ServletException;
    4. import javax.servlet.ServletRequest;
    5. import javax.servlet.ServletResponse;
    6. import javax.servlet.annotation.WebServlet;
    7. import javax.servlet.http.HttpServlet;
    8. import org.springframework.context.support.ClassPathXmlApplicationContext;
    9. import com.leaf.web.UserAction;
    10. /**
    11. * Spring与Web容器的整合原理
    12. * Why:建模的过程是十分耗时的
    13. * How:
    14. * 解决问题:
    15. * 1、建模必不可少
    16. * 2、建模要保障只执行一次
    17. * 3、建模后期望在每一个servlet都能拿到Spring的上下文对象ClassPathXmlApplicationContext;
    18. * 怎么解决:
    19. * 1、监听器的初始化方法
    20. * 2、Spring上下文 要 存放在 tomcat上下文中
    21. *
    22. * @author Leaf
    23. *
    24. * 2022年8月5日 下午4:33:15
    25. */
    26. @WebServlet("/springDemo")
    27. public class DemoServlet extends HttpServlet {
    28. @Override
    29. public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
    30. //拿到spring的上下文
    31. //ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
    32. ClassPathXmlApplicationContext context = (ClassPathXmlApplicationContext) req.getServletContext().getAttribute("springContext");
    33. //拿到javabean对象
    34. UserAction userAction = (UserAction) context.getBean("userAction");
    35. //调用方法
    36. userAction.list();
    37. }
    38. }

    建立的servlet监听器:SpringLoadListener

    1. package com.leaf.ioc.listener;
    2. import javax.servlet.ServletContext;
    3. import javax.servlet.ServletContextEvent;
    4. import javax.servlet.ServletContextListener;
    5. import org.springframework.context.support.ClassPathXmlApplicationContext;
    6. import com.leaf.web.UserAction;
    7. /**
    8. * servlet上下文的监听器
    9. * @author Leaf
    10. *
    11. * 2022年8月5日 下午4:40:08
    12. */
    13. public class SpringLoadListener implements ServletContextListener{
    14. @Override
    15. public void contextInitialized(ServletContextEvent sce) {
    16. System.out.println("初始化...");
    17. //拿到servlet上下文
    18. ServletContext servletContext = sce.getServletContext();
    19. //拿到spring配置文件名
    20. String springConfigLocation = servletContext.getInitParameter("springConfigLocation");
    21. //拿到spring的上下文
    22. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
    23. //将Spring上下文保存到tomcat上下文中
    24. servletContext.setAttribute("springContext", context);
    25. }
    26. }

    最后我们运行测试一下:


    OK啦,我们下期再见!!! 

  • 相关阅读:
    PanTools v1.0.27 多网盘批量管理、遍历分享、转存、重命名、复制...
    UNIAPP day_00(8.29) 准备运行环境
    高等数学(第七版)同济大学 习题4-4(后14题) 个人解答
    C++学习——优先级队列模拟实现与仿函数初步认识
    PHP使用阿里云对象存储oss
    C打印内存16进制
    深度清洁,清洗小物件的好助手——希亦CG超声波清洗机体验
    Linux内存管理(四):内存架构和内存模型简述
    K8s的Service详解
    论文阅读(10) 基于吸力的推进是动物高效游泳的基础(2015)
  • 原文地址:https://blog.csdn.net/qq_63492318/article/details/126182642