spring创建对象的几种方式

spring创建对象的几种方式

Spring 通过容器(bean工厂),创建对象和属性。

对象是由spring容器创建的,对象属性是spring容器设置的。这个过程就叫控制反转:

控制的内容:指谁来控制对象的创建,传统的应用程序对象的创建是由程序本身控制的,使用spring后,是由spring来创建对象的。
反转:正转指程序来创建对象,反转指程序本身不去创建对象,而变为被打接收对象。

总结:以前对象是由程序本身来创建,使用spring后,程序变为被动接收spring创建好的对象。
  控制反转--等同于-依赖注入(dependency injection) 


第一种:调用无参构造器创建。

1、创建vo类

public class Hello {
	private String name;
	
	public Hello(){
		System.out.println("无参构造创建");
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public void show(){
		System.out.println("name:"+name);
	}
}

2、编写beans.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就是java对象,由spring容器来创建和管理
scope="":创建对象是单例还是每次都创建一个新对象等。
 -->
	<bean id="hello" class="spring.bean.Hello">
		<property name="name" value="张三"></property>
	</bean>
</beans>

3、测试

blic class Test {
	public static void main(String[] args) {
		//解析beans.xml,生产管理相应的bean对象
		ApplicationContext conte
t = new ClassPathXmlApplicationContext("beans.xml");
		//调用getBean()方法,会创建一个对象,通过无参构造方法创建。
		Hello hello = (Hello)context.getBean("hello");
		hello.show();
	}
二、调用有参构造器,创建对象。
1、创建vo类

public class Hello {
	private String name;
	
	public Hello(String name){
		System.out.println("调用有参构造器!");
		this.name = name;
	}
	
	public void show(){
		System.out.println("name:"+name);
	}
}

2、编写beans.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就是java对象,由spring容器来创建和管理
scope="":创建对象是单例还是每次都创建一个新对象等。
 -->
	<bean id="hello" class="spring.bean.Hello">
		<!-- index指构造方法参数下标从0开始,有多个参数,设置多个<constructor-arg/>,改变index即可 -->
		<!-- <constructor-arg index="0" value="李四"></constructor-arg>-->		
		
		<!-- 根据参数名称,配置参数 -->
		<!-- <constructor-arg name="name" value="王五"></constructor-arg> -->
	
		<!-- 根据参数类型,配置参数, -->
		<constructor-arg type="java.lang.String" value="张三"></constructor-arg>
	</bean>
</beans>


3、测试

public class Test {
	public static void main(String[] args) {
		//解析beans.xml,生产管理相应的bean对象
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
		//调用getBean()方法,会创建一个对象,通过有参构造方法创建。
		Hello hello = (Hello)context.getBean("hello");
		hello.show();
	}
}

三、通过工厂方法创建对象

第一种:静态工厂

1、创建工厂类

package spring.Factory;

import spring.bean.User;

public class UserFactory {
	public static User getUser(String name){
		return new User(name);
	}
}

2.编写bean.xml

	<bean id="user" class="spring.Factory.UserFactory" factory-method="getUser">
		<constructor-arg index="0" value="张三"></constructor-arg>
	</bean>
3、测试

public class Test {
	public static void main(String[] args) {
		//解析beans.xml,生产管理相应的bean对象
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
		//调用getBean()方法,会创建一个对象,通过工厂创建。
		User hello = (User)context.getBean("user");
		hello.show();
	}
}
第二种:动态工厂

1、创建动态工厂类

package spring.Factory;

import spring.bean.User;

public class UserDynamicFactory {
	public User getUser(String name){
		return new User(name);
	}
}
2、编写beans.xml

<bean id="userFactory" class="spring.Factory.UserDynamicFactory" ></bean>
	<bean id="user" factory-bean="userFactory" factory-method="getUser">
		<constructor-arg index="0" value="张三"></constructor-arg>
	</bean>








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值