技术书籍这么多,每次好不容易读完一本,但总过不了多久就会遗忘。为了对抗,整理记录和回看,也是实属必要。
由此,从这《Spring 实战(第四版)》开始,记录一下知识点,下次再要复习时,能免去翻阅厚厚的原文,是以为把书读薄。

1、Spring概要

1.1、Spring简化开发

Spring的使命是简化java开发,为此其采用了4种关键策略:

基于POJO的轻量级和最小侵入性编程;
通过依赖注入和面向接口实现松耦合;
基于切面和惯例进行声明式编程;
通过切面和模板减少样式代码;

1.1.1、DI依赖注入Demo:

  1. package sia.knights;
  2.  
  3. public class BraveKnight implements Knight {
  4.  
  5. private Quest quest;
  6.  
  7. public BraveKnight(Quest quest) {
  8. this.quest = quest;
  9. }
  10.  
  11. public void embarkOnQuest() {
  12. quest.embark();
  13. }
  14.  
  15. }

在BraveKnight的构造方法中,Quest被作为参数传入,这是依赖注入的方式之一,即构造器注入。

  1. package sia.knights;
  2.  
  3. import java.io.PrintStream;
  4.  
  5. public class SlayDragonQuest implements Quest {
  6.  
  7. private PrintStream stream;
  8.  
  9. public SlayDragonQuest(PrintStream stream) {
  10. this.stream = stream;
  11. }
  12.  
  13. public void embark() {
  14. stream.println("Embarking on quest to slay the dragon!");
  15. }
  16.  
  17. }

SlayDragonQuest没有使用System.out.println(),而是在构造方法中请求一个更为通用的PrintStream。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.  
  7. <bean id="knight" class="sia.knights.BraveKnight">
  8. <constructor-arg ref="quest" />
  9. </bean>
  10.  
  11. <bean id="quest" class="sia.knights.SlayDragonQuest">
  12. <constructor-arg value="#{T(System).out}" />
  13. </bean>
  14.  
  15. </beans>

如上XML的装配(wiring)方式,用一个简单的Spring配置文件:knights.xml,该配置文件将BraveKnight、SlayDragonQuest和PrintStream装配到了一起。这里,BraveKnight和SlayDragonQuest被声明为Spring中的bean。就BraveKnight bean来讲, 它在构造时传入了对SlayDragonQuest bean的引用,将其作为构造器参数。同时,SlayDragonQuest bean的声明使用了Spring表达式语言(Spring Expression Language),将System.out(这是一个PrintStream)传入到了SlayDragonQuest的构造器中。  当然xml配置也可换成java配置。

  1. package sia.knights.config;
  2.  
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5.  
  6. import sia.knights.BraveKnight;
  7. import sia.knights.Knight;
  8. import sia.knights.Quest;
  9. import sia.knights.SlayDragonQuest;
  10.  
  11. @Configuration
  12. public class KnightConfig {
  13.  
  14. @Bean
  15. public Knight knight() {
  16. return new BraveKnight(quest());
  17. }
  18.  
  19. @Bean
  20. public Quest quest() {
  21. return new SlayDragonQuest(System.out);
  22. }
  23.  
  24. }

Spring通过应用上下文(Application Context)装载bean的定义并把它们组装起来。Spring应用上下文全权负责对象的创建和组装。Spring自带了多种应用上下文的实现,它们之间主要的区别仅仅在于如何加载配置。因为knights.xml中的bean是使用XML文件进行配置的,所以选择ClassPathXmlApplicationContext作为应用上下文(采用knightConfig的话,选择AnnotationConfigApplicationContext)。该类加载位于应用程序类路径下的一个或多个XML配置文件。下面的代码中的main()方法调用ClassPathXmlApplicationContext加载knights.xml,并获得Knight对象的引用。

  1. package sia.knights;
  2.  
  3. import org.springframework.context.support.
  4. ClassPathXmlApplicationContext;
  5.  
  6. public class KnightMain {
  7.  
  8. public static void main(String[] args) throws Exception {
  9. ClassPathXmlApplicationContext context =
  10. new ClassPathXmlApplicationContext(
  11. "META-INF/spring/knight.xml");
  12. Knight knight = context.getBean(Knight.class);
  13. knight.embarkOnQuest();
  14. context.close();
  15. }
  16.  
  17. }

1.1.2、AOP切面Demo:

利用上面依赖注入的Demo,添加一个组件,再将这个组件声明为切面,再适当配置即可。

  1. package sia.knights;
  2.  
  3. import java.io.PrintStream;
  4.  
  5. public class Minstrel {
  6.  
  7. private PrintStream stream;
  8.  
  9. public Minstrel(PrintStream stream) {
  10. this.stream = stream;
  11. }
  12.  
  13. public void singBeforeQuest() {
  14. stream.println("Fa la la, the knight is so brave!");
  15. }
  16.  
  17. public void singAfterQuest() {
  18. stream.println("Tee hee hee, the brave knight " +
  19. "did embark on a quest!");
  20. }
  21.  
  22. }
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xsi:schemaLocation="http://www.springframework.org/schema/aop
  6. http://www.springframework.org/schema/aop/spring-aop.xsd
  7. http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd">
  9.  
  10. <bean id="knight" class="sia.knights.BraveKnight">
  11. <constructor-arg ref="quest" />
  12. </bean>
  13.  
  14. <bean id="quest" class="sia.knights.SlayDragonQuest">
  15. <constructor-arg value="#{T(System).out}" />
  16. </bean>
  17.  
  18. <bean id="minstrel" class="sia.knights.Minstrel">
  19. <constructor-arg value="#{T(System).out}" />
  20. </bean>
  21.  
  22. <aop:config>
  23. <aop:aspect ref="minstrel">
  24. <aop:pointcut id="embark"
  25. expression="execution(* *.embarkOnQuest(..))"/>
  26.  
  27. <aop:before pointcut-ref="embark"
  28. method="singBeforeQuest"/>
  29.  
  30. <aop:after pointcut-ref="embark"
  31. method="singAfterQuest"/>
  32. </aop:aspect>
  33. </aop:config>
  34.  
  35. </beans>

这里使用了Spring的aop配置命名空间把Minstrel bean声明为一个切面。首先,需要把Minstrel声明为一个bean,然后在<aop:aspect>元素中引用该bean。为了进一步定义切面,声明(使用<aop:before>)在embarkOnQuest()方法执行前调用Minstrel的singBeforeQuest()方法。这种方式被称为前置通知(before advice)。同时声明(使用<aop:after>)在embarkOnQuest()方法执行后调用singAfter Quest()方法。这种方式被称为后置通知(after advice)。

在这两种方式中,pointcut-ref属性都引用了名字为embank的切入点。该切入点是在前边的<pointcut>元素中定义的, 并配置expression属性来选择所应用的通知。表达式的语法采用的是AspectJ的切点表达式语言。

1.1.3、使用模板消除样板式代码Demo:

