Spring笔记,作者:赵

目录

1.Spring的概念

2.Spring项目环境搭建

3.Spring 中的 Bean 配置

4.依赖注入的方法

5.内部 Bean与级联属性

6.集合属性,List,Map,Set,Properties,数组

7.使用 p 命名空间

8.utility scheme 定义集合

9.Bean 自动装配

10.继承 Bean

11.使用外部属性文件


1.Spring的概念

创建时间: 2018/7/17 20:49
更新时间: 2018/7/18 11:26
作者: [email protected]

 

    第一步:下载 springsource-tool-suite-3.4.0.RELEASE-e4.3.1-updatesite.zip 插件

 第二步 :安装插件

    打开Eclipse ------> Help -----> Install New Software ----->add ---->Archive ----->找到下载的插件打开勾选IDE结尾的选项 ----> Click Next  and then Finish

----->Restart eclipse when that is asked

    第三步:检查插件是否安装成功

            打开Eclipse ---->File ----> New -----> Other  搜索Spring如果有说明成功


2.Spring项目环境搭建

创建时间: 2018/7/18 8:53
更新时间: 2018/7/18 11:26
作者: [email protected]

第一步:创建一个Web项目,把spring的jar导入到lib目录下面

   下载地址: https://repo.spring.io/webapp/#/artifacts/browse/tree/General/libs-release-local/org/springframework/spring/4.3.2.RELEASE

        需要导入的jar:

                            

第二步:创建一个实体类如下:给上set,get,构造方法,toString方法(方便查看结果)

            

package com.yirong.bean;

public class Person {

     private int price;

     private String book;

}

第三步:创建spring的配置文件 在需要放配置文件的目录下鼠标右键----> New ----> Other ---->  搜索Spring ------> 找到Spring Bean Configuration File ----> Next ---- > 输入配置文件的名字后缀是.xml -----> Next 勾选beans -----> Next ----- > Finish----->文件创建成功

第四步:为第二步实体类配置bean(一个bean就是一个容器) 结构如下

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">

     <!-- 通过全类名方式配置Bean id:自己设定,最好为实体类的小写  class:实体类的全路径-->

     <bean id="name" class="com.yirong.bean.Person">

          <!-- name:填入的参数是实体类的属性 value:需要给属性赋的值 -->

          <property name="price" value="200"></property>

          <property name="book" value="编程设计思想"></property>

     </bean>

     

</beans>

第五步:写测试类,测试实体类属性是否赋值成功

    

package com.yirong.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.yirong.bean.Person;

public class Test {

     public static void main(String[] args) {

          

          //不使用spring,给Person实体类赋值,方法

          Person person = new Person();  //获取实体类对象

          person.setBook("编程思想设计");   //通过set方法给属性赋值

          person.setPrice(1000);        

          System.out.println(person);   //打印实体类结果

          

          //使用spring方法给属性赋值

          //1.创建spring的Ioc容器 spring容器xml文件名字

          ClassPathXmlApplicationContext x = new ClassPathXmlApplicationContext("applicationContext.xml");

          //2.从bean的容器中获取bean的实例

          Person person2 = (Person) x.getBean("person");

          //3.打印实体类结果

          System.out.println(person2);

     }

}


3.Spring 中的 Bean 配置

创建时间: 2018/7/18 10:01
更新时间: 2018/7/18 11:26
作者: [email protected]

        


4.依赖注入的方法

创建时间: 2018/7/18 10:25
更新时间: 2018/7/18 14:12
作者: [email protected]

第一种:属性注入,如下:

    <bean id="person" class="com.yirong.bean.Person">

          <!-- name:填入的参数是实体类的属性 value:需要给属性赋的值 -->

          <property name="price" value="200"></property>

          <property name="book" value="编程设计思想"></property>

     </bean>

