梦入琼楼寒有月,行过石树冻无烟

Java Spring Bean

Benan即Spring应用中的IoC容器可以创建、装配、配置的应用组件对象,而这些组件通常会被称之为“Spring Bean”。

Spring Bean 实例化

通常我们在面向对象编程中如果需要使用某某某对象需要事先对其实例化,同样在Spring框架中想使用Spring容器中的Bean即“春豆”,Spring共提供了三种方法,分别为构造方法、静态工厂实例化、实例工厂实例化,而在这其中最为经常使用的无非就是构造方法实例化

实例化包 instance

调用构造方法实例化 BeanClass.java

1
2
3
4
5
6
7
8
9
10
11
12
13
package instance;

public class BeanClass {
public String name;

public BeanClass() {
name = "调用构造方法实例化";
}

public BeanClass(String s) {
name = s;
}
}

调用静态方法实例化 BeanInstanceFactory.java

instance;
1
2
3
4
5
6

public class BeanInstanceFactory {
public BeanClass createBeanClassInstance() {
return new BeanClass("调用实例工厂方法实例化");
}
}

调用实例工厂实例化 BeanStaticFactory.java

1
2
3
4
5
6
7
8
package instance;

public class BeanStaticFactory {
private static BeanClass beanInstance = new BeanClass("调用静态的方法实例化");
public static BeanClass createInstance() {
return beanInstance;
}
}

建立测试包 config

实例化类 JavaConfig.java

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
28
29
30
31
32
33
34
package config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import instance.BeanClass;
import instance.BeanInstanceFactory;
import instance.BeanStaticFactory;

@Configuration
public class JavaConfig {

// 构造方法实例化
@Bean("beanClass")
public BeanClass getBeanClass() {
return new BeanClass();
}

// 静态工厂实例化
@Bean("beanStaticFactory")
public BeanClass getBeanStaticFactory() {
return BeanStaticFactory.createInstance();
}

// 势力工厂实例化
@Bean("beanInstanceFactory")
public BeanClass getBeanInstanceFactory() {
BeanInstanceFactory beaninstancefactory = new BeanInstanceFactory();
return beaninstancefactory.createBeanClassInstance();
}


}

测试类 TestBean.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package config;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import instance.BeanClass;

public class TestBean {
public static void main (String args[]) {
AnnotationConfigApplicationContext application =
new AnnotationConfigApplicationContext(JavaConfig.class);
// 调用构造方法实例化
BeanClass beanclassone = (BeanClass)application.getBean("beanClass");
System.out.println(beanclassone + beanclassone.name);

// 调用静态的方法实例化
BeanClass beanclasstwo = (BeanClass)application.getBean("beanStaticFactory");
System.out.println(beanclasstwo + beanclasstwo.name);

// 调用实例工厂方法实例化
BeanClass beanclassthree = (BeanClass)application.getBean("beanInstanceFactory");
System.out.println(beanclassthree + beanclassthree.name);
}
}

Bean 作用域

在Java Spring Bean作用域中,其中Bean的实例定义了六种作用域,主要可通过@Scope来实现 :

ID DA FA
singleton 默认使用的作用域,通过使用singleton在Spring中只有一个Bean实例 1
prototype Spring 容器每次获取Prototype定义的Bean,容器都将创建一个新的Bean实例 2
request 在HTTP请求中容器将返回一个Bean实例,仅可以在Web Spring应用上下文中使用 3
application 在每个ServletContexr对象上创建一个实例,即同一个应用共享一个Bean实例,仅可以在Web Spring应用程序上进行使用 4
websocket 为每个WebSocket对象中创建一个Bean实例,仅可以在Web Spring应用程序上下文中进行使用 5

通常在以上的六种作用域中,singleton和prototype是最常用的两种,而其他的作用域使用在Web Spring应用程序上下文中,通过下一个实例来演示Bean作用域。

Config

ScopeConfig.java
1
2
3
4
5
6
7
8
9
package config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("service")
public class ScopeConfig {
}
TestScope.java
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
package config;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import service.PrototypeService;
import service.SingletonService;

public class TestScope {
public static void main(String args[]) {
AnnotationConfigApplicationContext application =
new AnnotationConfigApplicationContext(ScopeConfig.class);
SingletonService singletonserviceone = application.getBean(SingletonService.class);
SingletonService singletonservicetwo = application.getBean(SingletonService.class);

System.out.println(singletonserviceone);
System.out.println(singletonservicetwo);

PrototypeService prototypeserviceone = application.getBean(PrototypeService.class);
PrototypeService prototypeservicetwo = application.getBean(PrototypeService.class);
System.out.println(prototypeserviceone);
System.out.println(prototypeservicetwo);

application.close();
}
}

Service

PrototypeService.java
1
2
3
4
5
6
7
8
9
package service;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

@Service
@Scope("prototype")
public class PrototypeService {
}
SingletonService.java
1
2
3
4
5
6
7
package service;

import org.springframework.stereotype.Service;

@Service // 默认为 singleton 相当于@Scope("singleton");
public class SingletonService {
}

从上图的运行结果可以的出@Service与@Scope(“prototype”)两个一起使用和单个使用的不同是最后在“PrototypeService”中输出的信息会随之改变,如下:

service.SingletonService@41089d
service.SingletonService@41089d
service.PrototypeService@a068d1
service.PrototypeService@1cb5951

Bean 的初始化和销毁

在Java Spring Bean中,存在生命周期,即当一个bean被实例化的时候,他需要执行一些可用的状态,同样,如不需要则在容器中移除。通常情况下我们使用**@Bean注解的initMethod、destroyMethod**属性来对Bean进行初始化和销毁:

Config

JavaConfig.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import service.MyService;

@Configuration
public class JavaConfig {
@Bean(initMethod="initService",destroyMethod="destroyService")
public MyService getMyService() {
return new MyService();
}
}

TesInitAndDestroy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package service;

public class MyService {
public void initService() {
System.out.println("initService - MyService");
}
public MyService() {
System.out.println("构造方法 - MyService");
}
public void destroyService() {
System.out.println("destroyService - MyService");
}
}

Service

MyService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package service;

public class MyService {
public void initService() {
System.out.println("initService - MyService");
}
public MyService() {
System.out.println("构造方法 - MyService");
}
public void destroyService() {
System.out.println("destroyService - MyService");
}
}

⬅️ Go back