如下为一般的使用JDBC访问数据库查询数据的代码:

  1. public Employee getEmployeeById(long id) {
  2. Connection conn = null;
  3. PreparedStatement stmt = null;
  4. ResultSet rs = null;
  5. try {
  6. conn = dataSource.getConnection();
  7. stmt = conn.prepareStatement("select id, firstname, lastname, salary from employee where id=?");
  8. stmt.setLong(1, id);
  9. rs = stmt.executeQuery();
  10. Employee employee = null;
  11. if (rs.next()) {
  12. employee.setId(rs.getLong("id"));
  13. employee.setFirstName(rs.getString("firstname"));
  14. employee.setLastName(rs.getString("lastname"));
  15. employee.setSalary(rs.getBigDecimal("salary"));
  16. }
  17. return employee;
  18. } catch (SQLEception e) {
  19. } finally {
  20. if (rs != null) {
  21. try {
  22. rs.close();
  23. } catch(SQLEception e) {}
  24. }
  25. if (stmt != null) {
  26. try {
  27. rs.close();
  28. } catch(SQLEception e) {}
  29. }
  30. if (conn != null) {
  31. try {
  32. rs.close();
  33. } catch(SQLEception e) {}
  34. }
  35. }
  36. return null;
  37. }

Spring通过模板封装来消除样板式代码。使用Spring的JdbcTemplate执行数据库操作时,可以避免传统的JDBC样板代码。对上例的方法重写:

  1. public Employee getEmployeeById(long id) {
  2. return jdbcTemplate.queryForObject(
  3. "select id, firstname, lastname, salary from employee where id=?",
  4. new RowMapper<Employee>() {
  5. public Employee mapRow(ResultSet rs, int rowNum) throws SQLException {
  6. Employee employee = new Employee();
  7. employee.setId(rs.getLong("id"));
  8. employee.setFirstName(rs.getString("firstname"));
  9. employee.setLastName(rs.getString("lastname"));
  10. employee.setSalary(rs.getBigDecimal("salary"));
  11. return employee;
  12. }
  13. },
  14. id);
  15. }

1.2、Spring容器和Bean对象

  在基于Spring的应用中,应用对象生存于Spring容器(container)中。容器是Spring框架的核心。Spring容器使用DI管理构成应用的组件,它会创建相互协作的组件之间的关联。
  Spring容器并不是只有一个。Spring自带了多个容器实现,可以归为两种不同的类型。bean工厂(由org.springframework. beans.factory.eanFactory接口定义)是最简单的容器,提供基本的DI支持。应用上下文(由org.springframework.context.ApplicationContext接口定义)基于BeanFactory构建,并提供应用框架级别的服务,例如从属性文件解析文本信息以及发布应用事件给感兴趣的事件监听者。
  虽然我们可以在bean工厂和应用上下文之间任选一种,但bean工厂对大多数应用来说往往太低级了,因此,应用上下文要比bean工厂更受欢迎。我们会把精力集中在应用上下文的使用上,不再浪费时间讨论bean工厂。

1.2.1、使用应用上下文(ApplicationContext)

Spring中常用的几种应用上下文:

AnnotationConfigApplicationContext:从一个或多个基于Java的配置类中加载Spring应用上下文。
AnnotationConfigWebApplicationContext:从一个或多个基于Java的配置类中加载Spring Web应用上下文。
ClassPathXmlApplicationContext:从类路径下的一个或多个XML配置文件中加载上下文定义,把应用上下文的定义文件作为类资源。
FileSystemXmlapplicationcontext:从文件系统下的一个或多个XML配置文件中加载上下文定义。
XmlWebApplicationContext:从Web应用下的一个或多个XML配置文件中加载上下文定义。

获得上下文示例:

  1. ApplicationContext context = new FileSystemXmlapplicationcontext("D:/knight.xml");
  2. ApplicationContext context = new ClassPathXmlApplicationContext("knight.xml");
  3. ApplicationContext context = new AnnotationConfigApplicationContext(com.springinaction.knights.config.knightConfig.class);

获得应用上下文后,调用geiBean()方法,即可获取Spring容器中的bean。

1.2.2、bean的生命周期

bean在Spring容器中从创建到销毁经历了若干阶段,每一阶段都可以针对Spring如何管理bean进行个性化定制。

1.Spring对bean进行实例化;
2.Spring将值和bean的引用注入到bean对应的属性中;
3.如果bean实现了BeanNameAware接口,Spring将bean的ID传递给setBean-Name()方法;
4.如果bean实现了BeanFactoryAware接口,Spring将调用setBeanFactory()方法,将BeanFactory容器实例传入;
5.如果bean实现了ApplicationContextAware接口,Spring将调用setApplicationContext()方法,将bean所在的应用上下文的引用传入进来;
6.如果bean实现了BeanPostProcessor接口,Spring将调用它们的post-ProcessBeforeInitialization()方法;
7.如果bean实现了InitializingBean接口,Spring将调用它们的after-PropertiesSet()方法;
8.如果bean使用initmethod声明了初始化方法,该方法也会被调用;
9.如果bean实现了BeanPostProcessor接口,Spring将调用它们的post-ProcessAfterInitialization()方法;
10.此时,bean已经准备就绪,可以被应用程序使用了,它们将一直驻留在应用上下文中,直到该应用上下文被销毁;
11.如果bean实现了DisposableBean接口,Spring将调用它的destroy()接口方法;
12.如果bean使用destroymethod声明了销毁方法,该方法也会被调用。

第3、4、5步是让Bean了解spring容器。

第6和第9步 可以针对指定的Bean进行功能增强,这时一般会使用动态代理。BeanPostProcessor是容器级别的,默认会对applicationContext里的所有bean增强。

第7和第11步是通过实现指定的接口来完成init与destroy操作,但一般不用,而是使用第8步和第12步来做。自定义的初始化与销毁操作它无耦合,推荐使用。但是必须在配置文件中指定初始化与销毁的方法,通过bean标签的 init-method="myInit" 和 destroy-method="myDestroy" 来完成。

对于bean的生命周期,我们需要关注的主要有两个方法:
1. 增强bean的功能可以使用后处理Bean, BeanPostProcessor
2. 如果需要初始化或销毁操作我们可以使用 init-method  destroy-method
注意:destroy-method只对scope=singleton有效果。

2、装配Bean

2.1、Spring配置的可选方案

Spring为Bean的装配提供了三种主要的装配机制:

在xml中进行显示配置;
在java中进行显示配置;
隐式的bean发现机制和自动装配。

这三种配置方案可以任意选择,及搭配使用。在实际的开发中使用的最多的还是自动装配机制,这也是书作者推荐的,但在xml和javaConfig的选择上个人更愿意使用xml。

2.2、自动化装配bean

自动装配的实现有两部分:

组件扫描(component scanning):Spring会自动发现应用上下文中所创建的bean。
自动装配(autowiring):Spring自动满足bean之间的依赖。

2.2.1、创建可被发现的bean

