Spring实战第4版 第1部分 Spring的核心
读前须知: 本篇章内容取自《Spring实战第4版》 P3~P131,如需更多详细内容请购买正版书籍
下一章节: 【书籍篇】Spring实战第4版 第2部分 Web中的Spring(一)
一. 简化Java开发
1. 四大关键策略
- 基于POJO的轻量级和最小侵入性编程
- 通过依赖注入和面向接口实现松耦合
- 给予切面和惯例进行声明式编程
- 通过切面和模板减少样式模板
2. spring容器
2.1 bean工厂
- 由org.springframework.beans.factory.BeanFactory接口定义
- 是最简单赌到容器,提供基本的DI支持。
2.2 应用上下文
- 由org.springframework.context.ApplicationContext接口定义。
- 基于bean工厂构建,提供框架级别的服务。
二. 装配Bean
1. spring配置
- xml配置
- javaConfig配置
- 自动化配置
- 组件扫描
- 自动装配(存在歧义性)
2. 混合配置
@Import({TestDemo.class, TestDemo2.class})
@ImportResource("testDemo.xml")
@Configuration
public class DemoConfig {
...
}
<xml version="1.0" encoding="UTF-8">
<beans ...>
<bean class="com.wpj.DemoConfig" />
<import resource="testDemo.xml" />
</beans>
</xml>
三. 高级装配
1. profile配置
@Profile("dev")
public class DemoConfig{
...
}
@Profile("prod")
public DataSource getMysqlDataSource() {
...
}
<xml version="1.0" encoding="UTF-8">
<beans ... profile="dev">
<bean>...</bean>
</beans>
<beans ... profile="prod">
<bean>...</bean>
</beans>
</xml>
2. 激活profile
# 如果没有设置active的只就会查到default值,如果均没有设置就会创建所有没有定义profile的bean
spring:
profiles:
default: ...
active: ...
## 有多种方式设置这两个属性
1. 作为DispatcherServlet的初始化参数
2. 作为Web应用赌到上下文参数
3. 作为JDNI条目
4. 作为环境变量
5. 作为JVM赌到系统属性
6. 在集成测试类上,使用@ActiveProfiles注解设置
<!-- 举例 web.xml中 -->
<xml version="1.0" encoding="UTF-8">
<web-app version="2.5" ...>
<!-- 为上下文设置默认的profile-->
<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>dev</param-value>
</context-param>
<!-- 为Servlet设置默认的profile-->
<servlet>
<servlet-name>...</servlet-name>
<servlet-class>...</servlet-class>
<init-param>
<param-name>spring.profiles.default</param-name>
<param-value>dev</param-value>
</init-param>
</servlet>
</web-app>
</xml>
3. 条件化的Bean
// 只有配置文件中配置了magic属性才会初始化DemoBean
@Bean
@Conditional(DemoCondition.class)
public DemoBean createBean() {
return new BemoBean();
}
public interface Condition {
boolean matches(ConditionContext ctxt, AnotatedTypeMetadata metadata);
}
public class DemoCondition implements Condition {
public boolean matches(ConditionContext ctxt, AnotatedTypeMetadata metadata) {
Environment env = context.getEnvironment();
return env.containsProperty("magic");
}
}
4. 处理自动装配的歧义性
4.1 设置首选
// 通过设置首选(primary避免自动装配的歧义性,同一类型只能设置一个,多个无效)
@Bean
@Primary
public class DemoConfig implements Config {
...
}
@Bean
@Primary
public DataSource getMysqlDataSource() {
return new DataSource();
}
<xml version="1.0" encoding="UTF-8">
<beans ...>
<bean id="..." class="..." primary="true"/>
</beans>
</xml>
4.2 限定符
// 通过Qualifier注解,将spring容器实例化出来的id为demoBean的实例注入进来
@Autowired
@Qualifier("demoBean")
public void setDemoBean(DemoBean demoBean) {
this.demoBean = demoBean;
}
4.3 自定义限定符
略。。。详细可从书中p82获知
5. Bean的作用域
5.1 Spring作用域
单例(singleton)
原型(prototype)
会话(session)
请求(request)
@Component
// @scope("prototype")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) // 建议使用常量
public class Demo {
...
}
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public DataSource getMysqlDataSource() {
return new DataSource();
}
<xml version="1.0" encoding="UTF-8">
<beans ...>
<bean id="..." class="..." primary="true" scope="prototype" />
</beans>
</xml>
5.2 会话和请求作用域
@Component
@Scope(
value=WebApplicationContext.SCOPE-SESSION
proxyModed=scopedProxyMode.INTERFACES // 解决将会话或请求作用域的bean注入到单例bean中所遇到的问题
)
public DataSource getMysqlDataSource() {
...
}
5.2.1 问题
/**
* 因为DataService是一个单例的bean,会在spring应用上下文加载的时候创建
* 当它被创建时,spring试图将DataSource注入setDataSource()方法中
* 但是DataSource时会话作用域,此时并不存在,知道某个用户进入系统,创建会后之后才会出现
*/
@Component
public class DataService {
@Autowired
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
}
// 另外,系统中将会由多个DataSource实例,每个用户一个。
// 我们不希望让spring注入某个固定的DataSource实例到DataService中
// 希望这个实例且恰好是当前会话会对应的那一个
5.3 作用域代理
@Component
@Scope(
value=WebApplicationContext.SCOPE_SESSION
proxyModed=scopedProxyMode.INTERFACES // 生成基于接口的代理
// proxyModed=scopedProxyMode.TARGET_CLASS // 生成基于类的代理
)
public DataSource getMysqlDataSource() {
...
}
<xml version="1.0" encoding="UTF-8">
<!--beans中需要声明spring的aop命名空间,这里忽略 -->
<beans ...>
<!-- 声明作用域 -->
<bean id="..." class="..." primary="true" scope="session">
<aop:scoped-proxy />
</bean>
<!-- proy-target-class为false生产基于接口的代理,反之为类的代理 -->
<bean id="..." class="..." primary="true" scope="session">
<aop:scoped-proxy proy-target-class="false"/>
</bean>
</beans>
</xml>
5.4 运行时值注入
略。。。详细可从书中p88获知