三大框架之Spring (初级学习 1)

来源:互联网 发布:医院行政后勤待遇知乎 编辑:程序博客网 时间:2024/06/11 09:13

1. applicationContext.xml 文件的配置

需要导的包:(4+1) 包可以在Spring 官网上面下载

这里写图片描述

能实现的功能:

  1. DI:Dependency Inject 依赖注入 创建对象时动态为属性赋值

  2. IOC 是 Spring 帮我们创建对象,DI 是 Spring 帮我们为属性赋值

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:p="http://www.springframework.org/schema/p"    xsi:schemaLocation="http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">    <!-- xml 也是使用标签对组织数据 -->    <!-- <bean></bean> -->    <!-- 如果标签中没有内容,也可以合并为一个 -->    <!-- <bean/> -->    <!-- p:owner-ref="user" -->    <bean id="car" class="com.zhiyou100.model.Car" autowire="byType" lazy-init="true" p:price="1000" p:name="#{user.name + ' 的车'}" >        <!-- DI:Dependency Inject 依赖注入 创建对象时动态为属性赋值 -->        <!-- 两种方式:setter 注入和 构造方法注入 -->        <!-- IOC 是 Spring 帮我们创建对象,DI 是 Spring 帮我们为属性赋值 -->        <!-- 创建对象的时候为属性赋值 -->        <!-- 1. 通过 setter 方法进行设置 -->        <!-- <property name="name" value="奥迪双钻"></property> -->        <!-- 2. 通过构造方法进行设置 -->        <!-- index 是参数的索引,从 0 开始 -->        <!-- index 和 type 可以省略 -->        <!-- <constructor-arg name="name" value="奥迪双钻" index="0" type="java.lang.String"></constructor-arg> -->        <!-- <constructor-arg name="price" value="200"></constructor-arg> -->        <!-- 4. 使用 SpEL 语言赋值:写一些简单的 java 代码 -->        <!-- <property name="name" value="#{'bmw'.toUpperCase()}"></property> -->        <!-- <property name="name" value="#{user.name + ' 的汽车'}"></property> -->        <!-- 5. p 标签,                                需要引入 xmlns:p="http://www.springframework.org/schema/p"                                在 bean 标签的属性中使用 p:属性名="" 进行赋值操作         -->         <!-- 对象赋值需要使用 ref,包括 p 标签也是 p:属性名-ref="bean 的 id" -->         <!-- <property name="owner" ref="user"></property> -->         <property name="colors">            <array>                <value>红色</value>                <value>黄色</value>                <value>蓝色</value>            </array>         </property>         <!--             <list></list>           ArrayList            <set></set>             HashSet            <map>                   HashMap                <entry>                    <key></key>                    <value></value>                </entry>            </map>            <props>                 Properties                <prop key="">value</prop>                <prop key="">value</prop>            </props>          -->    </bean>    <!-- 注册 User 类,其它类就可以通过 user 名字来获取对应的对象 -->    <bean id="user" name="user" class="com.zhiyou100.model.User"        scope="singleton" lazy-init="default">    </bean>    <!--        id:唯一标识,不能重复        name:设置名字,如果有 id 的话可以和别的重复,否则不能        class:类的全名:包名 + 类名        scope:设置创建模式             singleton:单例,只有一个对象,默认的,最常用的             prototype:多例,每次都是一个新的对象       init-method / destroy-method:在对象创建和销毁的时候调用的方法,                       只有在 singleton 下有效,并且方法不能有参数       lazy-init:默认 false 在 bean factories 创建的时候,创建注册过的所有单例类             bean factories 在第一使用 getBean 获取单例对象的时候创建                       设置为 true 就是懒加载,只有第一次使用的时候才会去创建对象       autowire:自动把注册过的 bean 关联到当前 bean 对应的属性上             byType:如果注册过的 bean 的类型和属性的类型一致,那么进行赋值操作                               自动把 user 复制给 car 的 own 属性,因为它们类型一致             byName:根据注册过的 bean 的名字自动注入属性    --></beans>

2. Test 获取:

在xml文件配置好后,我们没必要 User user = new User(); 这样来获取对象了
// Context:上下文,容器,运行环境
1 . // 读取配置文件内容,并把解析的结果存入 ApplicationContext 中

    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

2 . 获取对象:

 User user = context.getBean("user");/*  在 xml 配置文件中:         * scope 默认值是:singleton,表示采用单例模式,         *      user2 和 user 是同一个对象,名字相同         *          * 如果设置为 prototype,每次都返回一个新的对象,         *      user2 和 user 不相同,名字不一样         *          * 没有特殊情况,一般使用默认的单例模式         *          */