Demo:

  1. package soundsystem;
  2.  
  3. public interface CompactDisc {
  4. void play();
  5. }
  1. package soundsystem;
  2. import org.springframework.stereotype.Component;
  3.  
  4. @Component
  5. public class SgtPeppers implements CompactDisc {
  6.  
  7. private String title = "Sgt. Pepper's Lonely Hearts Club Band";
  8. private String artist = "The Beatles";
  9.  
  10. public void play() {
  11. System.out.println("Playing " + title + " by " + artist);
  12. }
  13.  
  14. }
  1. package soundsystem;
  2. import org.springframework.context.annotation.ComponentScan;
  3. import org.springframework.context.annotation.Configuration;
  4.  
  5. @Configuration
  6. @ComponentScan
  7. public class CDPlayerConfig {
  8. }

@Component 注解表示该类是组件类;
@Configuration 注解表示该类是配置类;
@ComponentScan 注解表示会扫描与配置类相同的包及子包,因为Spring默认不开启组件扫描。
若是使用xml来启用组件扫描,那得使用Spring context命名空间的<context:component-scan>元素。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:c="http://www.springframework.org/schema/c"
  6. xmlns:p="http://www.springframework.org/schema/p"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  9.  
  10. <context:component-scan base-package="soundsystem" />
  11.  
  12. </beans>

soundsystem.xml

创建JUnit测试,它会创建Spring上下文, 并判断CompactDisc是不是真的创建出来了。

  1. package soundsystem;
  2.  
  3. import static org.junit.Assert.*;
  4.  
  5. import org.junit.Test;
  6. import org.junit.runner.RunWith;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.test.context.ContextConfiguration;
  9. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  10.  
  11. @RunWith(SpringJUnit4ClassRunner.class)
  12. @ContextConfiguration(classes=CDPlayerConfig.class)
  13. public class CDPlayerTest {
  14.  
  15. @Autowired
  16. private CompactDisc cd;
  17.  
  18. @Test
  19. public void cdShouldNotBeNull() {
  20. assertNotNull(cd);
  21. }
  22.  
  23. }

CDPlayerTest.java

CDPlayerTest使用了Spring的SpringJUnit4ClassRunner,以便在测试开始的时候自动创建Spring的应用上下文。 注解@ContextConfiguration会告诉它需要在CDPlayerConfig中加载配置。 因为CDPlayerConfig类中包含了@ComponentScan,因此最终的应用上下文中应该包含CompactDisc bean。

2.2.2、为组件扫描的bean命名

Spring应用上下文中所有的bean都会给定一个ID。在前面的例子中,尽管没有明确地为SgtPeppersbean设置ID, 但Spring会将类名的第一个字母变为小写指定为其ID,这个bean所给定的ID即为sgtPeppers。
如果想为这个bean设置不同的ID,你所要做的就是将期望的ID作为值传递给@Component注解。比如说, 如果想将这个bean标识为lonelyHeartsClub,@Component注解配置为 @Component("lonelyHeartsClub")。

@Component注解的替换对象为@Named,不过几乎不用,字面意思不直观。

2.2.3、设置组件扫描的基础包

通过对@ComponentScan注解的value的设置,来指定组件扫描的包。

@ComponentScan("soundsystem")
@ComponentScan(basePackages="soundsystem")
@ComponentScan(basePackages={"soundsystem", "video"})

上例中,所设置的基础包都是String类型表示的,但是类型不安全,如果重构代码(改变了包名或包层次),那么指定的基础包就可能会出错。为此可以将其指定为包中所包含的类或接口:
@ComponentScan(basePackageClasses={CDPlayer.class, DVDPlayer.class})
这里,给basePackageClasses设置的是组件类, 但是你可以考虑在包中创建一个用来进行扫描的空标记接口(markerinterface)。通过标记接口的方式, 你依然能够保持对重构友好的接口引用,但是可以避免引用任何实际的应用程序代码。但实际上这个影响不大,智能的ide能告诉你这个包是否应用出错。

2.2.4、bean添加注解完成制动装配

在构造方法、属性的Setter方法或其他方法上加上@Autowired注解就可以注入。如果没有匹配的bean,那么在应用上下文创建的时候,Spring会抛出异常。为了避免异常的出现,可以将@Autowired的required属性设置为false:@Autowired(required=false) 。@Inject和@Autowired可以互相替换。

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

2.2.5、验证自动装配

  1. package soundsystem;
  2.  
  3. import static org.junit.Assert.*;
  4.  
  5. import org.junit.Rule;
  6. import org.junit.Test;
  7. import org.junit.contrib.java.lang.system.StandardOutputStreamLog;
  8. import org.junit.runner.RunWith;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.test.context.ContextConfiguration;
  11. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  12.  
  13. @RunWith(SpringJUnit4ClassRunner.class)
  14. @ContextConfiguration(classes=CDPlayerConfig.class)
  15. public class CDPlayerTest {
  16.  
  17. @Rule
  18. public final StandardOutputStreamLog log = new StandardOutputStreamLog();
  19.  
  20. @Autowired
  21. private MediaPlayer player;
  22.  
  23. @Autowired
  24. private CompactDisc cd;
  25.  
  26. @Test
  27. public void cdShouldNotBeNull() {
  28. assertNotNull(cd);
  29. }
  30.  
  31. @Test
  32. public void play() {
  33. player.play();
  34. assertEquals(
  35. "Playing Sgt. Pepper's Lonely Hearts Club Band by The Beatles\n",
  36. log.getLog());
  37. }
  38.  
  39. }

CDPlayerTest.java

2.3、通过java代码装配bean

  有时候自动化配置的方案行不通,因此需要明确配置Spring。比如说,你想要将第三方库中的组件装配到你的应用中,在这种情况下,是没有办法在它的类上添加@Component和@Autowired注解的, 因此就不能使用自动化装配的方案了。在这种情况下,你必须要采用显式装配的方式。在进行显式配置的时候,有两种可选方案:Java和XML。

  javaConfig就是java代码,但不包含业务逻辑,也不应该侵入到业务逻辑代码中。尽管不是必须的,但通常会将JavaConfig放到单独的包中,使它与其他的应用程序逻辑分离开来。

  1. package soundsystem;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4.  
  5. @Configuration
  6. public class CDPlayerConfig {
  7.  
  8. @Bean
  9. public CompactDisc compactDisc() {
  10. return new SgtPeppers();
  11. }
  12.  
  13. @Bean
  14. public CDPlayer cdPlayer(CompactDisc compactDisc) {
  15. return new CDPlayer(compactDisc);
  16. }
  17.  
  18. }

