第一步:导入Spring开发之必须用到的jar包
第二步:创建类第三步:创建Spring开发之applicationContext.xml文件
第四步:编写代码和测试
第一步:导入Spring开发之必须用到的jar包
第二步:创建类
- User类
package com.domain; public class User { private String name; private School school; public User() { super(); } public User(String name) { super(); this.name = name; } public User(String name, School school) { super(); this.name = name; this.school = school; } public String getName() { return name; } public void setName(String name) { this.name = name; } public School getSchool() { return school; } public void setSchool(School school) { this.school = school; } @Override public String toString() { return "User [name=" + name + ", school=" + school + "]"; } }
- School类
package com.domain; public class School { private String school; public String getSchool() { return school; } public void setSchool(String school) { this.school = school; } @Override public String toString() { return school; } }
- 工厂类
package com.factory; import com.domain.User; public class BeanFactory { //静态工厂创建 public static User getUser1(){ System.out.println("这是静态工厂创建的对象"); return new User(); } //动态工厂创建 public User getUser2(){ System.out.println("这是动态工厂创建的对象"); return new User(); } }
第三步:创建Spring开发之applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<!-- 静态工厂创建 -->
<bean name="user1" class="com.factory.BeanFactory" factory-method="getUser1"></bean>
<!-- 动态工厂创建 -->
<bean name="beanFactory" class="com.factory.BeanFactory"></bean>
<bean name="user2" factory-bean="beanFactory" factory-method="getUser2"></bean>
<!-- 构造方法创建+set方法注入 -->
<bean name="user3" class="com.domain.User">
<property name="name" value="Jack"></property>
<property name="school" ref="school"></property>
</bean>
<bean name="school" class="com.domain.School">
<property name="school" value="Harvard"></property>
</bean>
<!-- 构造方法创建+构造方法注入 -->
<bean name="user4" class="com.domain.User">
<constructor-arg name="name" value="Jack"></constructor-arg>
<constructor-arg name="school" ref="school"></constructor-arg>
</bean>
</beans>
package com.test;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.domain.User;
public class Demo {
/*
* 静态工厂创建的User
*/
@Test
public void fun1() {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) ac.getBean("user1");
user.setName("李明");
System.out.println(user);
}
/*
* 动态工厂创建的User
*/
@Test
public void fun2() {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) ac.getBean("user2");
user.setName("小华");
System.out.println(user);
}
/*
* 构造方法创建的User+set方法注入
*/
@Test
public void fun3() {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) ac.getBean("user3");
System.out.println(user);
}
/*
* 构造方法创建的User+构造方法注入
*/
@Test
public void fun4() {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) ac.getBean("user4");
System.out.println(user);
}
}