• Spring之IOC


    目录

    一:spring的介绍

    1.技术层面

    2.什么是spring

    3.spring能做什么

     二:在spring中定义和配置一个javabean

    1.set注入

     2.构造注入

    3.自动配置

    三:所有代码

    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.技术层面

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

    2.什么是spring

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

    3.spring能做什么

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

     二:在spring中定义和配置一个javabean

     3.1 id:在容器中查找Bean的id(唯一、且不能以/开头)
       3.2 class:bean的完整类名
       3.3 name:在容器中查找Bean的名字(唯一、允许以/开头、允许多个值,多个值之间用逗号或空格隔开)
       3.4 scope:(singleton|prototype)默认是singleton
         3.4.1 singleton(单例模式):在每个Spring IoC容器中一个bean定义对应一个对象实例
         3.4.2 prototype(原型模式/多例模式):一个bean定义对应多个对象实例
       3.4 abstract:将一个bean定义成抽象bean(抽象bean是不能实例化的),抽象类一定要定义成抽象bean,非抽象类也可以定义成抽象bean
       3.5 parent:指定一个父bean(必须要有继承关系才行)
       3.6 init-method:指定bean的初始化方法
       3.7 constructor-arg:使用有参数构造方法创建javaBean

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

     2.构造注入

    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>

    3.自动配置

    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.zking.biz.impl.UserBizImpl2" id="userBiz">bean>
    9. <bean class="com.zking.web.UserAction" id="userAction">
    10. <property name="userBiz" ref="userBiz">property>
    11. bean>
    12. <bean class="com.zking.web.OrderAction" id="orderAction">
    13. <property name="userBiz" ref="userBiz">property>
    14. bean>
    15. beans>

    三:所有代码

    1、UserBiz

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

    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. 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. /**
    6. * 1.对spring框架的配置文件进行建模,建模之后,spring-context.xml中所有的javabean信息都会
    7. * 加载进spring 容器的上下文
    8. * 2.上下文中就包含了spring-context.xml 所有对象
    9. *
    10. * @author Administrator
    11. *
    12. * IOC的特点-什么叫控制反转
    13. * 指的是将创建对象的权利反转给spring容器来完成
    14. *
    15. */
    16. public class Demo1 {
    17. @SuppressWarnings("resource")
    18. public static void main(String[] args) {
    19. ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("/spring-context.xml");
    20. UserAction userAction = (UserAction)context.getBean("userAction");
    21. userAction.list();
    22. OrderAction orderAction=(OrderAction)context.getBean("orderAction");
    23. orderAction.list();
    24. }
    25. }

    5、DemoServlet

    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. /**
    11. * spring与web容器的整合原理
    12. * why:建模的过程是十分耗时的
    13. * 解决问题:
    14. * 1.建模必不可少
    15. * 2.建模要保障只执行一次
    16. * 3.建模后期望在每一个servlet都能够拿到spring的上下文对象ClassPathXmlApplicationContext
    17. * how:
    18. * 1.监听器的初始化方法
    19. * 2.spring的上下 要 存放 在tomcat上下文中
    20. *
    21. * @author Administrator
    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. // ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("/spring-context.xml");
    30. ClassPathXmlApplicationContext context=(ClassPathXmlApplicationContext) req.getServletContext().getAttribute("springContext");
    31. UserAction userAction = (UserAction)context.getBean("userAction");
    32. userAction.list();
    33. }
    34. }

    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. 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. /**
    7. * 依赖注入的三种方式
    8. * 1.set 注入
    9. * 2.构造注入
    10. * 3.自动装配(spring3后出现的特性)
    11. * byName:是通过spring管理的bean对象的ID进行查找,如果找不到则注入失败;反之成功
    12. * byType:是通过spring管理的bean对象接口实现类进行查找;如果没有或两个以上,则注入失败,反之成功
    13. *
    14. * default-autowire="byName"
    15. * @author Administrator
    16. *
    17. */
    18. public class UserAction {
    19. // private UserBiz userBiz=new UserBizImpl1();
    20. private UserBiz userBiz;
    21. public UserBiz getUserBiz() {
    22. return userBiz;
    23. }
    24. public void setUserBiz(UserBiz userBiz) {
    25. this.userBiz = userBiz;
    26. }
    27. private String name;
    28. private int age;
    29. private List<String> hobby;
    30. public String getName() {
    31. return name;
    32. }
    33. public void setName(String name) {
    34. this.name = name;
    35. }
    36. public int getAge() {
    37. return age;
    38. }
    39. public void setAge(int age) {
    40. this.age = age;
    41. }
    42. public List<String> getHobby() {
    43. return hobby;
    44. }
    45. public void setHobby(List<String> hobby) {
    46. this.hobby = hobby;
    47. }
    48. public void list() {
    49. System.out.println(name);
    50. System.out.println(age);
    51. System.out.println(hobby);
    52. userBiz.list();
    53. }
    54. public UserAction() {
    55. // TODO Auto-generated constructor stub
    56. }
    57. }

    9、spring-context.xml

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

    10、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.zking.ioc.listener.SpringLoadListenerlistener-class>
    12. listener>
    13. web-app>

  • 相关阅读:
    crmeb从创建数据表到实现最基础增删改查的实现全过程【带附件】
    windows系统一键开启和关闭虚拟化
    【MicroPython ESP32】 触摸传感器使用示例
    缓存穿透、雪崩与击穿
    100天精通Python(数据分析篇)——第50天:numpy进阶
    Leetcode 49.字母异位词分组
    JDBC进行批量插入数据操作
    波士顿房价预测分析----以线性回归LinearRegression为例
    尚医通 (一) --------- 项目介绍
    11-2 集合之Collection接口
  • 原文地址:https://blog.csdn.net/m0_67864917/article/details/126181655