• Spring,搭建Spring环境


    控制反转:控制了对象的创建,反转:反转的是获取对象的方式,从自己创建对象变为由Spring工厂推送

    1. 搭建Spring环境,导入依赖

    • spring-aop:开发AOP特性时需要的JAR 
    • spring-beans:处理Bean的jar
    • spring-context:处理spring上下文的jar
    • spring-core.jar:spring核心jar
    • spring-expression:spring表达式

    还要导入:

    • mysql-connection-java
    • druid
    • mybatis
    • mybatis-spring
    • log4j

     2. application-Context.xml配置文件

    先导入模板

    1. "1.0" encoding="utf8" ?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:context="http://www.springframework.org/schema/context"
    4. xmlns:p="http://www.springframework.org/schema/p"
    5. xmlns:aop="http://www.springframework.org/schema/aop"
    6. xmlns:tx="http://www.springframework.org/schema/tx"
    7. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    8. xsi:schemaLocation="http://www.springframework.org/schema/beans
    9. http://www.springframework.org/schema/beans/spring-beans.xsd
    10. http://www.springframework.org/schema/p
    11. http://www.springframework.org/schema/p/spring-p.xsd
    12. http://www.springframework.org/schema/aop
    13. http://www.springframework.org/schema/aop/spring-aop.xsd
    14. http://www.springframework.org/schema/tx
    15. http://www.springframework.org/schema/tx/spring-tx.xsd
    16. http://www.springframework.org/schema/context
    17. http://www.springframework.org/schema/context/spring-context.xsd "
    18. >
    19. beans>

    3. 详解Spring原理

    新建Student类

    1. public class Student {
    2. private Integer stuno;
    3. private String stuname;
    4. private Integer stuage;
    5. public Integer getStuno() {
    6. return stuno;
    7. }
    8. public void setStuno(Integer stuno) {
    9. this.stuno = stuno;
    10. }
    11. public String getStuname() {
    12. return stuname;
    13. }
    14. public void setStuname(String stuname) {
    15. this.stuname = stuname == null ? null : stuname.trim();
    16. }
    17. public Integer getStuage() {
    18. return stuage;
    19. }
    20. public void setStuage(Integer stuage) {
    21. this.stuage = stuage;
    22. }
    23. @Override
    24. public String toString() {
    25. return "Student{" +
    26. "stuno=" + stuno +
    27. ", stuname='" + stuname + '\'' +
    28. ", stuage=" + stuage +
    29. '}';
    30. }
    31. }

    写个测试类test.java

    来看以前的做法:先新建对象,在通过set()属性赋值

    再来用IOC方法

                    1. 在application-context.xml文件中写bean,通过property标签设置属性

                    

                    2. 回到test测试类

    现在怎么创建student对象?

                    第一步:获取Spring上下文对象context

                    第二步:通过 context.getBean()获取刚刚在Spring配置文件application-context.xml文件中的bean(id="Student")

             

    对象的创建和对象的赋值全部交给IOC容器 

     下面的代码,执行从springIOC容器中获取一个id为student的bean对象,该对象的类型为对应bean标签class的值

    Student student = (Student)context.getBean("student");

    理解工厂的概念:

    创建一个ICourse接口:课程

    1. public interface ICourse {
    2. public void learn();
    3. }

    不同课程例如JavaCourse、MybatisCourse实现这个接口

    1. public class JavaCourse implements ICourse {
    2. @Override
    3. public void learn() {
    4. System.out.println("learning Java...");
    5. }
    6. }
    1. public class MybatisCourse implements ICourse {
    2. @Override
    3. public void learn() {
    4. System.out.println("learning Mybatis...");
    5. }
    6. }

    学生可以学习课程:为Student类添加方法 learnJava()、learnMybatis()

    1. public void learnJava(){
    2. ICourse iCourse = new JavaCourse();
    3. iCourse.learn();
    4. }
    5. public void learnMybatis(){
    6. ICourse iCourse = new MybatisCourse();
    7. iCourse.learn();
    8. }

    测试一下:

     上面是我们原始的开发方式

    每次需要课程都要在Student类里面写一个learnXxx(),

    如果要学习20个课程,就要20个方法,且每个方法里面都要创建一个课程的实现类对象

     这种方式创建对象new非常零散,造成后期维护较为麻烦

    创建工厂,由工厂统一的创建对象

     根据名称创建课程对象

    1. public class CourseFactory {
    2. public static ICourse getCourse(String name){
    3. if(name.equalsIgnoreCase("java")){
    4. return new JavaCourse();
    5. }else {
    6. return new MybatisCourse();
    7. }
    8. }
    9. }

    把Student中的learnJava()、learnMybatis()方法统一写在learn方法中,并给形参String name,并把name交给工厂CourseFactory来创建对象 

     测试:

     现在要学习不同课程只需要改参数值就可以了,通过简单的工厂,可以将创建课程new 集中起来操作,方便后期维护

    所以SpringIOC容器就是一个工厂,不需要我们去写CourseFactory,由SpringIOC容器为我们创建

    我们只需要通过调用

    为了更方便理解控制反转,我们也可以把这种方式叫做依赖注入

    spring配置文件中把Course的两个实现类设置bean

     如下代码:在需要创建JavaCourse对象的地方通过spring上下文context调用getBean()方法拿对象,不需要自己创建

    1. import org.springframework.context.ApplicationContext;
    2. import org.springframework.context.support.ClassPathXmlApplicationContext;
    3. public class CourseFactory {
    4. public static ICourse getCourse(String name) {
    5. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    6. if (name.equalsIgnoreCase("java")) {
    7. return (ICourse) context.getBean("javaCourse");
    8. } else {
    9. return (ICourse) context.getBean("mybatisCourse");
    10. }
    11. }
    12. }

    对比之前自己new对象

    1. public class CourseFactory {
    2. public static ICourse getCourse(String name){
    3. if(name.equalsIgnoreCase("java")){
    4. return new JavaCourse();
    5. }else {
    6. return new MybatisCourse();
    7. }
    8. }
    9. }

  • 相关阅读:
    LeetCode 1345. 跳跃游戏 IV (bfs+哈希表)
    网络编程
    软件测试之测试评估
    Grid布局
    全网最全JAVA面试八股文,终于整理完了
    win11cf烟雾头怎么调?
    搜索增长超80%,小红书多元内容发力!这些行业迎好机遇…
    测试应届生是去自研小公司好还是外包公司好?
    竞赛选题 深度学习疲劳检测 驾驶行为检测 - python opencv cnn
    Java 实现简易计算器
  • 原文地址:https://blog.csdn.net/m0_47010003/article/details/127288215