默认情况下,bean的ID与带有@Bean注解的方法名是一样的。在本例中,bean的名字将会是compactDisc。也可以通过name属性指定不同的名字,@Bean(name="xxx")。
    在这里,cdPlayer()方法请求一个CompactDisc作为参数。当Spring调用cdPlayer()创建CDPlayerbean的时候, 它会自动装配一个CompactDisc到配置方法之中。然后,方法体就可以按照合适的方式来使用它。借助这种技术, cdPlayer()方法也能够将CompactDisc注入到CDPlayer的构造器中,而且不用明确引用CompactDisc的@Bean方法。
    通过这种方式引用其他的bean通常是最佳的选择,因为它不会要求将CompactDisc声明到同一个配置类之中,也不要求必须在javaConfig中声明。你可以将配置分散到多个配置类、XML文件以及自动扫描和装配bean之中,只要功能完整健全即可。不管CompactDisc是采用什么方式创建出来的,Spring都会将其传入到配置方法中,并用来创建CDPlayer bean。

2.4、通过xml装配bean

2.4.1、构造器注入初始化bean

1、构造器注入bean两种方式

<constructor-arg>元素

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.  
  6. <bean id="compactDisc" class="soundsystem.SgtPeppers" />
  7.  
  8. <bean id="cdPlayer" class="soundsystem.CDPlayer">
  9. <constructor-arg ref="compactDisc" />
  10. </bean>
  11.  
  12. </beans>

constructor-arg.xml

使用Spring 3.0所引入的c-命名空间

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:c="http://www.springframework.org/schema/c"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.  
  7. <bean id="compactDisc" class="soundsystem.SgtPeppers" />
  8.  
  9. <bean id="cdPlayer" class="soundsystem.CDPlayer"
  10. c:cd-ref="compactDisc" />
  11.  
  12. </beans>

c-名称空间.xml

在这里,引用了构造器参数的名称,为了避免优化时参数名称的该变造成问题,可以使用参数的索引来表示,上例可变成:c:_0-ref="compactDisc"。因为xml中不允许数字属性的第一个字符,因此加了一个下划线作为前缀。如果只有一个参数,数值索引也可以省略成为c:_-ref的表示方式。

2、构造注入字面量

  1. <bean id="compactDisc" class="soundsystem.BlankDisc">
  2. <constructor-arg value="Sgt. Pepper's Lonely Hearts Club Band" />
  3. <constructor-arg value="The Beatles" />
  4. </bean>
  5.  
  6. <bean id="compactDisc" class="soundsystem.BlankDisc"
  7. c:_0="Sgt. Pepper's Lonely Hearts Club Band"
  8. c:_1="The Beatles" />

constructor-arg标签和c-名称空间的字面量注入.xml

3、构造注入装配集合

  1. <bean id="compactDisc" class="soundsystem.collections.BlankDisc">
  2. <constructor-arg value="Sgt. Pepper's Lonely Hearts Club Band" />
  3. <constructor-arg value="The Beatles" />
  4. <constructor-arg>
  5. <list>
  6. <value>Sgt. Pepper's Lonely Hearts Club Band</value>
  7. <value>With a Little Help from My Friends</value>
  8. <value>Lucy in the Sky with Diamonds</value>
  9. <value>Getting Better</value>
  10. <value>Fixing a Hole</value>
  11. <value>She's Leaving Home</value>
  12. <value>Being for the Benefit of Mr. Kite!</value>
  13. <value>Within You Without You</value>
  14. <value>When I'm Sixty-Four</value>
  15. <value>Lovely Rita</value>
  16. <value>Good Morning Good Morning</value>
  17. <value>Sgt. Pepper's Lonely Hearts Club Band (Reprise)</value>
  18. <value>A Day in the Life</value>
  19. </list>
  20. </constructor-arg>
  21. </bean>
  22.  
  23. <constructor-arg>
  24. <list>
  25. <ref bean="sgtPeppers" />
  26. ...
  27. </list>
  28. </constructor-arg>

构造注入字面值和引用值集合.xml

2.4.2、设置属性

1、使用Spring XML实现属性注入的两种方式:<property>元素和p-命名空间。

  1. <bean id="cdPlayer" class="soundsystem.properties.CDPlayer">
  2. <property name="compactDisc" ref="compactDisc" />
  3. </bean>
  4.  
  5. <bean id="cdPlayer"
  6. class="soundsystem.properties.CDPlayer"
  7. p:compactDisc-ref="compactDisc" />

property标签和p-名称空间属性注入

<property>元素为属性的Setter方法所提供的功能与<constructor-arg>元素为构造器所提供的功能是一样的。上面,它将ID为compactDisc的bean注入到compactDisc属性中(通过setCompactDisc()方法)。Spring提供了p-命名空间,作为<property>元素的替代方案。在XML文件中与其他的命名空间一起对其进行声明,启用p-命名空间。

2、属性注入字面量和装配注入集合

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.  
  7. <bean id="compactDisc"
  8. class="soundsystem.properties.BlankDisc"
  9. p:title="Sgt. Pepper's Lonely Hearts Club Band"
  10. p:artist="The Beatles">
  11. <property name="tracks">
  12. <list>
  13. <value>Sgt. Pepper's Lonely Hearts Club Band</value>
  14. <value>With a Little Help from My Friends</value>
  15. <value>Lucy in the Sky with Diamonds</value>
  16. <value>Getting Better</value>
  17. <value>Fixing a Hole</value>
  18. <value>She's Leaving Home</value>
  19. <value>Being for the Benefit of Mr. Kite!</value>
  20. <value>Within You Without You</value>
  21. <value>When I'm Sixty-Four</value>
  22. <value>Lovely Rita</value>
  23. <value>Good Morning Good Morning</value>
  24. <value>Sgt. Pepper's Lonely Hearts Club Band (Reprise)</value>
  25. <value>A Day in the Life</value>
  26. </list>
  27. </property>
  28. </bean>
  29.  
  30. <bean id="cdPlayer"
  31. class="soundsystem.properties.CDPlayer"
  32. p:compactDisc-ref="compactDisc" />
  33.  
  34. </beans>

属性注入字面量和集合

3、util-命名空间

名称空间不能用来装配集合,不能指定一个值(或bean引用)的列表。但是,我们可以使用Spring util-命名空间中的一些功能来简化BlankDisc bean。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:util="http://www.springframework.org/schema/util"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/util
  9. http://www.springframework.org/schema/util/spring-util.xsd">
  10.  
  11. <bean id="compactDisc"
  12. class="soundsystem.properties.BlankDisc"
  13. p:title="Sgt. Pepper's Lonely Hearts Club Band"
  14. p:artist="The Beatles"
  15. p:tracks-ref="trackList" />
  16.  
  17. <util:list id="trackList">
  18. <value>Sgt. Pepper's Lonely Hearts Club Band</value>
  19. <value>With a Little Help from My Friends</value>
  20. <value>Lucy in the Sky with Diamonds</value>
  21. <value>Getting Better</value>
  22. <value>Fixing a Hole</value>
  23. <value>She's Leaving Home</value>
  24. <value>Being for the Benefit of Mr. Kite!</value>
  25. <value>Within You Without You</value>
  26. <value>When I'm Sixty-Four</value>
  27. <value>Lovely Rita</value>
  28. <value>Good Morning Good Morning</value>
  29. <value>Sgt. Pepper's Lonely Hearts Club Band (Reprise)</value>
  30. <value>A Day in the Life</value>
  31. </util:list>
  32.  
  33. <bean id="cdPlayer"
  34. class="soundsystem.properties.CDPlayer"
  35. p:compactDisc-ref="compactDisc" />
  36.  
  37. </beans>

