Spring开发之静态工厂创建对象+动态工厂创建对象+构造方法创建对象+set方法注入+构造方法注入
2016-12-07 18:06
489 查看
第一步:导入Spring开发之必须用到的jar包
第二步:创建类
第三步:创建Spring开发之applicationContext.xml文件
第四步:编写代码和测试
第一步:导入Spring开发之必须用到的jar包
第二步:创建类
User类
School类
工厂类
第三步:创建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);
}
}
第二步:创建类
第三步:创建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);
}
}
相关文章推荐
- 84——工厂,静态方法,SpringIOC创建对象(6条)
- Spring创建对象的三种方法之二静态工厂创建(源码)
- 第1条:考虑用静态工厂方法代替构造器(第一章:创建和销毁对象)
- 优先用静态方法创建对象,考虑用静态工厂方法代替构造器
- Spring属性注入、构造方法注入、工厂注入以及注入参数(转)
- Spring 创建对象的问题,不同构造方法创建对象,使用工厂类类获取对象、单例或多例、延迟创建问题
- spring中通过静态工厂方法获取bean实例化对象
- Spring学习--静态工厂方法、实例工厂方法创建 Bean
- 类的加载、创建对象、静态变量static、构造函数、静态代码块、构造代码块、构造方法以及总结
- 创建和销毁对象之用静态工厂方法代替构造器
- 创建对象_工厂方法(Factory Method)模式 与 静态工厂方法
- 对象的动态和静态构造创建的区别
- 尚学堂js面向对象开发 (五)两种创建对象的方式(工厂方式,构造方法)
- spring创建bean的三种方式-通过构造器,通过静态工厂方法,通过实例工厂方法
- 第1条 创建和销毁对象——考虑用静态工厂方法代替构造器
- js(十五)——工厂和构造方法创建对象
- 对象的动态和静态构造创建的区别
- 关于java中子类,父类中,静态代码块: staic{},动态代码块:{},构造方法,类属性,对象属性等执行顺序做个总结:
- 学习effective java-1创建和销毁对象之静态工厂方法
- 创建和销毁对象--考虑用静态工厂方法代替构造函数