Spring day01

本文详细介绍了Spring框架的核心概念和优势,包括其轻量级特性、对比EJB的非侵入性,以及如何通过工厂模式实现解耦合。文章通过实例展示了如何使用Spring工厂创建对象,并探讨了Spring的单例和多例策略。此外,还深入讲解了Spring中的注入机制,包括Set注入、构造注入等不同方式,以及如何通过配置文件管理对象属性。最后,提到了Spring框架的模块组成和测试支持。
摘要由CSDN通过智能技术生成

Spring day01

1 Spring简介

Spring是一个轻量级的JavaEE应用框架,对比EJB(Enterprise Java Beans)技术是官方制定的重量级的JavaEE解决方案。EJB的重的表现:编码必须实现EJB内置的组件、必须部署在支持EJB的服务器中才能运行测试。EJB有很强的侵入性,造成开发的代码庞大而且无法通用。Spring框架非侵入性,在开发、测试、扩展方面都远超EJB,已经是JavaEE开发的事实标准。

Spring构建于众多优秀的设计模式之上,比如:工厂设计模式代理设计模式、模板方法设计模式、策略设计模式…。

设计模式:前人总结的,用于解决特定问题的优秀的方案。学习Spring,本质上就是在学这些设计模式解决了哪些问题?

2 工厂设计模式

工厂设计模式:使用工厂类创建对象,替换传统的new创建对象。

new创建对象的问题:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BS8WnWoR-1630934244188)(Spring day01.assets/image-20210610215905907.png)]

new模式下:类和类之间是强耦合,如果要扩展程序,必须要修改源码,违背开闭原则。

解决方案:工厂模式(解耦合)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3bw9XLtK-1630934244191)(Spring day01.assets/image-20210610222651381.png)]

  1. 创建一个工厂类BeanFactory+配置文件(applicationContext.properties)

    applicationContext.properties

    userService=com.baizhi.service.impl.UserServiceImpl2
    

    BeanFactory.java

    public class BeanFactory {
        private static Properties env = new Properties();
        static {
            InputStream in = BeanFactory.class.getResourceAsStream("/applicationContext.properties");
            try {
                env.load(in);
                in.close();
            }catch(IOException e){
                e.printStackTrace();
                throw new RuntimeException(e);
            }
    
        }
        //用于创建对象
        public static Object getBean(String id){
            //通过流读取配置文件,获取id和全类名的对应关系
            try {
                //通过反射创建对象
                Class c = Class.forName(env.getProperty(id));
                return c.newInstance();
            }catch(Exception e){
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
    }
    
  2. 将new替换为工厂创建对象

  3. 面向接口编程(定义接口类型的引用)

    UserService service = //面向接口编程
        (UserService) BeanFactory.getBean("userService");
    

总结:工厂模式特点:配置文件+反射+面向接口编程。好处:解耦合

3 第1个Spring程序

Spring框架最基本的使用:对象工厂(容器)。通过Spring工厂创建对象,解耦合从而提高程序的可维护性和扩展性。

3.1 Spring工厂的使用步骤

  1. 搭建开发环境

    1. 新建项目

    2. 导入依赖

      <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>5.2.8.RELEASE</version>
      </dependency>
      
    3. 导入配置文件

      applicationContext.xml 位置任意,建议resources根目录下

  2. 编码+配置

    配置:在applicationContext.xml中配置要创建哪些对象

    <bean id="唯一标识" class="全类名"/>
    

    编码:从Spring工厂中获取对象

    ApplicationContext 工厂类型(接口)

    ​ 实现类:

    ​ ClassPathXmlApplicationContext (非web环境)

    ​ XmlWebApplicationContext (Web环境)

    ​ 方法:

    ​ Object getBean(String id)

3.2 实战

配置:

<!-- new UserServiceImpl1() -->
    <!--<bean id="唯一标识" class="全类名"/>-->
    <bean id="userService" class="com.baizhi.service.impl.UserServiceImpl1"/>

编码:

//创建Spring工厂
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

//从工厂中获取对象
UserService userService = (UserService) context.getBean("userService");
userService.removeUser(1);

4 Spring框架的模块一览

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cmQTgWt4-1630934244193)(Spring day01.assets/image-20200330140458611.png)]

  1. Test:简化Spring项目的测试
  2. 核心容器:Spring作为工厂的实现
  3. AOP:面向切面编程,是面向对象编程有利的补充,可以非侵入的为代码添加额外功能。
  4. 辅助功能
  5. Data Access:提供数据库访问
  6. Web:Spring对JavaWeb开发的支持