util-List简化bean

<util:list>只是util-命名空间中的多个元素之一。 下表列出了util-命名空间提供的所有元素。

Spring util-命名空间中的元素
元素 描述
util:constant 引用某个类型的public static域,并将其暴露为bean
util:list 创建一个java.util.List类型的bean,其中包含值或引用
util:map 创建一个java.util.Map类型的bean,其中包含值或引用
util:properties 创建一个java.util.Properties类型的bean
util:property-path 引用一个bean的属性(或内嵌属性),并将其暴露为bean
util:set 创建一个java.util.Set类型的bean,其中包含值或引用

2.5、导入和混合配置

JavaConfig中引用XML配置

创建一个高级别的SoundSystemConfig,在这个类中使用@Import将两个配置类组合在一起:@Import({CDPlayerConfig.class, CDConfig.class})。或一个配置类,一个xml:

  1. package soundsystem;
  2.  
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.context.annotation.Import;
  5. import org.springframework.context.annotation.ImportResource;
  6.  
  7. @Configuration
  8. @Import(CDPlayerConfig.class)
  9. @ImportResource("classpath:cd-config.xml")
  10. public class SoundSystemConfig {
  11.  
  12. }

Import和ImportResource引入配置

XML中引用JavaConfig:用<bean>

  1. package soundsystem;
  2.  
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5.  
  6. @Configuration
  7. public class CDConfig {
  8. @Bean
  9. public CompactDisc compactDisc() {
  10. return new SgtPeppers();
  11. }
  12. }

在soundsystem包下的CDConfig

把CDConfig中的bean元素引入到XML中

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:c="http://www.springframework.org/schema/c"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.  
  7. <bean class="soundsystem.CDConfig" />
  8.  
  9. <bean id="cdPlayer"
  10. class="soundsystem.CDPlayer"
  11. c:cd-ref="compactDisc" />
  12.  
  13. </beans>

在cdplayer-config.xml中引入JavaConfig中定义的bean

也可以创建一个更高层次的配置文件,这个文件不声明任何的bean,只是负责将两个或更多的配置组合起来。可以将CDConfig bean从之前的XML文件中移除掉,而是使用第三个配置文件将这两个组合在一起:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:c="http://www.springframework.org/schema/c"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.  
  7. <bean class="soundsystem.CDConfig" />
  8.  
  9. <import resource="cdplayer-config.xml" />
  10.  
  11. </beans>

root configuration根配置

3、高级装配

3.1、环境和profile

3.1.1、配置profile bean

在开发的时候,基于开发、测试、预发、线上等不同环境,会有某些对应的东西是不能通用的。例如数据库配置、加密算法以及与外部系统的集成在跨环境部署时会发生变化。

使用@Profile注解指定某个bean属于哪一个profile,@Profile("dev")此注解在Spring3.1里是只能加在类上,要分别对对不同的环境配置不同的类(类中的bean随环境的激活生成)。到了Spring3.2时,@Profile注解可以加在方法上,和@Bean注解一起使用,这样能将多个bean的声明放到同一个配置类中。

  1. package com.myapp;
  2.  
  3. import javax.sql.DataSource;
  4.  
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.context.annotation.Profile;
  8. import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
  9. import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
  10. import org.springframework.jndi.JndiObjectFactoryBean;
  11.  
  12. @Configuration
  13. public class DataSourceConfig {
  14.  
  15. @Bean(destroyMethod = "shutdown")
  16. @Profile("dev")
  17. public DataSource embeddedDataSource() {
  18. return new EmbeddedDatabaseBuilder()
  19. .setType(EmbeddedDatabaseType.H2)
  20. .addScript("classpath:schema.sql")
  21. .addScript("classpath:test-data.sql")
  22. .build();
  23. }
  24.  
  25. @Bean
  26. @Profile("prod")
  27. public DataSource jndiDataSource() {
  28. JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
  29. jndiObjectFactoryBean.setJndiName("jdbc/myDS");
  30. jndiObjectFactoryBean.setResourceRef(true);
  31. jndiObjectFactoryBean.setProxyInterface(javax.sql.DataSource.class);
  32. return (DataSource) jndiObjectFactoryBean.getObject();
  33. }
  34.  
  35. }

DataSourceConfig.java

每个DataSource bean都被声明在一个profile中,并且只有当规定的profile激活时,相应的bean才会被创建,但是可能会有其他的bean并没有声明在一个给定的profile范围内。没有指定profile的bean始终都会被创建,与激活哪个profile没有关系。

多个profile的xml配置方式

3.1.2、激活profile

Spring在确定哪个profile处于激活状态时,需要依赖两个独立的属性:spring.profiles.active和spring.profiles.default。如果设置了spring.profiles.active属性的话,那么它的值就会用来确定哪个profile是激活的。但如果没有设置spring.profiles.active属性(优先于default)的话,那Spring将会查找spring.profiles.default的值。如果spring.profiles.active和spring.profiles.default均没有设置的话,那就没有激活的profile,因此只会创建那些没有定义在profile中的bean。

有多种方式来设置这两个属性:
  作为DispatcherServlet的初始化参数;
  作为Web应用的上下文参数;
  作为JNDI条目;
  作为环境变量;
  作为JVM的系统属性;
  在集成测试类上,使用@ActiveProfiles注解设置。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  7. <context-param>
  8. <param-name>contextConfigLocation</param-name>
  9. <param-value>/WEB-INF/spring/root-context.xml</param-value>
  10. </context-param>
  11.  
  12. <context-param>
  13. <param-name>spring.profiles.default</param-name>
  14. <param-value>dev</param-vaule>
  15. </context-param>
  16.  
  17. <listener>
  18. <listener-class>
  19. org.springframework.web.context.ContextLoaderListener
  20. </listener-class>
  21.  
  22. <servlet>
  23. <servlet-name>appServlet</servlet-name>
  24. <servlet-class>
  25. org.springframework.web.servlet.DispatcherServlet
  26. </servlet-class>
  27. <init-param>
  28. <param-name>spring.profiles.default</param-name>
  29. <param-value>dev</param-value>
  30. </init-param>
  31. <load-on-startup>1</load-on-startup>
  32. </servlet>
  33.  
  34. <servlet-mapping>
  35. <servlet-name>appServlet</servlet-name>
  36. <url-pattern>/</url-pattern>
  37. </servlet-mapping>
  38.  
  39. </web-app>

在Web应用的web.xml文件中设置默认的profile

使用profile进行测试

