版权声明:本文为博主原创文章,经博主允许,可自由共享,尽量不要用于商业用途。 https://blog.csdn.net/matrixbbs/article/details/88298374
1构造器实例化bean
其中,构造器可以有参,也可以无参,如果有参,需要传入相应的参数,即实例方法一定会用到这个入参
2静态工厂方法实例化业务bean
3实例工厂方法实例化业务bean
实例安排如下:
接口:HelloSpring
主要动用:sayHello()
实现:HelloSpringImpl
静态工厂类:HelloSpringStaticFactory
实例工厂类:HelloSpringInstanceFactory
驱动:main函数
配置:conf/conf-instance.xml
实现代码如下:
HelloSpring.java
package com.zfh.spring.chapter02.instance;
public interface HelloSpring {
public void sayHello();
}
HelloSpringImpl.java
package com.zfh.spring.chapter02.instance;
public class HelloSpringImpl implements HelloSpring {
private String message;
/**
* 空参构造器
*/
public HelloSpringImpl() {
this.message = "Hello Spring from null constructor.";
}
/**
* 有参构造器
*/
public HelloSpringImpl(String message) {
this.message = message + "from constructor with 1 argument.";
}
@Override
public void sayHello() {
System.out.println(message);
}
}
HelloSpringStaticFactory.java
package com.zfh.spring.chapter02.instance;
public class HelloSpringStaticFactory {
/**
* 静态工厂方法
* @param message
* @return 一个HelloSpring的bean实例
*/
public static HelloSpring newInstance(String message) {
//实例化一个bean然后返回它
HelloSpring helloSpring = new HelloSpringImpl(message);
return helloSpring;
}
}
HelloSpringInstanceFactory.java
package com.zfh.spring.chapter02.instance;
public class HelloSpringInstanceFactory {
/**
* 工厂方法
* @param message
* @return 一个HelloSpring实例bean
*/
public HelloSpring newInstance(String message) {
HelloSpring helloSpring = new HelloSpringImpl(message);
return helloSpring;
}
}
Main.java
package com.zfh.spring.chapter02.instance;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
/**
* 主驱动
* @param args
*/
public static void main(String[] args) {
// System.out.print("无参构造器注入测试: ");
// sayHelloWithNoArgs();
// System.out.print("有参构造器注入测试: ");
// sayHelloWithArgs();
// System.out.print("用静态工厂实例化bean的测试: ");
// helloSpringStaticFactory();
System.out.print("用工厂bean来实例化业务bean的测试: ");
helloSpringInstanceFactory();
}
/**
* 直接用工厂bean来实例化业务bean
*/
private static void helloSpringInstanceFactory() {
//实例化工厂bean
BeanFactory beanFactory =
new ClassPathXmlApplicationContext("conf/conf-instance.xml");
//实例化业务bean
HelloSpring helloSpring = beanFactory.getBean("HelloSpringInstance", HelloSpring.class);
//用业务bean完成业务
helloSpring.sayHello();
}
/**
* 无参构造器注入方式
*/
public static void sayHelloWithNoArgs() {
BeanFactory beanFactory =
new ClassPathXmlApplicationContext("conf/conf-instance.xml");
HelloSpring helloSpring = beanFactory.getBean("HelloSpringWithNoArgs",HelloSpring.class);
helloSpring.sayHello();
}
/**
* 有参构造器注入方式
*/
public static void sayHelloWithArgs() {
BeanFactory beanFactory =
new ClassPathXmlApplicationContext("conf/conf-instance.xml");
HelloSpring helloSpring = beanFactory.getBean("HelloSpringWithArgs",HelloSpring.class);
helloSpring.sayHello();
}
/**
* 用静态工厂注入方式,即用静态工厂来实例化一个业务bean
*/
public static void helloSpringStaticFactory() {
// 1、读取配置文件实例化一个IOC容器
BeanFactory beanFactory =
new ClassPathXmlApplicationContext("conf/conf-instance.xml");
// 2、从容器中获取Bean,注意此处完全“面向接口编程,而不是面向实现”
HelloSpring helloSpring
= beanFactory.getBean("HelloSpringStaticFactory", HelloSpring.class);
// 3、执行业务逻辑
helloSpring.sayHello();
}
}
conf/conf-instance.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-3.0.xsd">
<!-- 无参构造器注入配置,也就是默认构造函数配置 -->
<bean id="HelloSpringWithNoArgs"
class="com.zfh.spring.chapter02.instance.HelloSpringImpl" />
<!-- 有参构造器注入配置 -->
<bean id="HelloSpringWithArgs"
class="com.zfh.spring.chapter02.instance.HelloSpringImpl" >
<!-- 需要指定构造函数的参数 -->
<constructor-arg index="0" value="Hello Spring with Arguments."></constructor-arg>
</bean>
<!-- 静态工厂方式注入配置 -->
<bean id="HelloSpringStaticFactory"
class="com.zfh.spring.chapter02.instance.HelloSpringStaticFactory"
factory-method="newInstance" >
<!-- 为静态工厂指定实例化方法,这是一个静态方法 -->
<!-- 该静态工厂实例化方法有参数,在这里注入参数 -->
<!-- 该参数即为工厂调用了有参构造器的参数,要指定构造器参数 -->
<constructor-arg index="0" value="HelloSpring Static Factory"></constructor-arg>
</bean>
<!-- 实例工厂方式注入配置 -->
<bean id="HelloSpringInstanceFactory"
class="com.zfh.spring.chapter02.instance.HelloSpringInstanceFactory" />
<!-- 用实例工厂的bean来注入业务bean -->
<bean id="HelloSpringInstance"
factory-bean="HelloSpringInstanceFactory"
factory-method="newInstance" >
<!-- 指定: 1 实例工厂bean,2 实例化方法 -->
<!-- 该工厂实例化方法有参数,在这里注入参数 -->
<!-- 该参数即为工厂调用了有参构造器的参数,要指定构造器参数 -->
<constructor-arg index="0" value="HelloSpring Instance Factory"></constructor-arg>
</bean>
</beans>
要特别学习一下:面向接口的编程思想。当容器可以直接实现相应的接口(其实是拿到相应接口的实例)时,就可以直接用接口来实例化业务bean,然后就可以直接做业务了。