• J2EE基础:Spring及ioc


    目录

    一、Spring的简介

    二、Spring中ioc的特点 

    1. 统一的路口,一处改变,多处改变,版本抽离出来的一个原因

    2.具体的jar包依赖 

     3.导入jdk的插件

    4.目录放出来,接着把我们的web2.3改为3.1

     5.把这个maven项目搭建起来

    6.更新

    ​编辑 

    二、Spring中ioc的特点 

     三、依赖注入的3中方式

    1.set 注入

    2.构造 注入 

    3.自动装配 

            byName 

            byType 

    4.总结  

     default-autowire="byType"

     四、Spring与web容器的整合 

    1.why:建模的过程是十分耗时的  

    2.解决问题 

    1.建模必不可少

    2.建模要保障只执行一次

    3.3.建模后期望在每一个Servlet都能够拿到Spring的上下文对象ClassPathXmlApplicationContext 

    3.how 

    1.监听器的初始化方法

     2.Spring的上下文要存放在Tomcat上下文中

    3.还需优化 

     五、总结

    1.Spring的作用

    2.IOC的作用

    3.依赖注入的方式

    4.自动装配

    5.Spring于web集合    


    一、Spring的简介

     Spring是一个开源框架,它由Rod Johnson创建。它是为了解决企业应用开发的复杂性而创建的。

       Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。

       然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。

       

       目的解决企业应用开发的复杂性

       功能使用基本的JavaBean代替EJB,并提供了更多的企业应用功能

       范围任何Java应用

       简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)容器框架

       1.1 中间层框架、万能胶

           struts2

           spring

           hibernate

       1.2 容器框架

             JavaBean 项目中的一个个类

             IOC和AOP

    二、Spring中ioc的特点 

    我们先新建一个maven项目

    1. 统一的路口,一处改变,多处改变,版本抽离出来的一个原因

    2.具体的jar包依赖 

     如果这个文件报红了,下面就不用写了,如果说当前目录解决不了,就切换一个目录,在解决不了,那么就把仓库清空

     3.导入jdk的插件

     

    4.目录放出来,接着把我们的web2.3改为3.1

    web.xml

    1. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    4. version="3.1">
    5. <display-name>Archetype Created Web Applicationdisplay-name>
    6. web-app>

     5.把这个maven项目搭建起来

    6.更新

    二、Spring中ioc的特点 

     

    通过上面这些我们目前就只需要修改UserBizImp12即可,项目越大修改的就越多,目前我们只是有两个

     总结:
         最原始:频繁修改业务层biz层代码    反复去修改原有的代码,本来没有bag就会修改成bug
         多实现:凡是涉及到用户业务层 调用的地址,都需要修改代码    修改的就是这个接口的实现类

    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. beans>

     把这个复制到如图所示:

    我们新建一个Demo1测试一下:

     

    我们现在运行的UserBizImpl2-----》运行结果如图所: 

     

    运行的UserBizImpl2-----》运行结果如图所:  

     三、依赖注入的3中方式

    目前我只是把OrderAction的set和get方法注释了如图所示 

     我们在运行一下就会报错

     建模就报错了,就是说没有set方法了,也就是说我们注入对象方法的话它是跟set方法是有关系的

    1.set 注入

    打印然后我们在去Demo1里面运行一下看看有没有如图所示:

    userBiz.java

    1. package com.jwj.biz;
    2. /**
    3. * 用户业务类
    4. * @author 蒋文娟
    5. *
    6. * @date 2022年8月6日 下午4:22:40
    7. */
    8. public interface UserBiz {
    9. void list();
    10. }

     

     代码块:UserAction.java

    1. package com.jwj.biz.web;
    2. import java.util.List;
    3. import com.jwj.biz.UserBiz;
    4. import com.jwj.biz.impl.UserBizImpl1;
    5. public class UserAction {
    6. // private UserBiz userBiz=new UserBizImpl1();
    7. private UserBiz userBiz;
    8. public UserBiz getUserBiz() {
    9. return userBiz;
    10. }
    11. public void setUserBiz(UserBiz userBiz) {
    12. this.userBiz = userBiz;
    13. }
    14. private String name;
    15. private int age;
    16. private List<String> hobby;
    17. public String getName() {
    18. return name;
    19. }
    20. public void setName(String name) {
    21. this.name = name;
    22. }
    23. public int getAge() {
    24. return age;
    25. }
    26. public void setAge(int age) {
    27. this.age = age;
    28. }
    29. public List<String> getHobby() {
    30. return hobby;
    31. }
    32. public void setHobby(List<String> hobby) {
    33. this.hobby = hobby;
    34. }
    35. public void list() {
    36. System.out.println(name);
    37. System.out.println(age);
    38. System.out.println(hobby);
    39. // 调用业务处理类
    40. userBiz.list();
    41. }
    42. }

    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="com.jwj.biz.impl.UserBizImpl1" id="userBiz">bean>
    9. <bean class="com.jwj.biz.web.UserAction" id="userAction">
    10. <property name="userBiz" ref="userBiz">property>
    11. <property name="name" value="zhangsan">property>
    12. <property name="age" value="22">property>
    13. <property name="hobby">
    14. <list>
    15. <value>篮球value>
    16. <value>rapvalue>
    17. <value>唱跳value>
    18. list>
    19. property>
    20. bean>
    21. <bean class="com.jwj.biz.web.OrderAction" id="orderAction">
    22. <property name="userBiz" ref="userBiz">property>
    23. bean>
    24. beans>

    2.构造 注入 

    通过构造器 ,我们在OrderAction里面设置如图所示:

    我们在去运行一下如图所示:

    OrderAction.java代码块:

    1. package com.jwj.biz.web;
    2. import java.util.List;
    3. import com.jwj.biz.UserBiz;
    4. import com.jwj.biz.impl.UserBizImpl1;
    5. public class OrderAction {
    6. //private UserBiz userBiz=new UserBizImpl1();
    7. private UserBiz userBiz;
    8. public UserBiz getUserBiz() {
    9. return userBiz;
    10. }
    11. public void setUserBiz(UserBiz userBiz) {
    12. this.userBiz = userBiz;
    13. }
    14. private String name;
    15. private int age;
    16. private List<String> hobby;
    17. public OrderAction(String name, int age, List<String> hobby) {
    18. super();
    19. this.name = name;
    20. this.age = age;
    21. this.hobby = hobby;
    22. }
    23. public OrderAction() {
    24. // TODO Auto-generated constructor stub
    25. }
    26. public void list() {
    27. System.out.println(name);
    28. System.out.println(age);
    29. System.out.println(hobby);
    30. // 调用业务处理类
    31. userBiz.list();
    32. }
    33. }

    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="com.jwj.biz.impl.UserBizImpl1" id="userBiz">bean>
    9. <bean class="com.jwj.biz.web.UserAction" id="userAction">
    10. <property name="userBiz" ref="userBiz">property>
    11. <property name="name" value="zhangsan">property>
    12. <property name="age" value="22">property>
    13. <property name="hobby">
    14. <list>
    15. <value>篮球value>
    16. <value>rapvalue>
    17. <value>唱跳value>
    18. list>
    19. property>
    20. bean>
    21. <bean class="com.jwj.biz.web.OrderAction" id="orderAction">
    22. <property name="userBiz" ref="userBiz">property>
    23. <constructor-arg name="name" value="zhangsan">constructor-arg>
    24. <constructor-arg name="age" value="23">constructor-arg>
    25. <constructor-arg name="hobby">
    26. <list>
    27. <value>篮球value>
    28. <value>rapvalue>
    29. <value>唱跳value>
    30. list>
    31. constructor-arg>
    32. bean>
    33. beans>

    3.自动装配 

    报空指针错误,因为没有输入所以才会报错 

            byName 

    改为这样如图所示: 

    我们在去运行一下: 就没有报错了

     我们把UserAction里面的userBiz改为userBiz1,全部改好了之后我们在去运行一下:就会报错如图所示:

     报的错就是我们刚刚改动的地址

            byType 

    当我们在换成byType的时候如图所示: 

     

    我们在去运行一下就不会报错了如图所示:

      

    4.总结  

    我们在换回来byName 

     

     当我们有改为byType的时候,它就会报错了如图所示:

     default-autowire="byType"

    byName: 是Spring管理的bean对象的ID进行查找 ,如果找不到则注入失败,反之成功。

    byType: 是Spring管理的bean对象接口实现类进行查找,如果没有或2个以上,则注入失败,反之成功

     四、Spring与web容器的整合 

     它是为了解决问题的,最终我们的开发肯定是开发web应用的web网页程序,最终就是要运行起来,要拿到整个工程里的任何一个bin对象的,那么我们当前是通过建模的方式去拿的,那么我们不可能把建模的那段代码放到每一个servlet里面去做

    1.why:建模的过程是十分耗时的  

    项目启动两三分钟少之又少,如果每一次请求都要加这个文件,给客户的响应速度是极其慢的

     如果要走上面那个建模就相当于睡觉了,也就相当于Thread.sleep(10000);

    2.解决问题 

    建模是耗时间的, 怎么才能让建模执行一次,并且同时能让每一个Servlet拿到context对象?

    1.建模必不可少

    2.建模要保障只执行一次

    3.3.建模后期望在每一个Servlet都能够拿到Spring的上下文对象ClassPathXmlApplicationContext 

    3.how 

    1.监听器的初始化方法

    Tomcat一启动监听器就打印,那么它会保障一定不会给你建模,并且它还保证执行一次

     2.Spring的上下文要存放在Tomcat上下文中

    SpringLoadListener.java代码块:

    1. package com.jwj.biz.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.jwj.biz.web.UserAction;
    7. public class SpringLoadListener implements ServletContextListener{
    8. @Override
    9. public void contextInitialized(ServletContextEvent sce) {
    10. // 拿到Spring的上下文中
    11. // 建模
    12. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
    13. // 将Spring的上下文保存到Tomcat上下文
    14. ServletContext servletContext = sce.getServletContext();
    15. servletContext.setAttribute("springContext", context);
    16. }
    17. }

     DemoServlet.java代码块:

    1. package com.jwj.biz.ioc.demo;
    2. import java.io.IOException;
    3. import javax.servlet.ServletException;
    4. import javax.servlet.http.HttpServlet;
    5. import javax.servlet.http.HttpServletRequest;
    6. import javax.servlet.http.HttpServletResponse;
    7. import org.springframework.context.support.ClassPathXmlApplicationContext;
    8. import com.jwj.biz.web.UserAction;
    9. /**
    10. * Spring于web容器的整合原理
    11. * why:建模的过程是十分耗时的
    12. * 解决问题:
    13. * 1.建模必不可少
    14. * 2.建模要保障只执行一次
    15. * 3.建模后期望在每一个Servlet都能够拿到Spring的上下文对象ClassPathXmlApplicationContext
    16. * how:
    17. * 1.监听器的初始化方法
    18. *  2.Spring的上下文要存放在Tomcat上下文中
    19. *
    20. * @author 蒋文娟
    21. *
    22. * @date 2022年8月5日 下午4:34:22
    23. */
    24. @WebServlet("/springDemo")
    25. public class DemoServlet extends HttpServlet{
    26. @Override
    27. protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    28. // Thread.sleep(1000);
    29. // 建模
    30. // ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
    31. ClassPathXmlApplicationContext context = (ClassPathXmlApplicationContext) req.getServletContext().getAttribute("springContext");
    32. UserAction userAction = (UserAction) context.getBean("userAction");
    33. userAction.list();
    34. }
    35. }

     我们在我们在上面的测试改动一下

    把我们之前注册的那两个行代码还原,然后我们有多写了一个UserBiz1那个给注释掉,把id改为原来的UserBiz

    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="com.jwj.biz.impl.UserBizImpl1" id="userBiz">bean>
    9. <bean class="com.jwj.biz.web.UserAction" id="userAction">
    10. <property name="userBiz" ref="userBiz">property>
    11. <property name="name" value="zhangsan">property>
    12. <property name="age" value="22">property>
    13. <property name="hobby">
    14. <list>
    15. <value>篮球value>
    16. <value>rapvalue>
    17. <value>唱跳value>
    18. list>
    19. property>
    20. bean>
    21. <bean class="com.jwj.biz.web.OrderAction" id="orderAction">
    22. <property name="userBiz" ref="userBiz">property>
    23. <constructor-arg name="name" value="zhangsan">constructor-arg>
    24. <constructor-arg name="age" value="23">constructor-arg>
    25. <constructor-arg name="hobby">
    26. <list>
    27. <value>篮球value>
    28. <value>rapvalue>
    29. <value>唱跳value>
    30. list>
    31. constructor-arg>
    32. bean>
    33. beans>

     UserAction.java代码块 把里面的UserBiz1改为UserBIz

    1. package com.jwj.biz.web;
    2. import java.util.List;
    3. import com.jwj.biz.UserBiz;
    4. import com.jwj.biz.impl.UserBizImpl1;
    5. /**
    6. * 依赖注入的3中方式
    7. * 1.set 注入
    8. * 2.构造 注入
    9. * 3.自动装配
    10. * byName
    11. * byType
    12. *
    13. * @author 蒋文娟
    14. *
    15. * @date 2022年8月5日 下午3:00:32
    16. */
    17. public class UserAction {
    18. // private UserBiz userBiz=new UserBizImpl1();
    19. private UserBiz userBiz;
    20. public UserBiz getUserBiz() {
    21. return userBiz;
    22. }
    23. public void setUserBiz(UserBiz userBiz) {
    24. this.userBiz = userBiz;
    25. }
    26. private String name;
    27. private int age;
    28. private List<String> hobby;
    29. public String getName() {
    30. return name;
    31. }
    32. public void setName(String name) {
    33. this.name = name;
    34. }
    35. public int getAge() {
    36. return age;
    37. }
    38. public void setAge(int age) {
    39. this.age = age;
    40. }
    41. public List<String> getHobby() {
    42. return hobby;
    43. }
    44. public void setHobby(List<String> hobby) {
    45. this.hobby = hobby;
    46. }
    47. public void list() {
    48. System.out.println(name);
    49. System.out.println(age);
    50. System.out.println(hobby);
    51. // 调用业务处理类
    52. userBiz.list();
    53. }
    54. }

    我们要记得去web.xml配置,没有配置的话会报500的错误

     web.xml代码块:

    1. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    4. version="3.1">
    5. <display-name>Archetype Created Web Applicationdisplay-name>
    6. <listener>
    7. <listener-class>com.jwj.biz.ioc.listener.SpringLoadListenerlistener-class>
    8. listener>
    9. web-app>

    然后我们在运行一下:在DemoServlet记得加上注解@WebServlet("/springDemo")     注意要用debug启动

     

    3.还需优化 

     在我们的SpringLoadListener框架里面一定叫ServletContextEvent不一定,我们的框架一定要支持可以更改框架名字的,如果一个项目要用十几个框架的就会重命名,所以一定要支持可配置

    我们就要在web.xml里面去配置一下

    1. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    4. version="3.1">
    5. <display-name>Archetype Created Web Applicationdisplay-name>
    6. <context-param>
    7. <param-name>springconfiglocationparam-name>
    8. <param-value>/applicationContext.xmlparam-value>
    9. context-param>
    10. <listener>
    11. <listener-class>com.jwj.biz.ioc.listener.SpringLoadListenerlistener-class>
    12. listener>
    13. web-app>

    SpringLoadListener.java

    在我们的上下文中拿到这个值

    1. package com.jwj.biz.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.jwj.biz.web.UserAction;
    7. public class SpringLoadListener implements ServletContextListener{
    8. @Override
    9. public void contextInitialized(ServletContextEvent sce) {
    10. System.out.println("初始化...");
    11. ServletContext servletContext = sce.getServletContext();
    12. // 拿到我们刚刚配置好的
    13. String springconfiglocation = servletContext.getInitParameter("springconfiglocation");
    14. // 拿到我们的值
    15. System.out.println(springconfiglocation+".....");
    16. // 拿到Spring的上下文中
    17. // 建模
    18. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
    19. // 将Spring的上下文保存到Tomcat上下文
    20. servletContext.setAttribute("springContext", context);
    21. }
    22. }

    我们在运行一下看看拿到了没有如图所示:拿到了

     五、总结

    前四个都是面试题 

    1.Spring的理解及作用

            容器框架        中间层框架        用来整合其他的第三方框架

            有两大核心组件        第一个是ioc        第二个是aop

    2.IOC的作用

            特点:依赖注入、控制反转

            控制反转:经创建对象的权利,有程序员手动new对象的权利转交给Spring容器完成

            优点:便于维护,拥抱变化

    3.依赖注入的方式

            set注入

            构造注入

            自动装配

    4.自动装配

            byName:根据bean 的 id在Spring的上下文中进行寻找,匹配上了会自动注入给属性

            byType:根据属性接口在Spring上下文中找对应的接口实现类,如果找到一个,就会自动注入;如果0或者2个以上就会报错

    5.Spring如何与web容器集成

    1.在监听器中初始化Spring上下文--建模

    2.拿到tomcat上下文,将Spring上下文存储到tomcat中

    3.在web.xml中配置监听器

    4.在测试的servlet中进行测试,在servlet看能否拿到Spring上下文 

  • 相关阅读:
    Union和union导致的数据不一致
    BUUCTF reverse wp 56 - 60
    mybatis、mybatisPlus
    【动态规划】139. 单词拆分、多重背包
    【c++】跟webrtc学容器:有序
    阿里云对象存储OSS的使用笔记
    [NCTF 2018]flask真香
    org.junit.runners.model.InvalidTestClassError:
    【RocketMQ】消息的拉取
    Java设计模式-抽象工厂模式
  • 原文地址:https://blog.csdn.net/weixin_67465673/article/details/126167436