Spring02创建对象的3种方式,通过set方法对对象赋值

一、Spring创建对象的3种方式

  1. 通过构造方法创建
    无参构造创建:默认情况
    有参构造创建:需要明确配置
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    工厂设计模式:帮助创建类对象,一个工厂可以创建多种对象。
  2. 实例工厂
    (1) 需要先创建工厂,才能再创建对象。
public class PeopleFactory {
	public static People newInstance(){
		return new People(1,"测试");
	}
}

(2) 在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">
    <!-- id表示获取到对象标识      class 创建哪个类的对象 -->
    <bean id="peo" class="com.bjsxt.pojo.People">
    	<!-- ref引用另一个bean   value 基本数据类型或String等 -->
    	<constructor-arg index="0" name="id" type="int" value="123"></constructor-arg>
    	<constructor-arg index="1" name="name" type="java.lang.String" value="张三"></constructor-arg>
    </bean>
    
    <!-- 配置工厂对象和需要创建的对象 -->
    <bean id="factory" class="com.bjsxt.pojo.PeopleFactory"></bean>
    <bean id="peo1" factory-bean="factory" factory-method="newInstance"></bean>
    
</beans>

(3) 编写测试方法

public class Test {
	public static void main(String[] args) {
//		People peo = new People();
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		People people = ac.getBean("peo2",People.class);
		System.out.println(people);
	/**
		 * 实例工厂测试代码
		 * 具体如何实例化工厂和本个知识点无关系
		 */
//		PeopleFactory factory = new PeopleFactory();
//		People people = factory.newInstance();
	}
}

  1. 静态工厂
    不用创建工厂,可以快速创建对象。
    (1) 编写一个静态(在方法上填写static)
public class PeopleFactory {
	public static People newInstance(){
		return new People(1,"测试");
	}
}

(2) 在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">
    <!-- id表示获取到对象标识   class 创建哪个类的对象 -->
    <bean id="peo" class="com.bjsxt.pojo.People">
    	<!-- ref引用另一个bean   value 基本数据类型或String等 -->
    	<constructor-arg index="0" name="id" type="int" value="123"></constructor-arg>
    	<constructor-arg index="1" name="name" type="java.lang.String" value="张三"></constructor-arg>
    </bean>
 
    <bean id="peo2" class="com.bjsxt.pojo.PeopleFactory" factory-method="newInstance"></bean>
</beans>

二、对对象属性赋值(通过set方法)

通过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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="peo" class="com.bjsxt.pojo.People">
    	<property name="id">
    		<value>456</value>
    	</property>
    	<property name="name">
    		<value>zhangsan</value>
    	</property>
    	<property name="sets">
    		<set>
    			<value>1</value>
    			<value>2</value>
    			<value>3</value>
    			<value>4</value>
    		</set>
    	</property>
    	<property name="list" value="1">
    	</property>
    	<property name="strs" >
    		<array>
    			<value>1</value>
    			<value>2</value>
    			<value>3</value>
    		</array>
    	</property>
    	<property name="map">
    		<map>
    			<entry key="a" value="b" >
    			</entry>
    			<entry key="c" value="d" >
    			</entry>
    		</map>
    	</property>
    	
    	<!-- <property name="demo">
    		<props>
    			<prop key="key">value</prop>
    			<prop key="key1">value1</prop>
    		</props>
    	</property> -->
    	
    	<!-- 当一个类A需要依赖另一个类B的对象时,将B赋值给A的过程就叫做“依赖注入DI” -->
    	<property name="desk" ref="desk"></property>
    </bean>
    
    <!-- 被依赖的类的bean -->
    <bean id="desk" class="com.bjsxt.pojo.Desk">
    	<property name="id" value="1"></property>
    	<property name="price" value="12"></property>
    </bean>
</beans>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值