glossary
bean_id
- 每个spring Bean都有个bean_id, 默认值类的第一个字母小写,eg:sgtPeppers
- 给定bean_id:
@Component("newBeanId")
base package
- Component scan 扫描Component注释的包路径
- 设置:@ComponentScan(basePackages="soundsystem")、或者@ComponentScan("soundsystem")、@ComponentScan(basePackages={"soundsystem", "video"})
- 推荐设置方式:@ComponentScan(basePackageClasses={CDPlayer.class, DVDPlayer.class}) 避免重构代码引起错误
Autowired
- spring 自动发现bean 依赖
- 自动处理bean间的依赖关系, Autowired可以用在任何的方法
@Autowired
public void setCompactDisc(CompactDisc cd) {
this.cd = cd;
}
- 当Autowired required方法为False时,代码中必须check null
@Autowired(required=false)
public CDPlayer(CompactDisc cd) {
this.cd = cd;
}
@RunWith(SpringJUnit4ClassRunner.class)
测试开始前会自动创建Spring application context
@ContextConfiguration(classes=CDPlayerConfig.class)
从CDPlayerConfig加载配置
automatic wiring
Component scanning
spring 自动发现beans,并在application context中创建。使用姿势:
- 使用Component 注释
package soundsystem;
import org.springframework.stereotype.Component;
@Component
public class SgtPeppers implements CompactDisc {
private String title = "Sgt. Pepper's Lonely Hearts Club Band";
private String artist = "The Beatles";
public void play() {
System.out.println("Playing " + title + " by " + artist);
} }
- 使用@ComponentScan enables component scanning
package soundsystem;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
public class CDPlayerConfig {
}
@ComponentScan 默认搜索同一个包底下使用@Component的类,找到后并创建
JavaConfig class
1. 创建一个配置类
@Configuration
public class CDPlayerConfig {
@Bean
public CompactDisc compactDisc() {
return new SgtPeppers();
}
@Bean
public CDPlayer cdPlayer(CompactDisc compactDisc) {
return new CDPlayer(compactDisc);
}
}
2. 声明一个bean
@Bean
public CDPlayer cdPlayer(CompactDisc compactDisc) {
return new CDPlayer(compactDisc);
}
3. 通过JavaConfig注入
注入方法:引用bean的方法,这边是指SgtPeppers()构造方法
@Bean
public CompactDisc compactDisc() {
return new SgtPeppers();
}
XML config
1. 创建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.xsd
http://www.springframework.org/schema/context">
<!-- configuration details go here -->
</beans>
- 必须指明引用的命名空间
- c-namespace,头部声明:
xmlns:c="http://www.springframework.org/schema/c
2. 用xml声明bean
<bean id="compactDisc" class="soundsystem.SgtPeppers" />
3. 使用构造方法初始化bean
- c-namespace demo:
<bean id="cdPlayer" class="soundsystem.CDPlayer"
c:_0-ref="compactDisc" />
- `_0` 编译后优化,无法知道具体参数名
- `c`: c-namespace prefix
- `-ref`: Injecting a bean reference
- `"compactDisc"`:The ID of the bean to inject
-
<constructor-arg>
比c-namespace功能更全面
<bean id="compactDisc"
class="soundsystem.BlankDisc">
<constructor-arg value="Sgt. Pepper's Lonely Hearts Club Band" />
<constructor-arg value="The Beatles" />
</bean>
properties 注入
两种配置方式:
- xml
<bean id="cdPlayer"
class="soundsystem.CDPlayer">
<property name="compactDisc" ref="compactDisc" />
</bean>
- p-namespace
<bean id="cdPlayer"
class="soundsystem.CDPlayer"
p:compactDisc-ref="compactDisc" />
- util-namespace 在xml配置复杂的数据结构,例如:constant, list, map, properties, property-path, set。 一般与p-namespace,c-namespace一起连用
mixed configuration
spring container管理所有Bean,不区分是从何种方式初始化的bean。
- JavaConfig 引用JavaConfig:
@Import(CDConfig.class)
- JavaConfig 引用xmlconfiguration:
@ImportResource("classpath:cd-config.xml")
- xml configuration 引用 xml configuration:
<import resource="cdplayer-config.xml" />
- xml configuration 引用 JavaConfig:
<bean class="soundsystem.CDConfig" />
- xml中autoscan配置:
<context:component-scan>
- 可单独一Java文件或者xml文件,当做root project,集合引用组合所有的config
网友评论