先来看一下工程目录结构
下面是接口及其实现类和实体类
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>