Spring开发之静态工厂创建对象+动态工厂创建对象+构造方法创建对象+set方法注入+构造方法注入


第一步:导入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);
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值