使用Spring创建对象的方式

使用Spring的IOC来创建对象一共有3中方式:


1)通过无参构造的方式来创建

Hello.java代码:


package com.myspring.bean;

public class Hello {

	private String name;
	
	public Hello() {
		System.out.println("hello 被创建");
	}

	public void setName(String name) {
		this.name = name;
	}

	public void show() {
		System.out.println("hello," + name);
	}

}


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容器创建和管理 -->
	<bean name="hello" class="com.myspring.bean.Hello">
		<property name="name" value="张三" />
	</bean>

</beans>



测试类Test代码:


package com.myspring.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.myspring.bean.Hello;


public class Test {

	public static void main(String[] args) {
		//解析beans.xml文件,生成管理相应的bean对象
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml") ;
		Hello hello = (Hello) context.getBean("hello") ;
		hello.show();
	}

}

运行可以看到控制台打印信息如下:





2)有参构造

Hello.java代码:


package com.myspring.bean;


public class Hello {

	private String name;
	
	public Hello(String name) {
		super();
		System.out.println("有参构造name:"+name);
		this.name = name;
	}

	public void show() {
		System.out.println("hello," + name);
	}

}


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容器创建和管理 -->
	<bean name="hello" class="com.myspring.bean.Hello">
		<constructor-arg index="0" value="李四" />
	</bean>

</beans>


有参的也有三种:第一种是index


其中index指构造方法参数下标从0开始,当有多个时,配置如下:

<constructor-arg index="0" value="李四" />
<constructor-arg index="1" value="王五" />

第二种是根据参数名称,指定name,如图:




第三种:根据参数类型设置,





测试类代码Test不变,测试结果控制台打印如下:





3)通过工厂方法

分为两种:


第一种是静态工厂


HelloFactory代码:


package com.myspring.factory;

import com.myspring.bean.Hello;

/**
 * 静态工厂类
 */
public class HelloFactory {

	public static Hello newInstance(String name) {
		return new Hello(name) ;
	}
	
}


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容器创建和管理 -->
	<bean id="hello" class="com.myspring.factory.HelloFactory"
		factory-method="newInstance">
		<constructor-arg index="0" value="李四" />
	</bean>

</beans>



第二种是动态工厂


动态工厂类代码:


package com.myspring.factory;

import com.myspring.bean.Hello;

/**
 * 动态工厂类
 */
public class HelloDynamicFactory {

	public Hello newInstance(String name) {
		return new Hello(name) ;
	}
	
}


注意这个代码的newInstance()方法,不是static的,否则运行程序会报错


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容器创建和管理 -->
	<bean id="helloFactory" class="com.myspring.factory.HelloDynamicFactory" />
	<bean id="hello" factory-bean="helloFactory" factory-method="newInstance">
		<constructor-arg index="0" value="李四" />
	</bean>

</beans>

测试代码:


package com.myspring.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.myspring.bean.Hello;


public class Test {

	public static void main(String[] args) {
		//解析beans.xml文件,生成管理相应的bean对象
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml") ;
		Hello hello = (Hello) context.getBean("hello") ;
		hello.show();
	}

}

测试结果:




















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值