• Spring(ioc)


    目录

    一、Spring

    二、Spring中ioc的特点

    三、依赖注入的3种方式

     1.set注入

     2.构造注入

     3.自动装配

           3.3.1 byName

           3.3.2 byType

    四、Spring与web容器的整合


    一、Spring

     Spring是一个开源框架,它由Rod Johnson创建。它是为了解决企业应用开发的复杂性而创建的。
       Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。
       然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。

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

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

    范围:任何的Java应用

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

    二、Spring中ioc的特点

    先建立一个maven项目将以下代码加入到maven项目中的pom.xml中

    1. <properties>
    2. <spring.version>5.0.1.RELEASEspring.version>
    3. <javax.servlet.version>4.0.0javax.servlet.version>
    4. <junit.version>4.12junit.version>
    5. properties>

     

    加入改代码块的是为了将当前项目jar依赖版本定义在外部,目的在于所有jar包版本进行统一管理 

    再将 以下代码块加入其中,替换掉原来标签中的

    1. <dependency>
    2. <groupId>junitgroupId>
    3. <artifactId>junitartifactId>
    4. <version>3.8.1version>
    5. <scope>testscope>
    6. dependency>
    7. <dependency>
    8. <groupId>org.springframeworkgroupId>
    9. <artifactId>spring-contextartifactId>
    10. <version>${spring.version}version>
    11. dependency>
    12. <dependency>
    13. <groupId>org.springframeworkgroupId>
    14. <artifactId>spring-aspectsartifactId>
    15. <version>${spring.version}version>
    16. dependency>
    17. <dependency>
    18. <groupId>junitgroupId>
    19. <artifactId>junitartifactId>
    20. <version>${junit.version}version>
    21. <scope>testscope>
    22. dependency>
    23. <dependency>
    24. <groupId>javax.servletgroupId>
    25. <artifactId>javax.servlet-apiartifactId>
    26. <version>${javax.servlet.version}version>
    27. <scope>providedscope>
    28. dependency>

     

     后面的步骤和昨天配置的步骤一样,可以前往配置maven

     在开发的时候,往往有这样的场景,有这样一个接口,里面放置的方法为增删改查等;需求:同时在用户模块、订单模块拿到所有的用户数据,并且要求用户数据是已经通过年龄排序了的,然而根据之前所学的对应策略:修改userBiz(业务层)中的方法,添加排序功能。但如果需求又发生改变呢?就可以想到多实现的方法。

    可以总结一下:

    最原始:频繁修改业务层biz层代码,这样bug容易很多。

    多实现:凡是涉及到用户业务层调用的地方,都需要修改代码

    将以下xml文件加入到src/main/resources中

    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>

     UserAction:

    1. package com.mgy.web;
    2. import com.mgy.biz.UserBiz;
    3. import com.mgy.biz.impl.UserBizImpl1;
    4. public class UserAction {
    5. // private UserBiz userBiz=new UserBizImpl1();
    6. private UserBiz userBiz;
    7. public UserBiz getUserBiz() {
    8. return userBiz;
    9. }
    10. public void setUserBiz(UserBiz userBiz) {
    11. this.userBiz = userBiz;
    12. }
    13. public void list() {
    14. userBiz.list();
    15. }
    16. }

    该类和OrederAction一样的代码 

     将之前写的业务层和web加入到之中进行配置,切记要联网

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

    Demo1:

    1. package com.mgy.demo;
    2. import org.springframework.context.support.ClassPathXmlApplicationContext;
    3. import com.mgy.web.OrederAction;
    4. import com.mgy.web.UserAction;
    5. /**
    6. * 1.对Spring框架的配置晚间进行建模,建模之后,spring-context.xml中所有的Javabean信息都会加载进Spring容器的上下文中
    7. * 2.上下文中就包含spring-context.xml 所有对象
    8. * @author Administrator
    9. *
    10. * IOC的特点-什么叫控制反转
    11. * 指的是将创建对象的权力反转给Spring容器来完成
    12. *
    13. */
    14. public class Demo1 {
    15. public static void main(String[] args) {
    16. //建模
    17. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
    18. UserAction userAction = (UserAction) context.getBean("userAction");
    19. userAction.list();
    20. OrederAction orederAction=(OrederAction)context.getBean("orederAction");
    21. orederAction.list();
    22. }
    23. }

     效果:

     之后就只需要修改spring-context.xml文件的中的第一行就可以了。

    三、依赖注入的3种方式

    将其中一个action中的get和set方法注销后会产生报错

     错误:

     1.set注入

    将其中一个action类加入字段,并且给set和get方法

    1. package com.mgy.web;
    2. import java.util.List;
    3. import com.mgy.biz.UserBiz;
    4. import com.mgy.biz.impl.UserBizImpl1;
    5. /**
    6. * 依赖注入的3种方式:
    7. * 1.set注入
    8. * 2.构造注入
    9. * 3.自动装配
    10. * @author Administrator
    11. *
    12. */
    13. public class UserAction {
    14. // private UserBiz userBiz=new UserBizImpl1();
    15. private UserBiz userBiz;
    16. public UserBiz getUserBiz() {
    17. return userBiz;
    18. }
    19. public void setUserBiz(UserBiz userBiz) {
    20. this.userBiz = userBiz;
    21. }
    22. private String name;
    23. private int age;
    24. private List hobby;
    25. public String getName() {
    26. return name;
    27. }
    28. public void setName(String name) {
    29. this.name = name;
    30. }
    31. public int getAge() {
    32. return age;
    33. }
    34. public void setAge(int age) {
    35. this.age = age;
    36. }
    37. public List getHobby() {
    38. return hobby;
    39. }
    40. public void setHobby(List hobby) {
    41. this.hobby = hobby;
    42. }
    43. public void list() {
    44. System.out.println(name);
    45. System.out.println(age);
    46. System.out.println(hobby);
    47. userBiz.list();
    48. }
    49. }

    在去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.mgy.biz.impl.UserBizImpl1" id="userBiz">bean>
    9. <bean class="com.mgy.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>boyvalue>
    17. <value>rapvalue>
    18. list>
    19. property>
    20. bean>
    21. <bean class="com.mgy.web.OrederAction" id="orederAction">
    22. <property name="userBiz" ref="userBiz">property>
    23. bean>
    24. beans>

    property该标签没有list这个属性,所以要在该标签内部进行

    效果图:

     

     2.构造注入

    将字段给入构造方法,切记要给入无参的构造方法

    1. package com.mgy.web;
    2. import java.util.List;
    3. import com.mgy.biz.UserBiz;
    4. import com.mgy.biz.impl.UserBizImpl1;
    5. public class OrederAction {
    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 hobby;
    17. public OrederAction() {
    18. // TODO Auto-generated constructor stub
    19. }
    20. public OrederAction(String name, int age, List hobby) {
    21. super();
    22. this.name = name;
    23. this.age = age;
    24. this.hobby = hobby;
    25. }
    26. public void list() {
    27. System.out.println(name);
    28. System.out.println(age);
    29. System.out.println(hobby);
    30. userBiz.list();
    31. }
    32. }

    xml文件中的配置,构建注入配置和set注入的配置是不一样的

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

    效果:

     

     3.自动装配

           3.3.1 byName

            根据spring中的id进行查询的,如果找不到则出现报错,反之成功

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

    效果:

      

           3.3.2 byType

             根据spring中的bean对象接口实现类进行查询的,如果找不到或出现两个则出现报错,反之成功

            将上面的byName给成byType就可以了

    四、Spring与web容器的整合

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

    1. package com.mgy.biz.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.mgy.web.UserAction;
    7. /**
    8. * 1.实现监听器
    9. * 2.初始化方法contextInitialized
    10. * 3.拿到Tomcat上下文
    11. * 2.将Spring上下文保存到Tomcat上下文中
    12. *
    13. * @author Administrator
    14. *
    15. */
    16. public class SpringLoadListener implements ServletContextListener{
    17. @Override
    18. public void contextInitialized(ServletContextEvent sce) {
    19. System.out.println("初始化");
    20. ServletContext servletContext = sce.getServletContext();
    21. //自定义配置文件
    22. String aa = servletContext.getInitParameter("applicationContext.xml");
    23. System.out.println(aa+"///拿到了自定义配置文件");
    24. //拿到Spring上下文(包含所有javabean)
    25. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
    26. //将Spring上下文保存到Tomcat上下文中
    27. //applicationContext.xml
    28. servletContext.setAttribute("springContext", context);
    29. }
    30. }

    然后我们配置到web服务器上,顺便把定义项目名也配置了 

    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.xlb.biz.ioc.listener.SpringLoadListenerlistener-class>
    12. listener>
    13. web-app>

    然后我们写一个实现类web层DemoServlet.java 

    1. package com.mgy.demo;
    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.mgy.web.UserAction;
    10. /**
    11. * Spring与web容器的整合原理
    12. * why:建模的过程十分耗时的
    13. * how:
    14. * 解决问题:
    15. * 1.建模比不可少
    16. * 2.建模要保障只执行一次
    17. * 3.建模后期望在每一个servlet都能够拿到Spring的上下文对象ClassPathXmlApplicationContext
    18. *
    19. * how:
    20. * 1.监听器(里面初始化只执行一次)
    21. * 2.Spring的上下文要存放在Tomcat上下文中
    22. *
    23. * @author Administrator
    24. *
    25. */
    26. @WebServlet("/springDemo")
    27. public class DemoServlet extends HttpServlet{
    28. @Override
    29. public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
    30. //建模
    31. //1、spring-context.xml模型对象
    32. //ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
    33. ClassPathXmlApplicationContext context = (ClassPathXmlApplicationContext)req.getServletContext().getAttribute("springContext");
    34. //拿到UserAction
    35. UserAction userAction = (UserAction)context.getBean("userAction");
    36. //调用方法
    37. userAction.list();
    38. }
    39. }

  • 相关阅读:
    多输入多输出 | MATLAB实现CNN-BiLSTM-Attention卷积神经网络-双向长短期记忆网络结合SE注意力机制的多输入多输出预测
    STL 乱序算法
    Anaconda安装和配置 ---- 详细到家
    ReentrantLock工作原理
    pycharm终端pip安装模块成功但还是显示找不到 ModuleNotFoundError: No module named
    Linux_虚拟机常用目录汇总
    grafana table合并查询
    如何用好Nginx的gzip指令
    Java的堆内存和栈内存
    卷积神经网络
  • 原文地址:https://blog.csdn.net/m0_62604616/article/details/126164755