• Spring值IOC


    目录

    一、对Spring的理解

    1、关于Spring

    2、什么是spring,它能够做什么 

    二、Spring中ioc的特点及依赖注入的三种方式

    1、什么是控制反转(或依赖注入)

    2、依赖注入的三种方式

    三、Spring与web容器的整合 

    1、why

    2、how

    3、配置xml

    四,完整代码

    1、UserBiz

     2.UserBizImp1

    3.UserBizImpl2

    4、Demo1

    5、DemoServlet

    6、SpringLoadListener

    7、OrderAction

     8、UserAction

    9、spring-context.xml

    10、web.xml


    一、对Spring的理解

    1、关于Spring

    ①、架构方案层面

    ②、技术层面

            安全技术方面:Shiro springSecurity
    数据库层面:hibernate/mybatis SpringDataJpa
    消息中间件:activityMQ、RabbitMQ、kaffka spring…MQ

    2、什么是spring,它能够做什么 

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

    二、Spring中ioc的特点及依赖注入的三种方式


    1、什么是控制反转(或依赖注入)


    控制反转(IoC=Inversion of Control)IoC,用白话来讲,就是由容器控制程序之间的(依赖)关系,而非传统实现中,由程序代码直接操控。这也就是所谓“控制反转”的概念所在:(依赖)控制权由应用代码中转到了外部容器,控制权的转移,是所谓反转。
    IoC还有一个另外的名字:“依赖注入 (DI=Dependency Injection)” ,即由容器动态的将某种依赖关系注入到组件之中
    案例:实现Spring的IoC
    IOC/DI
    将以前由程序员实例化对象/赋值的工作交给了spring处理

    2、依赖注入的三种方式

    ①set 注入

    1. <bean class="com.zking.web.UserAction" id="userAction">
    2. <property name="userBiz" ref="userBiz">property>
    3. <property name="age" value="22">property>
    4. <property name="name" value="zhangsan">property>
    5. <property name="hobby">
    6. <list>
    7. <value>篮球value>
    8. <value>boyvalue>
    9. <value>篮球value>
    10. list>
    11. property>
    12. bean>

    ②构造注入

    1. <bean class="com.zking.web.OrderAction" id="orderAction">
    2. <property name="userBiz" ref="userBiz">property>
    3. <constructor-arg name="name" value="zhangsan">constructor-arg>
    4. <constructor-arg name="age" value="22">constructor-arg>
    5. <constructor-arg name="hobby">
    6. <list>
    7. <value>篮球value>
    8. <value>boyvalue>
    9. <value>篮球value>
    10. list>
    11. constructor-arg>
    12. bean>

    ③自动装配(spring3后出现的特性)

    default-autowire="byName"

    自动装配总结:byName:是通过spring管理的bean对象的ID进行查找,如果找不到则注入失败;反之成功
    byType:是通过spring管理的bean对象接口实现类进行查找;如果没有或两个以上,则注入失败,反之成功

    三、Spring与web容器的整合 

    1、why


    建模的过程是十分耗时的
    解决问题:
    1.建模必不可少
    2.建模要保障只执行一次
    3.建模后期望在每一个servlet都能够拿到spring的上下文对象ClassPathXmlApplicationContext

    2、how

    1.监听器的初始化方法
    2.spring的上下 要 存放 在tomcat上下文中

    3、配置xml
     

    1. <param-name>springConfigLocationparam-name>
    2. <param-value>/applicationContext.xmlparam-value>
    3. <listener>
    4. <listener-class>com.zking.ioc.listener.SpringLoadListenerlistener-class>
    5. listener>

    四,完整代码

    1、UserBiz

    /**
     * 用户业务类
     * 需求:
     *     同时在用户模块、订单模块拿到所有的用户数据
     *     
     * 需求变更1:
     *     同时在用户模块、订单模块拿到所有的用户数据,并且要求用户数据是已经通过年龄排序了的
     *     对应策略:修改userBiz中list方法,添加排序功能
     *     
     * 需求变更2:
     *     同时在用户模块、订单模块拿到所有的用户数据,并且要求用户数据是已经注册的时间点排序了的
     *     对应策略:修改userBiz中list方法,添加排序功能,按照时间点排序
     * 
     * ...
     *     总结:
     *         最原始:频繁修改业务层biz层代码
     *         多实现:凡是涉及到用户业务层 调用的地方,都需要修改代码
     * 
     * @author Administrator
     *
     */

    1. package com.zking.biz;
    2. public interface UserBiz {
    3. void list();
    4. }

     2.UserBizImp1

    1. package com.zking.biz.impl;
    2. import com.zking.biz.UserBiz;
    3. public class UserBizImpl1 implements UserBiz{
    4. @Override
    5. public void list() {
    6. System.out.println("查询用户数据,按照年龄排序。。。");
    7. }
    8. }

    3.UserBizImpl2

    1. package com.zking.biz.impl;
    2. import com.zking.biz.UserBiz;
    3. public class UserBizImpl2 implements UserBiz{
    4. @Override
    5. public void list() {
    6. System.out.println("查询用户数据,按照入职时间排序。。。");
    7. }
    8. }

    4、Demo1

    注解:
     1.对spring框架的配置文件进行建模,建模之后,spring-context.xml中所有的javabean信息都会
      加载进spring 容器的上下文
      2.上下文中就包含了spring-context.xml 所有对象


       IOC的特点-什么叫控制反转
       指的是将创建对象的权利反转给spring容器来完成

     

    1. package com.zking.ioc.demo;
    2. import org.springframework.context.support.ClassPathXmlApplicationContext;
    3. import com.zking.web.OrderAction;
    4. import com.zking.web.UserAction;
    5. public class Demo1 {
    6. @SuppressWarnings("resource")
    7. public static void main(String[] args) {
    8. ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("/spring-context.xml");
    9. UserAction userAction = (UserAction)context.getBean("userAction");
    10. userAction.list();
    11. OrderAction orderAction=(OrderAction)context.getBean("orderAction");
    12. orderAction.list();
    13. }
    14. }

    5、DemoServlet

    注解:
      spring与web容器的整合原理
      why:建模的过程是十分耗时的
        解决问题:
        1.建模必不可少
         2.建模要保障只执行一次
        3.建模后期望在每一个servlet都能够拿到spring的上下文对象ClassPathXmlApplicationContext
     how:
        1.监听器的初始化方法
         2.spring的上下 要 存放 在tomcat上下文中

    1. package com.zking.ioc.demo;
    2. import java.io.IOException;
    3. import javax.servlet.ServletException;
    4. import javax.servlet.annotation.WebServlet;
    5. import javax.servlet.http.HttpServlet;
    6. import javax.servlet.http.HttpServletRequest;
    7. import javax.servlet.http.HttpServletResponse;
    8. import org.springframework.context.support.ClassPathXmlApplicationContext;
    9. import com.zking.web.UserAction;
    10. @WebServlet("/springDemo")
    11. public class DemoServlet extends HttpServlet{
    12. @Override
    13. protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    14. // Thread.sleep(1000);
    15. // ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("/spring-context.xml");
    16. ClassPathXmlApplicationContext context=(ClassPathXmlApplicationContext) req.getServletContext().getAttribute("springContext");
    17. UserAction userAction = (UserAction)context.getBean("userAction");
    18. userAction.list();
    19. }
    20. }

    6、SpringLoadListener

    1. package com.zking.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.zking.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. String springConfigLocation = servletContext.getInitParameter("springConfigLocation");
    13. System.out.println(springConfigLocation+"....");
    14. // 拿到spring上下文
    15. ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("/spring-context.xml");
    16. // 将spring上下文保存到tomcat上下文中
    17. servletContext.setAttribute("springContext", context);
    18. }
    19. }

    7、OrderAction

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

     8、UserAction

    注解:


      依赖注入的三种方式
         1.set 注入
         2.构造注入
         3.自动装配(spring3后出现的特性)

     

            default-autowire="byName"
             byName:是通过spring管理的bean对象的ID进行查找,如果找不到则注入失败;反之成功
              byType:是通过spring管理的bean对象接口实现类进行查找;如果没有或两个以上,则注入失败,反之成功
     
           

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

    9、spring-context.xml

    1. "1.0" encoding="UTF-8"?>
    2. <beans default-autowire="byName" xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:aop="http://www.springframework.org/schema/aop"
    5. xmlns:context="http://www.springframework.org/schema/context"
    6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    7. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
    8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    9. <bean class="com.zking.biz.impl.UserBizImpl1" id="userBiz">bean>
    10. <bean class="com.zking.web.UserAction" id="userAction">
    11. <property name="userBiz" ref="userBiz">property>
    12. <property name="age" value="22">property>
    13. <property name="name" value="zhangsan">property>
    14. <property name="hobby">
    15. <list>
    16. <value>篮球value>
    17. <value>boyvalue>
    18. <value>篮球value>
    19. list>
    20. property>
    21. bean>
    22. <bean class="com.zking.web.OrderAction" id="orderAction">
    23. <property name="userBiz" ref="userBiz">property>
    24. <constructor-arg name="name" value="zhangsan">constructor-arg>
    25. <constructor-arg name="age" value="22">constructor-arg>
    26. <constructor-arg name="hobby">
    27. <list>
    28. <value>篮球value>
    29. <value>boyvalue>
    30. <value>篮球value>
    31. list>
    32. constructor-arg>
    33. bean>
    34. beans>

    10、web.xml

    1. "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.zking.ioc.listener.SpringLoadListenerlistener-class>
    12. listener>

  • 相关阅读:
    动态SQL第一部分(重点)
    做软件测试如何突破月薪20K?
    R语言ggplot2可视化密度图:按照分组可视化密度图、自定义配置geom_density函数中的alpha参数设置图像透明度(防止多条密度曲线互相遮挡)
    SpringCloud之Stream框架集成RocketMQ消息中间件
    MySQL数据库(一)
    Dump文件分析 - PDB强制匹配流程
    算法模型总结:字符串
    [消息队列 Kafka] Kafka 架构组件及其特性(二)Producer原理
    Java案例找素数(三种方法)
    “px、pt、ppi、dpi、dp、sp”全攻略
  • 原文地址:https://blog.csdn.net/weixin_66202611/article/details/126182588