秋招日记

秋招日记

Day00 (2021.10.24)

Day01

Spring课程模块:

  1. Spring概念;

  2. IOC容器;

  3. Aop;

  4. JDBCTemplate;

  5. 事务管理;

  6. Spring5新特性

Spring概念:

  1. Spring是一个轻量级开源的JavaEE框架;
  2. Spring可以解决企业应用开发的复杂性;
  3. Spring有两个核心部分:IOC 和 Aop;
    • IOC:反转控制,把创建对象的过程交给Spring,Spring帮助我们new对象;
    • Aop:面向切面,在不修改源码的情况下,进行功能管理
  4. Spring框架特点:
    • 方便解耦、简化开发;
    • Aop编程支持;
    • Junit支持,方便测试;
    • 方便集成各种框架;
    • 方便进行事务操作;
    • 降低API开发难度
  5. 现在课程中,选用 Spring5.x

Spring入门案例:

下载Spring5:https://spring.io/projects/spring-framework#learn

Maven安装:

官网:https://maven.apache.org/download.cgi

配置:

新建系统变量 MAVEN_HOME 如变量值:E:\Maven\apache-maven-3.3.9

编辑系统变量 Path 添加变量值: ;%MAVEN_HOME%\bin

检测:命令行提示符窗口输入mvn --version查看: alt 导入Maven坐标:

<!-- https://mvnrepository.com/artifact/org.springframework/spring -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring</artifactId>
    <version>5.2.6.RELEASE</version>
    <type>pom</type>
</dependency>

创建普通类,在这个类中创建普通方法:

package com.sunwenhao.spring5;

/**
 * @author SunWenhao
 * @create 2021/10/25
 */
public class User {
    public void add(){
        System.out.println("输出......");
    }
}

创建配置文件,在配置文件配置创建的对象:

遇到问题:新版IDEA创建Spring项目时找不到没有Spring Config的XML文件

解决方案:https://blog.csdn.net/qq_43012792/article/details/107581105

重新导入Maven坐标:

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.6.RELEASE</version>
            <type>pom</type>
</dependency>

<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13.1</version>
    <scope>test</scope>
</dependency>

解决: alt

首先创建标签,有两个属性:id、class

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="user" class="com.sunwenhao.User"></bean>
</beans>

编写测试代码(仅限测试使用):

Junit模块@Test不能使用:将文件夹mark成Test

又出现问题:ApplicationContext无法导入:

怀疑没有配置好Tomcat环境:

IOC容器:

  • IOC底层原理;
  • IOC接口(BeanFactory);
  • IOC具体操作Bean管理(基于xml、基于注解方式)

IOC概念、原理:

控制反转:降低代码耦合度,把对象的创建和对象调用的过程交给Spring;底层原理:xml解析、工厂模式、反射;

//原始方案(耦合度高):
class UserDao{
    add(){
        ... ...
    }
}

class UserService{
    execute(){
        UserDao dao = new UserDao();
        dao.add;
    }
}
//*********************
//工厂模式:
class UserService{
    UserDao dao = UserFactory.getDao();
    dao.add();
    }
}

class UserDao{
    add(){
        ... ...
    }
}

class UserFactory{
    public static UserDao getDao(){
        return new UserDao();
    }
}
//*********************
//IOC方案:
//第一步:xml配置文件,配置创建的对象
<bean id= class=></bean>
//第二步:有service类和dao类 创建工厂类
class UserFactory{
    public static UserDao getDao(){
        String classValue = class属性值;//xml解析
        Class clazz = Class.forName(classValue);//通过反射创建对象
        return (UserDao)clazz.newInstanse();
    }
}

IOC(接口):

  • IOC思想要基于IOC容器完成,IOC容器底层就是对象工厂;

  • Spring提供IOC容器实现的两种方式(两个接口):

    • BeanFactory:IOC容器的基本实现,在开发中不经常使用,加载配置文件时不会创建对象,在获取对象、使用对象时才创建对象(什么时候用什么时候创建);
    • ApplicationContext:BeanFactory的子接口,提供更多的功能,载配置文件时就会创建对象,web开发中,在服务器启动时就把耗时耗资源的启动上
  • ApplicationContext 实现类:

    • FileSystemXmlApplicationContext :填写盘符路径 C:...;

    • ClassPathXmlApplicationContext:填写类路径 src...

IOC具体操作Bean管理(基于xml、基于注解方式):

  • 什么是Bean管理:class
    • Bean管理指的是两个操作:
    • Spring创建对象;
    • Spring注入属性
  • 进行Bean管理的两种实现方式:
    • 基于xml配置文件方式实现;
    • 基于注解方式实现

xml配置文件方式:

  1. 基于xml方式创建对象:

    <bean id="user" class="com.sunwenhao.spring5.User"></bean>
    

    在Spring配置文件中使用bean标签,填写属性就可以创建对象:

    bean标签常用属性:

    • id:唯一标识;
    • class:创建所在类的全路径:包 + 类名;
    • name:作用类似于id,id中不可用特殊符号,name可以用特殊符号;

    创建对象时默认执行无参构造方法;

  2. 基于xml方式注入属性:

    DI:依赖注入(注入属性),DI是IOC中的具体实现,需要在创建对象的基础上完成

    //使用set方法注入
    public class book {
        private String name;
    
        public void setName(String name) {
            this.name = name;
        }
    
        public static void main(String[] args) {
            book book1 = new book();
            book1.setName("BOOK");
        }
    }
    
    //使用有参构造器注入
    ... ...
    public book(String name) {
            this.name = name;
        }
    ... ...
    book book2 = new book("BOOK2");
    

    一、使用set方法注入:

    • 创建类,定义属性,设置set方法;
    • 在Spring配置文件配置对象创建,配置属性注入:bean标签、property标签;
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
        <bean id="person" class="com.sunwenhao.spring5.Person">
            <property name="author" value="author_sun"></property>
            <property name="name" value="name_sun"></property>
        </bean>
    </beans>
    

alt

二、使用有参构造器注入:

  • 创建有参构造器(constructor-arg标签):

  • public Person(String name, String author) {
          this.name = name;
          this.author = author;
      }
    
    ... ...
        
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="user" class="Person">
            <constructor-arg name="name" value="abc"></constructor-arg>
            <constructor-arg name="author" value="def"></constructor-arg>
        </bean>
    </beans>
    

xml注入其他类型属性:

  1. 字面量:
    1. null值;
    2. 属性值包含特殊符号;
<!--null值-->
<property name="author">
    <null></null>
</property>

<!--属性值包含特殊符号-->
<!--报错-->
<property name="author" value="<<南京>>"></property>

<!--转义符号-->
<property name="author" value="&lt;&dt<南京>>"></property>

<!--CDATA-->
<property name="author">
	<value>
    <![CDATA[<<南京>>]]>
    </value>
</property>
全部评论

相关推荐

点赞 评论 收藏
分享
在debug的柠檬精很迷人:好消息:现在HR挑三拣四 15年后 HR跪着求要简历 坏消息:被挑的是这代人,到时候求人的也是这代人。真好。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务