Spring族系的框架种类丰富:SpringMVC、SpringData、SpringTask、SpringSecurity、SpringBoot、SpringCloud…。除此之外,Spring框架可以轻松的和其它框架整合,调度其它框架。

5 Spring工厂的实现原理

Spring工厂创建对象:读取配置文件(applicationContext.xml)中的信息,获取class属性配置的全类名,通过反射,默认调用类的无参构造方法创建对象。

bean标签的scope属性控制创建的对象是否是单例的。

​ scope 属性值

​ singleton(默认值)单例 Spring在工厂创建时随即创建对象

​ prototype 多例 Spring工厂默认不主动创建,直到getBean时创建

Spring工厂创建对象为什么默认单例?节省内存资源。

可以被用户共用(单例):dao 、service、 Controller、sqlSessionFactory

不可以被用户公用(多例):connection、sqlSession、ShoppingCart

6 注入(Injection)

为对象的属性赋值(注入值)。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oTt1MsNf-1630934244194)(Spring day01.assets/image-20210610223656078.png)]

6.1 Set注入

本质:通过无参构造方法创建对象后,调用属性的set方法赋值。

前提:类中必须有无参构造方法,属性必须提供set方法。

操作:在bean标签中,定义property子标签给属性赋值。

6.1.1 基本类型(包装类)+String
用法:
<property name="属性名">
    <value>基本类型或String数据</value>
</property>

<property name="属性名" value="基本类型或String数据"/>
示例:
<bean id="s1" class="com.baizhi.entity.Student" scope="prototype">
        <!--<property name="id">
            <value>1</value>
        </property>
        <property name="name">
            <value>xiaohei</value>
        </property>
        <property name="sex">
            <value>true</value>
        </property>
        <property name="score">
            <value>100.0</value>
        </property>-->
        <property name="id" value="1"/>
        <property name="name" value="xiaohei"/>
        <property name="sex" value="true"/>
        <property name="score" value="100.0"/>
    </bean>
6.1.2 自定义类型
用法:
<property name="属性名">
	<ref bean="另外一个Bean的id属性"/>
</property>

<property name="属性名" ref="另外一个Bean的id属性"/>

示例:
 <bean id="addr" class="com.baizhi.entity.Address" scope="prototype">
     <property name="city" value="郑州"/>
     <property name="street" value="文化路"/>
</bean>

<bean id="p" class="com.baizhi.entity.Person">
    <property name="id" value="1"/>
    <property name="name" value="xiaohei"/>
    <!--<property name="address">
            <ref bean="addr"/>
        </property>-->
    <property name="address" ref="addr"/>
</bean>
6.1.3 数组、List、Set类型
用法:
<property name="数组属性名">
	<array>
    	<value>基本类型或者String</value>
        <ref bean="另外一个bean的id"/>
        ...
    </array>
</property>

<property name="数组属性名">
	<list>
    	<value>基本类型或者String</value>
        <ref bean="另外一个bean的id"/>
        ...
    </list>
</property>

<property name="数组属性名">
	<set>
    	<value>基本类型或者String</value>
        <ref bean="另外一个bean的id"/>
        ...
    </set>
</property>

示例:
<property name="os">
    <array>
        <value>1</value>
        <value>xiaohei</value>
        <value>true</value>
        <ref bean="addr"/>
    </array>
</property>

<property name="list">
    <list>
        <value>1</value>
        <value>xiaohei</value>
        <value>true</value>
        <value>1</value>
        <ref bean="addr"/>
    </list>
</property>

<property name="set">
    <set>
        <value>1</value>
        <value>xiaohei</value>
        <value>true</value>
        <value>1</value>
        <ref bean="addr"/>
    </set>
</property>