第二种:构造器注入,如下:

    <!-- 使用构造方法注入属性 -->

     <bean id="person" class="com.yirong.bean.Person">

          <!-- 

                使用参数的类型匹配属性可以,区分重载的构造器 

                type:类型匹配

                index:下标匹配

                name:名字

          -->

          <!-- 使用参数的类型匹配属性可以,区分重载的构造器 -->

          <constructor-arg value="200" type="int"></constructor-arg>

          <constructor-arg value="java开发" type="String"></constructor-arg>

     </bean>

  public Person(int price, String book) {

          super();

          this.price = price;

          this.book = book;

     }

字面值:

引用其它 Bean ,给set,get方法,构造方法

下面的Car是一个实体类类型

第一个实体类

package com.yirong.bean;

public class Person {

     private int price;

     private String book;

     private Car car//引用下一个实体类

}

第二个实体类

package com.yirong.bean;

public class Car {

     private String speed;

     private int price

}

Spring配置文件配置

<!-- 配置Person实体类的bean -->

     <bean id="person" class="com.yirong.bean.Person">

     <!-- 为car属性赋值 因为car属性是bean实体类的类型可以使用ref指向容器中的其他bean-->

          <property name="car" ref="car"></property>

     </bean>

     <!-- 配置Car实体类的bean -->

     <bean id="car" class="com.yirong.bean.Car">

          <property name="speed" value="200"></property>

          <property name="price" value="200"></property>

     </bean>


5.内部 Bean与级联属性

创建时间: 2018/7/18 14:12
更新时间: 2018/7/18 14:13
作者: [email protected]

内部 Bean

     <!-- 配置Person实体类的bean -->

     <bean id="person" class="com.yirong.bean.Person">

          <!-- 使用内部bean给属性car赋值 -->

          <property name="car">

              <!-- 配置Car实体类的bean -->

              <bean id="car" class="com.yirong.bean.Car">

                   <property name="speed" value="200"></property>

                   <property name="price" value="200"></property>

              </bean>

          </property>

     </bean>

级联属性:Spring 支持级联属性的配置

<!-- 配置Person的bean -->

     <bean id="person" class="com.yirong.bean.Person">

          <constructor-arg value="100" index="0"></constructor-arg>

          <constructor-arg value="java开发" index="1"></constructor-arg>

          <constructor-arg ref="car"></constructor-arg>

          <!-- 给Car属性中speed的属性赋值 -->

          <property name="car.speed" value="100"></property>

     </bean>

     <bean id="car" class="com.yirong.bean.Car"></bean>


6.集合属性,List,Map,Set,Properties,数组

创建时间: 2018/7/18 14:13
更新时间: 2018/7/19 10:42
作者: [email protected]

集合属性,List

第一步创建实体类:为实体类给上set,get,构造toString,方法

package com.yirong.bean;

import java.util.List;

public class Person {

     //创建List集合属性,类型是String类型

     private List<String> list;

}

第二步编写spring的配置文件,编写bean,格式如下:

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">

     <bean name="person" class="com.yirong.bean.Person">

          <property name="list">

              <!-- 向集合中添加数据 类型是实体类规定的类型-->

              <list>

                   <value>java开发</value>

                   <value>编程设计思想</value>

              </list>

          </property>

     </bean>

</beans>

第三步写测试类,打印实体类集合属性是否有值

public static void main(String[] args) {

        //创建 Spring 的 IOC 容器

        ApplicationContext x = new ClassPathXmlApplicationContext("applicatContext2.xml");

        Person person = (Person) x.getBean("person"); //person是配置文件中bean的id

        System.out.println(person);

       

    }

集合属性,Map

        

  • java.util.Map 通过 <map> 标签定义, <map> 标签里可以使用多个 <entry> 作为子标签. 每个条目包含一个键和一个值.

  • 必须在 <key> 标签里定义键

  • 因为键和值的类型没有限制, 所以可以自由地为它们指定 <value>, <ref>, <bean>  <null> 元素.

  • 可以将 Map 的键和值作为 <entry> 的属性定义: 简单常量使用 key  value 来定义; Bean 引用通过 key-ref  value-ref 属性定义

