目录
6.集合属性,List,Map,Set,Properties,数组
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 插件
----->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] |
-
在 Spring IOC 容器读取 Bean 配置创建 Bean 实例之前, 必须对它进行实例化. 只有在容器实例化后, 才可以从 IOC 容器里获取 Bean 实例并使用.
-
WebApplicationContext 是专门为 WEB 应用而准备的,它允许从相对于 WEB 根目录的路径中完成初始化工作
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 id="person" class="com.yirong.bean.Person">
<!-- 使用参数的类型匹配属性可以,区分重载的构造器 -->
<constructor-arg value="200" type="int"></constructor-arg>
<constructor-arg value="java开发" type="String"></constructor-arg>
public Person(int price, String book) {
-
组成应用程序的 Bean 经常需要相互协作以完成应用程序的功能. 要使 Bean 能够相互访问, 就必须在 Bean 配置文件中指定对 Bean 的引用
-
在 Bean 的配置文件中, 可以通过 <ref> 元素或 ref 属性为 Bean 的属性或构造器参数指定对 Bean 的引用.
<bean id="person" class="com.yirong.bean.Person">
<!-- 为car属性赋值 因为car属性是bean实体类的类型可以使用ref指向容器中的其他bean-->
<property name="car" ref="car"></property>
<bean id="car" class="com.yirong.bean.Car">
<property name="speed" value="200"></property>
<property name="price" value="200"></property>
5.内部 Bean与级联属性
创建时间: | 2018/7/18 14:12 |
更新时间: | 2018/7/18 14:13 |
作者: | [email protected] |
<bean id="person" class="com.yirong.bean.Person">
<bean id="car" class="com.yirong.bean.Car">
<property name="speed" value="200"></property>
<property name="price" value="200"></property>
<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>
<property name="car.speed" value="100"></property>
<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] |
第一步创建实体类:为实体类给上set,get,构造toString,方法
<?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 = (Person) x.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 = (Person) x.getBean("person");
//3.打印实体类结果
System.out.println(person2);
7.使用 p 命名空间
创建时间: | 2018/7/19 9:19 |
更新时间: | 2018/7/19 14:27 |
作者: | [email protected] |
private String name; //name属性定义
p:name :标示给当然 User实体类的属性name赋值
<bean id="user" class="com.yirong.bean.User" p:car-ref="car" p:name="张三"></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] |
-
使用基本的集合标签定义集合时, 不能将集合作为独立的 Bean 定义, 导致其他 Bean 无法引用该集合, 所以无法在不同 Bean 之间共享集合.
-
可以使用 util schema 里的集合标签定义独立的集合 Bean. 需要注意的是, 必须在 <beans> 根元素里添加 util schema 定义
-
如果想用util schema来定义集合,需要先导入util命名空间,具体方法是在配置文件编辑窗口找到Namespaces选项卡,然后加选util命令空间(打上勾表示选中)就可以了
public static final String str = "hello";
<!-- <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>
<util:properties id="properties">
<prop key="password">root</prop>
<prop key="url">jdbc:mysql://localhost:3306/test</prop>
<!-- <util:map> 创建一个java.util.map类型的bean,其中包含值或者引用 -->
<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: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] |
-
Spring IOC 容器可以自动装配 Bean. 需要做的仅仅是在 <bean> 的 autowire 属性里指定自动装配的模式
-
byType(根据类型自动装配): 若 IOC 容器中有多个与目标 Bean 类型一致的 Bean. 在这种情况下, Spring 将无法判定哪个 Bean 最合适该属性, 所以不能执行自动装配.
-
constructor(通过构造器自动装配): 当 Bean 中存在多个构造器时, 此种自动装配方式将会很复杂. 不推荐使用
<!-- 自动装配 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 id="car1" class="com.yirong.auto.Car"> <property name="speed" value="200"></property>
<!-- 使用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>
<!-- 使用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只能有一个,否则就会出现问题
10.继承 Bean
创建时间: | 2018/7/19 18:52 |
更新时间: | 2018/7/20 14:13 |
作者: | [email protected] |
-
Spring 允许继承 bean 的配置, 被继承的 bean 称为父 bean. 继承这个父 Bean 的 Bean 称为子 Bean
-
也可以忽略父 Bean 的 class 属性, 让子 Bean 指定自己的类, 而共享相同的属性配置. 但此时 abstract 必须设为 true
class="cn.edu.nuc.spring.beans.relation.Address"
p:city="Beijing" p:street="WuDaokou"/>
<bean id="address2" p:street="DaZhongsi" parent="address"/>
<!-- 抽象bean:bean的abstract属性为true的bean。这样的bean不能被ioc容器实例化,只能被用来继承配置。若某一个bean的class属性没有指定,则该bean必须是一个抽象bean -->
<bean id="address" p:city="Beijing" p:street="WuDaokou"
abstract="true"/>
<!-- bean配置的继承:使用bean的parent属性指定继承那个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);
}
<!-- 使用bean的scope属性来配置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="文件路径"/>
url=jdbc:mysql://localhost:3306/mysql
driverClass=com.mysql.jdbc.Driver
<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());
}