几个基本概念:
-
Spring框架:具有约束性的支撑我们实现某些功能的半成品项目
-
IOC(反转控制):是一种思想
-
DI(依赖注入):是一种具体实现,依赖:依赖于配置文件中的某个类,注入:给类中的属性赋值
1、工具:STS(spring tool suite)
2、导入需要的jar包:
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
commons-logging-1.1.1.jar
3、写一个Person类,里面有name、age属性,并有对应的get和set方法
public class Person {
private Integer age;
private String name;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person [age=" + age + ", name=" + name + "]";
}
}
4、写一个配置文件,applicationContext.xml(必须以.xml结尾)
<?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.xsd">
<!--
<bean>:定义spring所管理的一个对象
id:该对象的唯一标识(不能重复),在通过类型获取bean的过程中可以不设置
class:此对象所属类的全限定名
-->
<bean id="person1" class="com.ztt.spring.Person">
<property name="age" value="22"></property>
<property name="name" value="abc"></property>
</bean>
</beans>
5、定义一个测试类,testPerson.java
public class testPerson {
public static void main(String[] args){
//初始化容器
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过getBean()获取对象
//1、通过id获取
// Person person = (Person)ac.getBean("person1");
//2、通过类型获取对象,使用此方法时,要求spring所管理的此类型对象只能有一个
//Person person = ac.getBean(Person.class);
//3、同时通过id和类型获取对象(推荐)
Person person = ac.getBean("person1", Person.class);
System.out.println(person);
// ac.close(); //初始化容器时使用ClassPathXmlApplicationContext类,ac才可以关闭
}
}
ApplicationContext的主要实现类:
- ClassPathXmlApplicationContext:对应类路径下的xml格式的配置文件
- FileSystemXmlApplicationContext:对应文件系统中的xml格式的配置文件
依赖注入的两种方式:
1、属性注入(其实也就是通过set方法注入,该方法最常用)
<bean id="person1" class="com.ztt.spring.Person">
<property name="age" value="22"></property>
<property name="name" value="abc"></property>
</bean>
其中,“age”为Person类中属性名,即:set方法中对应的名字
2、构造器注入(此时,类中必须有无参和有参的构造方法)
<bean id="person1" class="com.ztt.spring.Person">
<constructor-arg value="22"></constructor-arg>
<constructor-arg value="abc"></constructor-arg>
</bean>
也可以指定属性值的先后顺序进行注入(index的值从0开始):
<bean id="person1" class="com.ztt.spring.Person">
<constructor-arg value="22" index="0"></constructor-arg>
<constructor-arg value="abc" index="1"></constructor-arg>
</bean>
但是上述方法容易出现问题,比如:类中有多个对应的构造方法时,赋值会产生错误,此时,最好给赋值的参数指定类型,使用type属性:
<bean id="person1" class="com.ztt.spring.Person">
<constructor-arg value="22" type="java.lang.Integer"></constructor-arg>
<constructor-arg value="abc" type="java.lang.String"></constructor-arg>
</bean>
处理特殊字符:给属性赋值时,若有特殊字符,比如:<>,使用<![CDATA[]]>来实现
<constructor-arg type="java.lang.String">
<value><![CDATA[<ShangHai>]]></value>
</constructor-arg>
P命名空间:
<bean id="person3" class="com.ztt.spring.Person" p:name="abc" p:age="18" p:age-ref="teacher"></bean>
引用其它Bean:
上述Person类中添加一个Car类,Car也是一个bean
public class Person {
private String name;
private int age;
private Car car;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
}
第一种方式:使用ref属性或者ref子节点
<bean id="person" class="com.wwj.springdemo.Person">
<property name="name" value="Tom"></property>
<property name="age" value="24"></property>
<property name="car" ref="car"></property>
</bean>
<bean id="car" class="com.wwj.springdemo.Car">
<constructor-arg value="Audi" type="java.lang.String"></constructor-arg>
<constructor-arg value="ShangHai" type="java.lang.String"/>
<constructor-arg value="300000" type="int"></constructor-arg>
</bean>
第二种方式:内部bean,此时内部bean只能在定义它的bean中使用,所以无需定义id,因为外界无法使用
<bean id="person" class="com.ztt.spring.Person">
<property name="name" value="ztt"></property>
<property name="age" value="18"></property>
<property name="car">
<bean class="com.ztt.spring.Car">
<constructor-arg value="Ford"></constructor-arg>
<constructor-arg value="Beijing"></constructor-arg>
<constructor-arg value="300000"></constructor-arg>
</bean>
</property>
</bean>
级联属性注入值:
<bean id="person" class="com.ztt.spring.Person">
<property name="name" value="ztt"></property>
<property name="age" value="18"></property>
<property name="car" ref="car"></property>
<property name="car.maxSpeed" value="250"/>
</bean>
通过级联属性进行注入之前需要先初始化
注入集合类型:
Person类如下:
public class Person {
private String name;
private int age;
private List<Car> cars;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<Car> getCars() {
return cars;
}
public void setCars(List<Car> cars) {
this.cars = cars;
}
}
配置文件如下:
<bean id="personList" class="com.ztt.spring.Person">
<property name="name" value="ztt"></property>
<property name="age" value="18"></property>
<property name="cars">
<list>
<ref bean="car1"/>
<ref bean="car2"/>
<ref bean="car3"/>
</list>
</property>
</bean>
数组、set、list类型的注入方式都一样,只有Map类型的不同,如下:
<bean id="person" class="com.ztt.spring.person">
<property name="name" value="ztt"></property>
<property name="age" value="18"></property>
<property name="cars">
<map>
<entry key="First" value-ref="car1"></entry>
<entry key="Second" value-ref="car2"></entry>
</map>
</property>
</bean>
properties注入:
properties类为map接口的实现类,经常用于JDBC的属性配置,定义一个DataSource数据源:
public class DataSource {
private Properties properties;
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public String toString() {
return "DataSource [properties=" + properties + "]";
}
}
配置文件如下:
<bean id="dataSource" class="com.ztt.spring.DataSource">
<property name="properties">
<props>
<prop key="user">root</prop>
<prop key="password">123456</prop>
<prop key="jdbcUrl">jdbc:mysql:///test</prop>
<prop key="driverClass">com.mysql.jdbc.Driver</prop>
</props>
</property>
</bean>