Spring提供了@ActiveProfiles注解,我们可以使用它来指定运行测试时要激活哪个profile。在集成测试时,通常想要激活的是开发环境的profile。例如,下面的测试类片段展现了使用@ActiveProfiles激活dev profile:

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration(classes={PersistenceTestConfig.class})
  3. @ActiveProfiles("dev")
  4. public class PersistenceTest {
  5. ...
  6. }

3.2 条件化的bean

若希望一个或多个bean只有在应用的类路径下包含特定的库时才创建。或希望某个bean只有当另外某个特定的bean也声明了之后才会创建。或要求只有某个特定的环境变量设置之后,才会创建某个bean。在Spring 4之前,很难实现这种级别的条件化配置,但是Spring 4引入了一个新的@Conditional注解,它可以用到带有@Bean注解的方法上。如果给定的条件计算结果为true,就会创建这个bean,否则的话,这个bean会被忽略。

  1. package com.habuma.restfun;
  2.  
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Conditional;
  5. import org.springframework.context.annotation.Configuration;
  6.  
  7. @Configuration
  8. public class MagicConfig {
  9.  
  10. @Bean
  11. @Conditional(MagicExistsCondition.class)
  12. public MagicBean magicBean() {
  13. return new MagicBean();
  14. }
  15.  
  16. }

MagicConfig

@Conditional中给定了一个Class,它指明了条件,即matches()方法的返回结果。在本例中,也就是MagicExistsCondition。@Conditional将会通过Condition接口进行条件对比,设置给@Conditional的类可以是任意实现了Condition接口的类型。

  1. package com.habuma.restfun;
  2.  
  3. import org.springframework.context.annotation.Condition;
  4. import org.springframework.context.annotation.ConditionContext;
  5. import org.springframework.core.env.Environment;
  6. import org.springframework.core.type.AnnotatedTypeMetadata;
  7.  
  8. public class MagicExistsCondition implements Condition {
  9.  
  10. @Override
  11. public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
  12. Environment env = context.getEnvironment();
  13. return env.containsProperty("magic");
  14. }
  15.  
  16. }

MagicExistsCondition

MagicExistsCondition中只是使用了ConditionContext得到的Environment,但Condition实现的考量因素可能会比这更多。matches()方法会得到ConditionContext和AnnotatedTypeMetadata对象用来做出决策。ConditionContext是一个接口,通过ConditionContext,我们可以做到如下几点:

借助getRegistry()返回的BeanDefinitionRegistry检查bean定义;
借助getBeanFactory()返回的ConfigurableListableBeanFactory检查bean是否存在,甚至探查bean的属性;
借助getEnvironment()返回的Environment检查环境变量是否存在以及它的值是什么;
读取并探查getResourceLoader()返回的ResourceLoader所加载的资源;
借助getClassLoader()返回的ClassLoader加载并检查类是否存在。

AnnotatedTypeMetadata则能够让我们检查带有@Bean注解的方法上还有什么其他的注解。像ConditionContext一样,AnnotatedTypeMetadata也是一个接口。借助isAnnotated()方法,我们能够判断带有@Bean注解的方法是不是还有其他特定的注解。借助其他的那些方法,我们能够检查@Bean注解的方法上其他注解的属性。

从Spring 4开始,@Profile注解进行了重构,使其基于@Conditional和Condition实现。

3.3、自动处理装配的歧义性

3.3.1、标示首选的bean

  1. @Component
  2. @Primary
  3. public class IceCream implements Dessert {...}
  4.  
  5. @Bean
  6. @Primary
  7. public Dessert iceCream() { return new IceCream(); }
  8.  
  9. <bean id="iceCream"
  10. class="com.desserteater.IceCream"
  11. primary="true" />

组件扫描、java配置和xml配置标示首选bean

不管采用什么方式来标示首选bean,效果都是一样的,都是告诉Spring在遇到歧义性的时候要选择首选的bean。但是,如果标示了两个或更多的首选bean,那么它就无法正常工作了。就得采用限定自动装配的bean的方式。

3.3.2、限定自动装配的bean

  1. @Autowored
  2. @Qualifier("iceCream")
  3. public void setDessert (Dessert dessert) {
  4. this.dessert = dessert;
  5. }

如果没有指定其他的限定符的话,所有的bean都会给定一个默认的限定符,这个限定符与bean的ID相同。基于默认的bean ID作为限定符是非常简单的,但这有可能会引入一些问题。如果你重构了IceCream类,自动装配会失败。

可以为bean设置自己的限定符,而不是依赖于将bean ID作为限定符。在这里所需要做的就是在bean声明上添加@Qualifier注解。例如,它可以与@Component组合使用或与@Bean组合使用。

  1. @Component
  2. @Qualifier("cold")
  3. public class IceCream implements Dessert {...}
  4.  
  5. @Bean
  6. @Qualifier("cold")
  7. public Dessert iceCream() {
  8. return new IceCream();
  9. }
  10.  
  11. /**使用*/
  12. @Autowired
  13. @Qualifier("cold")
  14. public void setDessert(Dessert dessert) {
  15. this.dessert = dessert;
  16. }

为bean设置自定义限定符

若要给bean添加多限定符,通过添加多个@Qualifier()注解的方式,不被允许。单是可以创建新的注解来完成。

  1. /**自定义限定注解*/
  2. @Target({ElementType.CONSTRUCTOR, ElementType.FIELD,
  3. ElementType.METHOD, ElementType.TYPE})
  4. @Retention(RetentionPolicy.RUNTIME)
  5. @Qualifier
  6. public @interface Cold {}
  7.  
  8. /**自定义限定注解*/
  9. @Target({ElementType.CONSTRUCTOR, ElementType.FIELD,
  10. ElementType.METHOD, ElementType.TYPE})
  11. @Retention(RetentionPolicy.RUNTIME)
  12. @Qualifier
  13. public @interface Creamy {}
  14.  
  15. /**使用,bean*/
  16. @Component
  17. @Cold
  18. @Creamy
  19. public class IceCream implements Dessert {...}
  20.  
  21. /**使用,注入点*/
  22. @Autowired
  23. @Cold
  24. @Creamy
  25. public void setDessert(Dessert dessert) {
  26. this.dessert = dessert;
  27. }

自定义限定符注解

3.4、bean的作用域

Spring定义了多种作用域,可以基于这些作用域创建bean,包括:
单例(Singleton):在整个应用中,只创建bean的一个实例。
原型(Prototype):每次注入或者通过Spring应用上下文获取的时候,都会创建一个新的bean实例。
会话(Session):在Web应用中,为每个会话创建一个bean实例。
请求(Rquest):在Web应用中,为每个请求创建一个bean实例。

单例是默认的作用域。如果选择其他的作用域,要使用@Scope注解。

  1. @Component
  2. @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
  3. public class Notepad {...}
  4.  
  5. @Bean
  6. @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
  7. public Notepad notepad {
  8. return new Notepad();
  9. }
  10.  
  11. <bean id="notpad"
  12. class="com.myapp.Notepad"
  13. scope="prototype" />

