装配Bean

Spring容器负责创建应用程序中的bean并通过DI来协调这些对象之间的关系。
它提供了三种主要的装配机制:

  • 在XML中进行显式配置
  • 在java中进行显式配置
  • 隐式的bean发现机制和自动装配

自动化装配bean

Spring通过组件扫描和自动装配来实现自动化装配
在类上面使用@Component注解告知Spring创建bean。如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
package soundsystem;
import org.springframework.stereotype.Component;

@Component
public class SgtPeppers implements CompactDisc {
private String title = "hello";
private String artist = "threetree";

@Override
public void play() {
System.out.println("playing " + title + " by " + artist);
}
}

注意:Spring会默认根据类名(首字母变为小写)为bean指定一个ID,
也可以使用@Component("club") 来设置ID

在类上使用@ComponentScan注解来启用组件扫描,如下:

1
2
3
4
5
6
package soundsystem;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan
public class CDPlayerConfig {
}

注意:@ComponentScan会默认扫描与配置类相同的包,这里是soundsystem,
Spring会扫描这个包以及所有子包,查找带有@Component注解的类,自动为其中创建bean。
使用@ComponentScan("soundsystem") 来设置基础包
使用@ComponentScan(basePackages={"soundsystem","video"}) 设置多个基础包
考虑到类型安全问题,使用@ComponentScan(basePackageClasses={CDPlayer.class,DVDPlayer.class}) 来设置

通过注解将依赖注入
使用@Autowired注解来实现,它可以用在构造器,以及类的任何方法上,如下:

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 soundsystem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class CDPlayer {
private CompactDisc cd;

@Autowired
public CDPlayer(CompactDisc cd)
{
this.cd = cd;
}

@Autowired
public void insertDisc(CompactDisc cd)
{
this.cd = cd;
}

public void play()
{
cd.play();
}
}

注意:确保注入的依赖是已经匹配的bean,否则会异常。

JavaConfig实现注入

1
2
3
4
5
@Bean
public CDPlayer cdPlayer(CompactDisc cd)
{
return new CDPlayer(cd);
}

通过这种方式,不会要求将CompactDisc声明到同一个配置类中。
注意:默认情况下,Spring中的bean都是单例的。
Spring定义了多种作用域,如下:

  • 单例(Singleton),在整个应用中,只创建bean的一个实例
  • 原型(Prototype),每次注入或者通过Spring应用上下文获取的时候,都会创建一个新的bean实例
  • 会话(Session),在web应用中,为每个会话创建一个bean实例
  • 请求(Request),在web应用中,为每个请求创建一个bean实例

使用@Scope注解来声明作用域,如声明原型 @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)

XML装配bean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/views/"/><!--设置JSP文件的目录位置-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>

在Spring-beans模式中,是Spring配置文件的根元素,xml配置在Springmvc项目中更容易理解。

坚持原创分享,您的支持将鼓励我继续创作!