Spring IoC容器实例化bean的三种方式

先来看一下工程目录结构


下面是接口及其实现类和实体类

package com.luhy.spring.hello;

public interface HelloApi {
	public void sayHello();
}

package com.luhy.spring.hello;

public class HelloImpl implements HelloApi {
	public void sayHello(){
		System.out.println("hello world...");
	}
}

package com.luhy.spring.hello;

public class HelloImpl2 implements HelloApi {
	//用构造方法实例实例化Bean
	private String message ;

	
	public HelloImpl2(){
		this.message = "hello world coming";
	}
	public HelloImpl2(String message ){
		this.message = message +"|||" ;
	}
	
	public HelloImpl2(String message,String g ){
		this.message = message +"|||" + g ;
	}
	@Override
	public void sayHello() {
		System.out.println(message);

	}

}

package com.luhy.spring.hello;

public class HelloApiStaticFactory {
	
	//用静态工厂方法实例实例化Bean
	public static HelloImpl2 newInstance(String message){
		return new HelloImpl2(message);
	}
}

package com.luhy.spring.hello;

public class HelloApiInstanceFactory {
//用实例工厂方法实例化Bean
	public HelloImpl2 newInstance(String message){
		return new HelloImpl2(message);
	}
}

测试类

package com.luhy.spring.hello;

import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloTest {
	@Test
	public void testHelloWorld(){
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		HelloApi h = context.getBean("hello4", HelloApi.class);
		h.sayHello();
	}
	
	
	public static void main(String[] args) {
//		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//		HelloApi h = context.getBean("hello4", HelloApi.class);
//		h.sayHello();
//		
		
//		BeanFactory bf = new ClassPathXmlApplicationContext("applicationContext.xml");
//		HelloApi hd = (HelloApi)bf.getBean("hello");
//		hd.sayHello();
	}
	
	
}

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" 
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="  
http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
http://www.springframework.org/schema/context                http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<!-- id 表示你这个组件的名字,class表示组件类 -->
	
	<!-- 使用空构造器  -->  
	<bean id="hello" class="com.luhy.spring.hello.HelloImpl">
	</bean>
	 <!-- 使用指定有参构造器  -->
	<bean id="hello2" class="com.luhy.spring.hello.HelloImpl2">
	<constructor-arg index="0" value="hhaha..."></constructor-arg>
	<constructor-arg index="1" value="hhaha..."></constructor-arg>
	</bean>
	
	<!-- 使用静态工厂方法 -->
	<bean id="hello3" class="com.luhy.spring.hello.HelloApiStaticFactory" factory-method="newInstance">
	<constructor-arg index="0" value="gagaga"></constructor-arg>
	</bean>
	
	<!--1、定义实例工厂Bean -->
	<bean id="ee" class="com.luhy.spring.hello.HelloApiInstanceFactory"/>  
	<!--2、使用实例工厂Bean创建Bean -->
	<bean id="hello4" factory-bean="ee" factory-method="newInstance">  
 	<constructor-arg index="0" value="Hello Spring!"></constructor-arg>  
	</bean>

	
 </beans>


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值