自动扫描、java配置和xml配置定义bean作用域

3.4.1、使用Session和Rquest域

以购物车bean来说,会话作用域是最为合适的,因为它与给定的用户关联性最大。要指定会话作用域,我们可以使用@Scope注解,它的使用方式与指定原型作用域是相同的。

  1. @Component
  2. @Scope(value=WebApplicationContext.SCOPE_SESSION,
  3. proxyMode=ScopedProxyMode.INTERFACES)
  4. public ShoppingCart cart() {...}

@Scope同时还有一个proxyMode属性,它被设置成了ScopedProxyMode.INTERFACES。这个属性解决了将会话或请求作用域的bean注入到单例bean中所遇到的问题。

proxyMode所解决问题的场景。
  假设我们要将ShoppingCart bean注入到单例StoreService bean的Setter方法中。因为StoreService是一个单例的bean,会在Spring应用上下文加载的时候创建。当它创建的时候,Spring会试图将ShoppingCart bean注入到setShoppingCart()方法中。但是ShoppingCart bean是会话作用域的,此时并不存在。直到某个用户进入系统,创建了会话之后,才会出现ShoppingCart实例。
  另外,系统中将会有多个ShoppingCart实例:每个用户一个。我们并不想让Spring注入某个固定的ShoppingCart实例到StoreService中。我们希望的是当StoreService处理购物车功能时,它所使用的ShoppingCart实例恰好是当前会话所对应的那一个。
  Spring并不会将实际的ShoppingCart bean注入到StoreService中,Spring会注入一个ShoppingCart bean的代理。这个代理会暴露与ShoppingCart相同的方法,所以StoreService会认为它就是一个购物车。但是,当StoreService调用ShoppingCart的方法时,代理会对其进行懒解析并将调用委托给会话作用域内真正的ShoppingCart bean。

  现在,我们带着对这个作用域的理解,讨论一下proxyMode属性。如配置所示,proxyMode属性被设置成了ScopedProxyMode.INTERFACES,这表明这个代理要实现ShoppingCart接口,并将调用委托给实现bean。
  如果ShoppingCart是接口而不是类的话,这是可以的(也是最为理想的代理模式)。但如果ShoppingCart是一个具体的类的话,Spring就没有办法创建基于接口的代理了。此时,它必须使用CGLib来生成基于类的代理。所以,如果bean类型是具体类的话,我们必须要将proxyMode属性设置为ScopedProxyMode.TARGET_CLASS,以此来表明要以生成目标类扩展的方式创建代理。
  尽管我主要关注了会话作用域,但是请求作用域的bean会面临相同的装配问题。因此,请求作用域的bean应该也以作用域代理的方式进行注入。

3.4.2、在XML中声明作用域代理

使用<aop:scoped-proxy>元素,在XML配置中声明Spring的aop命名空间。<aop:scoped-proxy>是与@Scope注解的proxyMode属性功能相同的Spring XML配置元素。它会告诉Spring为bean创建一个作用域代理。默认情况下,它会使用CGLib创建目标类的代理。但是我们也可以将proxy-target-class属性设置为false,进而要求它生成基于接口的代理。

  1. <bean id="cart" class="com.myapp.ShoppingCart" scope="session" />
  2. <aop:scoped-proxy proxy-target-class="false" />
  3. </bean>

3.5、运行时值注入

避免硬编码,Spring提供了两种在运行时求值的方式:
属性占位符(Property placeholder)。
Spring表达式语言(SpEL)。

3.5.1、注入外部的值

在Spring中,处理外部值的最简单方式就是声明属性源并通过Spring的Environment来检索属性。@PropertySource引用的路径,申明了属性来源。

  1. package com.soundsystem;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.context.annotation.PropertySource;
  7. import org.springframework.core.env.Environment;
  8.  
  9. @Configuration
  10. @PropertySource("classpath:/com/soundsystem/app.properties")
  11. public class EnvironmentConfig {
  12.  
  13. @Autowired
  14. Environment env;
  15.  
  16. @Bean
  17. public BlankDisc blankDisc() {
  18. return new BlankDisc(
  19. env.getProperty("disc.title"),
  20. env.getProperty("disc.artist"));
  21. }
  22.  
  23. }

EnvironmentConfig.java

Environment的常用方法:
String getProperty(String key)  // 返回String类型的值
String getProperty(String key, String defaultValue)  // 返回String类型,env没有值返回默认值
<T> T getProperty(String key, Class<T> targetType)  // 返回指定类型的值
<T> T getProperty(String key, Class<T> targetType, T defaultValue)
String getRequiredProperty(String key) throws java.lang.IllegalStateException  // 属性必须定义,没有会抛异常
boolean containsProperty(java.lang.String key)  //检查属性是否存在
<T> T getRequiredProperty(String key, Class<T> targetType)
boolean acceptsProfiles(String... profiles)  // 如果environment支持给定profile的话,就返回true
String[] getActiveProfiles()  // 返回激活profile名称的数组
String[] getDefaultProfiles()  // 返回默认profile名称的数组

在JavaConfig中装配使用Environment检索属性方便,但采用属性占位符能支持XML装配和组件扫描自动装配。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:c="http://www.springframework.org/schema/c"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
  8.  
  9. <context:property-placeholder
  10. location="com/soundsystem/app.properties" />
  11.  
  12. <bean class="com.soundsystem.BlankDisc"
  13. c:_0 = "${disc.title}"
  14. c:_1 = "${disc.artist}"/>
  15.  
  16. </beans>

属性占位符的XML装配

  1. // 解析占位符的配置bean
  2. @Bean
  3. public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
  4. return new PropertySourcesPlaceholderConfigurer();
  5. }
  6.  
  7. public BlackDisc(
  8. @Value("${disc.title}") String title,
  9. @Value("${disc.artist}") String artist) {
  10. this.title = title;
  11. this.artist = artist;
  12. }

属性占位符的组件扫描自动装配

  为了使用占位符,我们必须要配置一个PropertyPlaceholderConfigurer bean或PropertySourcesPlaceholderConfigurer bean。从Spring3.1开始,推荐使用PropertySourcesPlaceholderConfigurer,因为它能够基于Spring Environment及其属性源来解析占位符。
  使用XML配置的话,Spring context命名空间中的<context:propertyplaceholder>元素将会生成PropertySourcesPlaceholderConfigurer bean。
  解析外部属性能够将值的处理推迟到运行时,但是它的关注点在于根据名称解析来自于Spring Environment和属性源的属性。而Spring表达式语言提供了一种更通用的方式在运行时计算所要注入的值。

3.5.2、使用Spring表达式语言进行装配

SpEL表达式要放到“#{ ... }”之中,属性占位符需要放到“${ ... }”之中。
SpE特性,包括:
  使用bean的ID来引用bean;
  调用方法和访问对象的属性;
  对值进行算术、关系和逻辑运算;
  正则表达式匹配;
  集合操作。

