Spring Bean 的实例化

普通构造方法,使用构造器实例化Bean

依然是先创建一个HelloWorld接口

1
2
3
4
5
package me.ipz.spring.chapter01.definition;

public interface HelloWorld {
public void sayHello();
}

创建HelloWorldImpl类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package me.ipz.spring.chapter01.definition;

public class HelloWorldImpl implements HelloWorld {

private String message;

/**
* 空构造器
*/
public HelloWorldImpl() {
this.message = "Hello World!";
}

/**
* 带参构造器
*
* @param message
*/
public HelloWorldImpl(String message) {
this.message = message;
}

public void sayHello() {
System.out.println(message);
}

}

创建Bean配置文件.xml

1
2
3
4
5
6
7
8
<!-- 空构造器实例化 -->
<bean id="helloWorldNoWithArgs" class="me.ipz.spring.chapter01.definition.HelloWorldImpl" />

<!-- 有参数构造器实例化 -->
<bean id="helloWorldWithArgs" class="me.ipz.spring.chapter01.definition.HelloWorldImpl">
<!-- 指定构造器参数 -->
<constructor-arg index="0" value="Hello Spring loC!" />
</bean>

主方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package me.ipz.spring.chapter01.definition;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

public static void main(String[] args) {
sayHelloWithNoArgs();
sayHelloWithArgs();
}

// 使用无参数构造器来实例化Bean
public static void sayHelloWithNoArgs() {
BeanFactory beanFactory = new ClassPathXmlApplicationContext("conf-instance.xml");
HelloWorld helloWorld = beanFactory.getBean("helloWorldNoWithArgs", HelloWorld.class);
helloWorld.sayHello();
}

// 使用有参数构造器来实例化Bean
public static void sayHelloWithArgs() {
BeanFactory beanFactory = new ClassPathXmlApplicationContext("conf-instance.xml");
HelloWorld helloWorld = beanFactory.getBean("helloWorldWithArgs", HelloWorld.class);
helloWorld.sayHello();
}

}

使用静态工厂方式实例化Bean

依然是分别创建接口,实例化类,配置文件.xml

然后再创建一个类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package me.ipz.spring.chapter01.definition;

public class HelloWorldStaticFactory {
/**
* 工厂方法
*
* @param message
* @return
*/
public static HelloWorld newInstance(String message) {
// 返回需要的Bean实例
return new HelloWorldImpl(message);
}
}

配置文件.xml 如下

1
2
3
4
5
6
7
8
9
// 使用静态工厂方法来实例化Bean
public static void HelloWorldStaticFactory() {
// 1.读取配置文件实例化一个loC容器
BeanFactory beanFactory = new ClassPathXmlApplicationContext("conf-instance.xml");
// 2.从容器中获取Bean,注意此处完全“面向接口编程,而不是面向实现”
HelloWorld helloWorld = beanFactory.getBean("helloServiceStaticFactory", HelloWorld.class);
// 3.执行业务逻辑
helloWorld.sayHello();
}

Main方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package me.ipz.spring.chapter01.definition;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

public static void main(String[] args) {
HelloWorldStaticFactory();
}

// 使用静态工厂方法来实例化Bean
public static void HelloWorldStaticFactory() {
// 1.读取配置文件实例化一个loC容器
BeanFactory beanFactory = new ClassPathXmlApplicationContext("conf-instance.xml");
// 2.从容器中获取Bean,注意此处完全“面向接口编程,而不是面向实现”
HelloWorld helloWorld = beanFactory.getBean("helloServiceStaticFactory", HelloWorld.class);
// 3.执行业务逻辑
helloWorld.sayHello();
}


}

使用实例工厂方式实例化Bean

依然是分别创建接口,实例化类,配置文件.xml
然后创建一个实例工程类

1
2
3
4
5
6
7
8
9
10
11
12
13
package me.ipz.spring.chapter01.definition;

public class HelloWorldInstanceFactory {
/**
* 工厂方法
*
* @param message
* @return
*/
public HelloWorld newInstance(String message) {
return new HelloWorldImpl(message);
}
}

配置信息如下,要注意使用实例工厂方式不能指定class属性,此时必须使用factory-bean属性来指定工厂Bean,factory-method属性指定实例化Bean的方法

1
2
3
4
5
6
<!-- 1.定义实例工厂Bean -->
<bean id="beanInstanceFactory" class="me.ipz.spring.chapter01.definition.HelloWorldInstanceFactory" />
<!-- 2.使用实例工厂Bean创建Bean -->
<bean id="helloWorldInstance" factory-bean="beanInstanceFactory" factory-method="newInstance">
<constructor-arg index="0" value="Hello Instance Factory!"></constructor-arg>
</bean>

Main主方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package me.ipz.spring.chapter01.definition;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

public static void main(String[] args) {
HelloWorldInstanceFactory();
}

// 使用实例工厂方法来实例化Bean
public static void HelloWorldInstanceFactory() {
// 1.读取配置文件实例化一个loC容器
BeanFactory beanFactory = new ClassPathXmlApplicationContext("conf-instance.xml");
// 2.从容器中获取Bean,注意此处完全“面向接口编程,而不是面向实现”
HelloWorld helloWorld = beanFactory.getBean("helloWorldInstance", HelloWorld.class);
// 3.执行业务逻辑
helloWorld.sayHello();
}


}
xiu~xiu~xiu~


0%