注意:array、list、set 3个标签通用,但仍建议针对性使用。
6.1.4 Map类型
用法:
<property name="map属性名">
	<map>
    	<entry key="基本类型或String的key" value="基本类型或String的value"/>
        <entry key="基本类型或String的key" value-ref="bean的id"/>
    </map>
</property>

示例:
<property name="map">
    <map>
        <!-- 一个元素,是一个键值对-->
        <entry key="name" value="xiaohei"></entry>
        <entry key="address" value-ref="addr"></entry>
      <!--<entry key-ref="bean的id" value-ref="bean的id"/>-->
    </map>
</property>
6.1.5 Properties类型
用法:
<property name="properties属性名">
	<props>
    	<prop key="键字符串">值字符串</prop>
        ...
    </props>
</property>

示例:
<property name="properties">
    <props>
        <!-- 每一个键值对都是String -->
        <prop key="name">xiaohei</prop>
        <prop key="age">18</prop>
    </props>
</property>

6.2 构造注入

本质:在调用有参构造方法创建对象时,为属性赋值。

前提:类中必须有有参构造方法。

操作:在bean标签中添加constructor-arg子标签进行配置。

6.2.1基本使用
/*当形参数量不同时,直接根据constructor-arg的数量进行匹配,按照constructor-arg标签的顺序给参数赋值。*/

public class User implements Serializable {
    private Integer id;
    private String name;
    private String password;

    public User() {
    }

    public User(Integer id) {
        this.id = id;
    }
	 public User(String name) {
        this.name = name;
    }
    public User(Integer id, String name) {
        System.out.println("id = [" + id + "], name = [" + name + "]");
        this.id = id;
        this.name = name;
    }

    public User(Integer id, String name, String password) {
        System.out.println("id = [" + id + "], name = [" + name + "], password = [" + password + "]");
        this.id = id;
        this.name = name;
        this.password = password;
    }
    ...
}



 <!--通过构造注入为属性赋值-->
<bean id="u1" class="com.baizhi.entity.User" scope="prototype">
    <!-- 调用3个参数-->
    <constructor-arg value="1" />
    <constructor-arg value="xiaohei"/>
    <constructor-arg value="123456"/>
</bean>

<bean id="u2" class="com.baizhi.entity.User" scope="prototype">
    <!--调用2个参数-->
    <constructor-arg value="1"/>
    <constructor-arg value="xiaobai"/>
</bean>
6.2.2 type属性

type属性设置参数的类型,解决构造方法数量相同类型不同的匹配难题。

public User(Integer id) {
    System.out.println("id = [" + id + "]");
    this.id = id;
}
public User(String name) {
    System.out.println("name = [" + name + "]");
    this.name = name;
}
<bean id="u3" class="com.baizhi.entity.User" scope="prototype">
    <constructor-arg value="1" type="java.lang.Integer"/>
</bean>

注意:当构造参数多个时,一旦使用type属性,constructor-arg标签的顺序和构造方法参数的顺序不再匹配。

6.2.3 index属性

index属性用于设置constructor-arg标签的参数顺序,配合type属性一起解决形参数量相同、形参类型相同但顺序不同的匹配难题。

public User(Integer id, String name) {
    System.out.println("id = [" + id + "], name = [" + name + "]");
    this.id = id;
    this.name = name;
}

public User(String name,Integer id) {
    System.out.println("name = [" + name + "], id = [" + id + "]");
    this.id = id;
    this.name = name;
}
<bean id="u2" class="com.baizhi.entity.User" scope="prototype">
    <constructor-arg value="1" type="java.lang.Integer" index="0"/>
    <constructor-arg value="2" type="java.lang.String" index="1"/>
</bean>

], name = [" + name + “]”);
this.id = id;
this.name = name;
}

public User(String name,Integer id) {
System.out.println(“name = [” + name + “], id = [” + id + “]”);
this.id = id;
this.name = name;
}


```xml
<bean id="u2" class="com.baizhi.entity.User" scope="prototype">
    <constructor-arg value="1" type="java.lang.Integer" index="0"/>
    <constructor-arg value="2" type="java.lang.String" index="1"/>
</bean>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值