bean装配的时候使用表达式
通过组件扫描创建bean
public BlackDisc(
@Value("#{systemProperties['disc.title']}") String title,
@Value("#{systemProperties['disc.artist']}") String artist) {
  this.title = title;
  this.artist = artist;
}
XML配置
将SpEL表达式传入<property>或<constructor-arg>的value属性中,或者将其作为p-命名空间或c-命名空间条目的值。
<bean id="sgtPeppers"
   class="com.soundsystem.BlankDisc"
   c:_title = "#{systemProperties['disc.title']}"
   c:_artist = "#{systemProperties['disc.artist']}" />

SpEL表达式
#{1}      常量数字
#{3.14}    浮点值
#{9.87E4}  科学计数法
#{'Hello'}    String类型
#{false}   Boolean类型
#{sgtPeppers}         通过ID引用其他的bean
#{sgtPeppers.artist}        引用bean的属性
#{sgtPeppers.selectArtist()}    调用bean的方法
#{T(System).currentTimeMillis()}   T(),会将System视为Java中对应的类型
#{SystemProperties['disc.title']}      通过systemProperties对象引用系统属性
#{artistSelector.selectArtist()?.toUpperCase()}   使用了“?.”运算符,能确保在访问右边之前,对应元素不是null。是null表达式返回null。
T()运算符的结果是一个Class对象,可以将其装配到一个Class类型的bean属性中。但是T()运算符的真正价值在于它能够访问目标类型的静态方法(T(java.lang.Math).random())和常量(T(java.lang.Math).PI)。

SpEL运算符

运算符类型 运 算 符
算术运算 +、-、 * 、/、%、^
比较运算 < 、 > 、 == 、 <= 、 >= 、 lt 、 gt 、 eq 、 le 、 ge
逻辑运算 and 、 or 、 not 、│
条件运算 ?: (ternary) 、 ?: (Elvis)
正则表达式 matches
集合运算符 查询.?[]、查询第一个匹配项.^[]、、查询最后一个匹配项.$[]、投影.![]

Spring的核心的更多相关文章

  1. spring IOC核心原理

    下面来了解一下Spring到底是怎么运行的. public static void main(String[] args) { ApplicationContext context = new Fil ...

  2. spring技术核心概念纪要

    一.背景 springframework 从最初的2.5版本发展至今,期间已经发生了非常多的修正及优化.许多新特性及模块的出现,使得整个框架体系显得越趋庞大,同时也带来了学习及理解上的困难. 本文阐述 ...

  3. spring三大核心学习(一)---控制反转

    记得当年大学时候,java的企业级框架还是ssh的天下(spring,struts和hibernate),但是现在,感觉spring已经完全把那两个框架甩在后边了.用spring的人越来越多,用str ...

  4. Spring最核心的功能是什么?使用Spring框架的最核心的原因是什么?

    quote:Spring最核心的功能是什么?使用Spring框架的最核心的原因是什么? (IT公司面试手册,可以多看看) spring 框架中核心组件有三个:Core.Context 和 Beans. ...

  5. JAVA WEB快速入门之通过一个简单的Spring项目了解Spring的核心(AOP、IOC)

    接上篇<JAVA WEB快速入门之从编写一个JSP WEB网站了解JSP WEB网站的基本结构.调试.部署>,通过一个简单的JSP WEB网站了解了JAVA WEB相关的知识,比如:Ser ...

  6. Spring系列(一) Spring的核心

    Spring 简介 Spring 是一个开源轻量级企业应用架构,目的是为了简化企业级应用开发.(1)Spring 框架可以帮我们管理对象的生命周期,帮助我们管理对象间的依赖关系,相互协作:(2)Spr ...

  7. Spring的核心之IoC容器创建对象

    Spring的Ioc容器,是Spring的核心内容: 作用:对象的创建和处理对象的依赖关系. Spring容器创建对象有以下几种方式: 1:调用无参数的构造器 <!-- 默认无参的构造器 --& ...

  8. 7 -- Spring的基本用法 -- 3... Spring 的核心机制 : 依赖注入

    7.3 Spring 的核心机制 : 依赖注入 Spring 框架的核心功能有两个. Spring容器作为超级大工厂,负责创建.管理所有的Java对象,这些Java对象被称为Bean. Spring容 ...

  9. Spring之核心容器bean

    摘要:Spring的核心容器实现了Ioc,其目 的是提供一种无侵入式的框架.在本文中,首先讲解了Spring的基础bean的相关知识,然后介绍了Spring是如何对bean进行管理的. 在Spring ...

  10. 一头扎进Spring之---------Spring七大核心模块

    Spring七大核心模块 核心容器(Spring Core) 核心容器提供Spring框架的基本功能.Spring以bean的方式组织和管理Java应用中的各个组件及其关系.Spring使用BeanF ...

随机推荐

  1. Oracle Profile 配置文件

    Profile是用户的配置文件,它是密码限制,资源限制的命名集合.利用profile 可以对数据库用户进行基本的资源管理,密码管理. 1 创建profile 的语法 create profile pr ...

  2. 微信小程序登录授权并获取手机号

    一.请求发送 携带 code 到后台换取 openid var that = this; wx.login({ success(res) { console.log(res); var code = ...

  3. Keras和tensorflow的区别

    参考: https://blog.csdn.net/zhangbaoanhadoop/article/details/82111056

  4. hive JDBC客户端启动

    JDBC客户端操作步骤

  5. PLA-1

    PLA(Principal Component Analysis)主成分分析: 思路1:坐标旋转 1.数据分布以及投影: 2.特征值以及特征向量 思路2: 未完待续...

  6. 用 Python 写一个多进程兼容的 TimedRotatingFileHandler

    我前面有篇文章已经详细介绍了一下 Python 的日志模块.Python 提供了非常多的可以运用在各种不同场景的 Log Handler. TimedRotatingFileHandler 是 Pyt ...

  7. 猜数字游戏 在控制台运行--java详解!了;来玩

    import java.util.Scanner;//导入包 import java.util.Scanner; 注意格式 符号的使用 public class Demo{ //猜数字游戏 练习 pu ...

  8. Appium之开发环境搭建

    1.下载Appium 去官方网站下载http://appium.io/# : 本次以appium-desktop-setup-1.8.0.exe 文件为例,使用桌面版就不再需要下载server版本: ...

  9. 当使用cokie进行数据交互时候,cookie只需存储该对象的id即可不需要存放其他数据;只需在写个接口根据cookie里面的对象id来创建对象

    当使用cokie进行数据交互时候,cookie只需存储该对象的id即可不需要存放其他数据:只需在写个接口根据cookie里面的对象id来创建对象

  10. 训练赛-Eyad and Math

    题意:给你四个数,求出a^b是否小于c^d,是的话输出<,否则输出>; 思路:因为数据很大,所以我们需要降低数据的规模,比如用一个log10()函数,这就能解决了,注意,要用scanf输入 ...