1、Spring配置文件
singleton是单例的,prototype是多例的
2、Bean的属性注入
1、set方法注入
<?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">
<!--set方式注入:值类型和引用类型-->
<bean name="user" class="com.wisedu.springDemo.User">
<!--为User对象中名为name的属性注入tom作为值-->
<property name="name" value="tom"></property>
<property name="age" value="18"></property>
<!--为car属性注入下方配置的car对象-->
<property name="car" ref="car"></property>
</bean>
<!--必须将car对象配置到容器中-->
<bean name="car" class="com.wisedu.springDemo.Car">
<property name="name" value="兰博基尼"></property>
<property name="color" value="黄色"></property>
</bean>
</beans>
package com.wisedu.springDemo;
public class User {
private String name;
private Integer age;
private Car car;
public User() { System.out.println("User对象空构造方法"); }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Integer getAge() { return age; }
public void setAge(Integer age) { this.age = age; }
public Car getCar() { return car; }
public void setCar(Car car) { this.car = car; }
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", car=" + car +
'}';
}
}
package com.wisedu.springDemo;
public class Car {
private String name;
private String color;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getColor() { return color; }
public void setColor(String color) { this.color = color; }
@Override
public String toString() {
return "Car{" +
"name='" + name + '\'' +
", color='" + color + '\'' +
'}';
}
}
2、构造函数注入
<?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">
<!--set方式注入:值类型和引用类型-->
<bean name="user" class="com.wisedu.springDemo.User">
<!--为User对象中名为name的属性注入tom作为值-->
<property name="name" value="tom"></property>
<property name="age" value="18"></property>
<!--为car属性注入下方配置的car对象-->
<property name="car" ref="car"></property>
</bean>
<!-- 构造函数注入 -->
<bean name="user2" class="com.wisedu.springDemo.User">
<constructor-arg name="name" value="jerry"></constructor-arg>
<constructor-arg name="age" value="18"></constructor-arg>
<constructor-arg name="car" ref="car"></constructor-arg>
</bean>
<!--必须将car对象配置到容器中-->
<bean name="car" class="com.wisedu.springDemo.Car">
<property name="name" value="兰博基尼"></property>
<property name="color" value="黄色"></property>
</bean>
</beans>
package com.wisedu.springDemo;
public class User {
private String name;
private Integer age;
private Car car;
public User() {
System.out.println("User对象空构造方法");
}
//构造函数注入属性
public User(String name, Car car) {
System.out.println("User(String name,Integer age,Car car)");
this.name = name;
this.age = age;
this.car = car;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", car=" + car +
'}';
}
}
3、复杂类型注入
<!--复杂类型注入-->
<bean name="cb" class="com.wisedu.injection.CollectionBean">
<!--如果数组中只准备注入一个值(对象),直接使用value或ref即可。比如下面的配置直接往数组中注入了一个值tom-->
<!--<property name="arr" value="tom"></property>-->
<!--如果数组中准备注入多个元素-->
<property name="arr">
<array>
<value>tom</value>
<value>jerry</value>
<ref bean="user4"/>
</array>
</property>
<!--如果List中只准备注入一个值(对象),直接使用value或ref即可。-->
<!--<property name="list" value="jack"></property>-->
<!--如果list中准备注入多个元素-->
<property name="list">
<list>
<value>jack</value>
<value>rose</value>
<ref bean="user3"/>
</list>
</property>
<!--map类型注入-->
<property name="map">
<map>
<entry key="url" value="jdbc:mysql://crm"></entry>
<entry key="car" value-ref="car"></entry>
<entry key-ref="user3" value-ref="user4"></entry>
</map>
</property>
<!--Properties类型注入-->
<property name="prop">
<props>
<prop key="driverClass">com.jdbc.mysql.Driver</prop>
<prop key="userName">root</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
3、getBean()方法使用
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
//参数类型是字符串,适用于多个同样类型的,但是id不同的
//UserService userService = (UserService) app.getBean("userService");
//参数类型是class类型,适用于只有一个
UserService userService = app.getBean(UserService.class);
userService.save();
只能用字符串类型
4、spring容器加载properties文件
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--加载外部proprieties文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>