对象创建方式
方式一
applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--创建方式一-->
<bean name="user" class="com.company.bean.User"></bean>
</beans>
Demo
//创建方式一:空参构造方式
static void func1() {
//创建容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("com/company/create/applicationContext.xml");
//从容器取user对象
User user = (User) ac.getBean("user");
System.out.println(user);
}
方式二
applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--创建方式二-->
<bean name="user2" class="com.company.create.UserFactory" factory-method="createUser"></bean>
</beans>
UserFactory
package com.company.create;
import com.company.bean.User;
public class UserFactory {
public static User createUser() {
System.out.println("静态工厂创建User");
return new User();
}
}
Demo
//创建方式二:静态工厂(了解)
static void func2() {
//创建容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("com/company/create/applicationContext.xml");
//从容器取user对象
User user = (User) ac.getBean("user2");
System.out.println(user);
}
方式三
applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--创建方式三-->
<bean name="userFactory" class="com.company.create.UserFactory" ></bean>
<bean name="user3" factory-bean="userFactory" factory-method="createUser2"></bean>
</beans>
UserFactory
package com.company.create;
import com.company.bean.User;
public class UserFactory {
public User createUser2() {
System.out.println("实例工厂创建User");
return new User();
}
}
Demo
//创建方式三:实例工厂(了解)
static void func3() {
//创建容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("com/company/create/applicationContext.xml");
//从容器取user对象
User user = (User) ac.getBean("user3");
System.out.println(user);
}
scope属性
singleton :默认值,单例对象,被标识为单例的对象在spring容器中只会存在一个实例
prototype :多例原型,被标识为多例的对象,每次再获得才会创建,每次创建都是新的对象。整合struts2时,ActionBean必须配置为多例
生命周期方法
<!-- init-method: 配置一个方法作为生命周期的初始化方法
destroy-method: 配置一个方法作为生命周期的销毁方法 -->
<bean name="user" class="com.company.bean.User" scope="singleton" init-method="init" destroy-method="destory"></bean>
package com.company.bean;
public class User {
private String name;
private Integer age;
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 void init() {
}
public void destory(){
}
}
分模块配置
<import resource="com/company/create/applicationContext.xml"></import>
属性注入
方式一:set方法注入
Car
package com.company.bean;
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 + '\'' +
'}';
}
}
User
package com.company.bean;
public class User {
private String name;
private Integer age;
private Car car;
public Car getCar() {
return car;
}
public void setCar(Car car) {
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 void init() {
System.out.println("");
}
public void destory() {
System.out.println("");
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", car=" + car +
'}';
}
}
applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--set方式注入-->
<bean name="user" class="com.company.bean.User">
<!-- 值类型注入:为User对象中名为name的属性注入tom作为值-->
<property name="name" value="tom"></property>
<property name="age" value="20"></property>
<!--引用类型注入-->
<property name="car" ref="car"></property>
</bean>
<!--将Car对象配置到容器中-->
<bean name="car" class="com.company.bean.Car">
<property name="name" value="兰博基尼"></property>
<property name="color" value="黄色"></property>
</bean>
</beans>
Demo
static void func1() {
ApplicationContext ac = new ClassPathXmlApplicationContext("com/company/zhuru/applicationContext.xml");
User user = (User) ac.getBean("user");
System.out.println(user.toString());
}
方式二:构造函数注入
User 中加入构造函数
package com.company.bean;
public class User {
private String name;
private Integer age;
private Car car;
public User(String name, Integer age, Car car) {
this.name = name;
this.age = age;
this.car = car;
}
public User() {
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
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 void init() {
System.out.println("");
}
public void destory() {
System.out.println("");
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", car=" + car +
'}';
}
}
applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--构造函数注入-->
<bean name="user2" class="com.company.bean.User">
<!-- name: 构造函数的参数名
index: 构造函数的参数索引
type: 构造函数的参数类型
-->
<constructor-arg name="name" value="zhangsan" index="0" type="java.lang.String"></constructor-arg>
<constructor-arg name="age" value="18"></constructor-arg>
<constructor-arg name="car" ref="car"></constructor-arg>
</bean>
</beans>
方式三:p名称空间注入
applicationContext.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">
<!-- p名称空间注入
导入p名称空间 xmlns:p="http://www.springframework.org/schema/p"
-->
<bean name="user3" class="com.company.bean.User" p:name="jack" p:age="20" p:car-ref="car"></bean>
</beans>
方式四:spel注入
applicationContext.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">
<!--set方式注入-->
<bean name="user" class="com.company.bean.User">
<!-- 值类型注入:为User对象中名为name的属性注入tom作为值-->
<property name="name" value="tom"></property>
<property name="age" value="20"></property>
<!--引用类型注入-->
<property name="car" ref="car"></property>
</bean>
<!--将Car对象配置到容器中-->
<bean name="car" class="com.company.bean.Car">
<property name="name" value="兰博基尼"></property>
<property name="color" value="黄色"></property>
</bean>
<!-- spel注入-->
<bean name="user4" class="com.company.bean.User">
<!-- 将user的name和age的值赋值给user4 -->
<property name="name" value="#{user.name}"></property>
<property name="age" value="#{user.age}"></property>
<property name="car" ref="car"></property>
</bean>
</beans>
复杂类型注入(数组、List、Map、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:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!--复杂类型注入-->
<!--数组-->
<bean name="CollectionBean" class="com.company.zhuru.CollectionBean">
<!--数组中只有一个值-->
<!--<property name="arr" value="tom"></property>-->
<!--数组中有多个值-->
<property name="arr">
<array>
<value>tom</value>
<value>zhangsan</value>
<ref bean="user4"></ref>
</array>
</property>
<!--list中只有一个值-->
<!--<property name="list" value="tom"></property>-->
<!--list中有多个值-->
<property name="list">
<list>
<value>tom</value>
<value>zhangsan</value>
<ref bean="user4"></ref>
</list>
</property>
<!--map-->
<property name="map">
<map>
<entry key="url" value="www.baidu.com"></entry>
<entry key="user" value-ref="user4"></entry>
<entry key-ref="user3" value-ref="user2"></entry>
</map>
</property>
<!--properties-->
<property name="properties">
<props>
<prop key="class">com.company.zhuru.Demo</prop>
<prop key="username">zhangsan</prop>
</props>
</property>
</bean>
</beans>