第一步创建实体类:为实体类给上set,get,构造toString,方法

package com.yirong.bean;

import java.util.Map;

public class Person<T> {

     //创建map集合属性

     private Map<T, T> map;  //map是定义的属性名字,T代表泛型

}

第二步编写spring的配置文件,编写bean,格式如下:

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">

     <bean id="person" class="com.yirong.bean.Person">

          <property name="map"> <!-- map是实体类中的属性 -->

              <!-- 向集合中添加数据 类型是实体类规定的类型-->

              <map>

                   <entry key="键值对key1" value="value值1"></entry>

                   <entry key="键值对key2" value="value值2"></entry>

              </map>

          </property>

     </bean>

</beans>

第三步写测试类,打印实体类集合属性是否有值

public static void main(String[] args) {

          //使用spring方法给属性赋值

          //1.创建spring的Ioc容器 spring容器xml文件名字

          ClassPathXmlApplicationContext x = new ClassPathXmlApplicationContext("applicationContext.xml");

          //2.从bean的容器中获取bean的实例

          Person person2 = (Personx.getBean("person"); //person是配置文件中bean的id

          //3.打印实体类结果

          System.out.println(person2);

          

     }

集合属性,Set

第一步创建实体类:为实体类给上set,get,构造toString,方法

package com.yirong.bean;

import java.util.Set;

public class Person {

     //编写set属性

     private Set<String> set;

     

}

第二步编写spring的配置文件,编写bean,与list方法类似,格式如下:

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">

     <!-- 配置Person的bean -->

     <bean id="person" class="com.yirong.bean2.Person">

          <property name="set">

               <!-- 给set添加数据 -->

              <set>

                   <!-- 直接赋值 -->

                   <value>今天星期四</value>

                   <value>今天星期一</value>

                   <value>今天星期二</value>

                   <value>今天星期五</value>

              </set>

          </property>

     </bean>

</beans>

第三步写测试类,打印实体类集合属性是否有值,同list方法一样

集合属性,Properties

  • 使用 <props> 定义 java.util.Properties, 该标签使用多个 <prop> 作为子标签. 每个 <prop> 标签必须定义 key 属性.

第一步创建实体类:为实体类给上set,get,构造toString,方法

package com.yirong.bean;

import java.util.Properties;

import java.util.Set;

public class Person {

    //定义properties属性

   private Properties properties;

}

第二步编写spring的配置文件,编写bean,与list方法类似,格式如下:

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">

     <!-- 配置Person的bean -->

     <bean id="person" class="com.yirong.bean2.Person">

          <!-- 给properties属性赋值 -->

          <property name="properties">

              <!-- prop中必须定义key属性 -->

              <props>

                   <prop key="user">root</prop>

                   <prop key="password">root</prop>

                   <prop key="url">jdbc:mysql://127.0.0.1:3306/test</prop>

                   <prop key="driver">org.gjt.mm.mysql.Driver</prop>

              </props>

          </property>

     </bean>

     

</beans>

第三步写测试类,打印实体类集合属性是否有值,同上

集合属性,数组

第一步定义一个实体类,定义一个数组属性

private String[] arr;

第二步编写spring的配置文件,编写bean,与list方法类似,格式如下:

<!-- 配置Person的bean -->

     <bean id="person" class="com.yirong.bean2.Person">

          <!-- 给properties属性赋值 -->

          <property name="arr">

              <array>

                    <value>星期一</value>

                    <value>星期二</value>

                    <value>星期三</value>

                    <value>星期六</value>

              </array>

          </property>

     </bean>

第三步测试

//1.创建spring的Ioc容器 spring容器xml文件名字

          ClassPathXmlApplicationContext x = new ClassPathXmlApplicationContext("application.xml");

          //2.从bean的容器中获取bean的实例

          Person person2 = (Personx.getBean("person");

          //3.打印实体类结果

          System.out.println(person2);


7.使用 p 命名空间

创建时间: 2018/7/19 9:19
更新时间: 2018/7/19 14:27
作者: [email protected]

第一步编写实体类

实体类User 

package com.yirong.bean;

public class User {

     private Car car;     //Car属性定义

     private String name; //name属性定义   

}

//实体类Car

package com.yirong.bean;

public class Car {

     private String name;

}

      <!-- p引用属性赋值

          p:car-ref : 标示引用其他的bean

          p:name :标示给当然 User实体类的属性name赋值

      -->

     <bean id="user" class="com.yirong.bean.User" p:car-ref="car" p:name="张三"></bean>

     <!-- 配置Car的bean -->

     <bean id="car" class="com.yirong.bean.Car"></bean>


8.utility scheme 定义集合

创建时间: 2018/7/19 14:27
更新时间: 2018/7/19 18:25
作者: [email protected]

第一步实体类定义

//属性静态常量

public static final String str = "hello";

第二步配置文件配置bean

<!-- <util:constant> 引用某个类型public static 域,并将其暴露为bean -->

     <util:constant static-field="com.yirong.entry.User.str" id="str" />

<bean id="user" class="com.yirong.entry.User">

          <constructor-arg name="map" ref="map"></constructor-arg>

          <constructor-arg name="properties" ref="properties"></constructor-arg>

     </bean>

    <!-- 供其他bean引用 -->

     <util:properties id="properties">

          <prop key="user">root</prop>

          <prop key="password">root</prop>

          <prop key="url">jdbc:mysql://localhost:3306/test</prop>

     </util:properties>

     <!-- <util:map> 创建一个java.util.map类型的bean,其中包含值或者引用  -->

     <util:map id="map">

          <entry key="key1" value="1"></entry>

          <entry key="key2" value="2"></entry>

          <entry key="key3" value="3"></entry>

          <entry key="key4" value="4"></entry>

     </util:map>

     

     <!-- <util:constant> 引用某个类型public static 域,并将其暴露为bean

          <util:list> 创建一个java.util.list类型的bean,其中包含值或者引用

          <util:properties> 创建一个java.util.properties类型的bean

          <util:property-path> 引用一个属性(或内嵌属性),并将其暴露为bean

          <util:set> 创建一个java.util.set类型的bean,其中包含值或者引用 -->

     <util:constant static-field="com.yirong.entry.User.str" id="str" />

     


9.Bean 自动装配

创建时间: 2018/7/19 18:25
更新时间: 2018/7/19 20:25
作者: [email protected]

第一步创建实体类

private Car car;

private String name;

第二步配置spring文件

<!-- 自动装配 autodetect – 如果找到默认的构造函数,使用“自动装配用构造”; 否则,使用“按类型自动装配”

          1.constructor – 在构造函数参数的byType方式。

          2.byType – 按数据类型自动装配。如果一个bean的数据类型是用其它bean属性的数据类型,兼容并自动装配它。

          3.byName – 根据属性名称自动装配。如果一个bean的名称和其他bean属性的名称是一样的,将会自装配它。

          4.no – 缺省情况下,自动配置是通过“ref”属性手动设定 -->

     <!-- 使用构造自动装配 -->

     <bean id="constructor_user" class="com.yirong.auto.User" autowire="constructor">

          <constructor-arg name="name" value="小明"></constructor-arg>

     </bean>

          <bean id="car1" class="com.yirong.auto.Car"> <property name="speed" value="200"></property>

          </bean>

     <!-- 使用autowire="byType"类型自动装配 -->

     <bean id="aoto_user" class="com.yirong.auto.User" autowire="byType"></bean>

          <bean id="car1" class="com.yirong.auto.Car"> <property name="speed" value="200"></property>

          </bean>

     <!-- 使用autowire="byName"名字自动装配 -->

     <bean id="aoto_user2" class="com.yirong.auto.User" autowire="byName"></bean>

     <bean id="car" class="com.yirong.auto.Car">

          <property name="speed" value="200"></property>

     </bean>

需要注意,使用按类型装配bean方式配置的时候配置文件中该类型的bean只能有一个,否则就会出现问题


10.继承 Bean

创建时间: 2018/7/19 18:52
更新时间: 2018/7/20 14:13
作者: [email protected]

<bean id="address"

    class="cn.edu.nuc.spring.beans.relation.Address"

      p:city="Beijing" p:street="WuDaokou"/>

<bean id="address2" p:street="DaZhongsi" parent="address"/>

<!-- 抽象beanbeanabstract属性为truebean这样的bean不能被ioc容器实例化,只能被用来继承配置。某一个beanclass属性没有指定,则该bean必须是一个抽象bean -->

<bean id="address" p:city="Beijing" p:street="WuDaokou"

      abstract="true"/>

<!-- bean配置的继承:使用beanparent属性指定继承那个bean -->

<bean id="address2" p:street="DaZhongsi" parent="address"/>

Bean 的作用域

  •  Spring , 可以在 <bean> 元素的 scope 属性里设置 Bean 的作用域.

  • 默认情况下, Spring 只为每个在 IOC 容器里声明的 Bean 创建唯一一个实例, 整个 IOC 容器范围内都能共享该实例所有后续的 getBean() 调用和 Bean 引用都将返回这个唯一的 Bean 实例.该作用域被称为 singleton, 它是所有 Bean 的默认作用域.

<bean id="car" class="cn.edu.nuc.spring.beans.scope.Car">

  <property name="brand" value="Audi"/>

  <property name="price" value="300000"/>

</bean>

public static void main(String[] args) {

  ApplicationContext ctx new ClassPathXmlApplicationContext("beans-scope.xml");

//不管调用几次都只会实例化一次,因为bean的scope属性默认是singleton单利

  Car car1 = (Car) ctx.getBean("car");

  Car car2 = (Car) ctx.getBean("car");

  System.out.println(car1 == car2);

  }

<!-- 使用beanscope属性来配置bean的作用域

singleton:默认值。容器初始化时创建bean实例,整个容器的生命周期内只创建这一个bean。单例的

prototype:原型的。容器初始化时不创建bean实例,而在每次请求时都创建一个新的bean实例,并返回。

 -->

<bean id="car" class="cn.edu.nuc.spring.beans.scope.Car"

      scope="prototype">

  <property name="brand" value="Audi"/>

  <property name="price" value="300000"/>

</bean>


11.使用外部属性文件

创建时间: 2018/7/20 14:15
更新时间: 2018/7/20 15:24
作者: [email protected]

<!-- 导入属性文件 -->

     <context:property-placeholder location="文件路径"/>

使用外部属性文件配置数据源

第一步创建配置文件

user=root

password=root

url=jdbc:mysql://localhost:3306/mysql

driverClass=com.mysql.jdbc.Driver

第二步配置数据源bean

<!-- 导入属性文件 -->

     <context:property-placeholder location="db.properties"/>

     <!-- 使用外部类的属性,需要导入c3p0的jar -->

     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

          <property name="user" value="${user}"></property>

          <property name="password" value="${password}"></property>

          <property name="jdbcUrl" value="${url}"></property>

          <property name="driverClass" value="${driverClass}"></property>

     </bean>

public static void main(String[] args) throws SQLException {

          

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

          <!-- 返回数据源的对象 -->

          DataSource dataSource = (DataSource) a.getBean("dataSource");

          System.out.println(dataSource.getConnection());

     }

猜你喜欢

转载自blog.csdn.net/qq_34310949/article/details/81137746