24
Spring
Calvin Xiao edited this page 2014-01-26 19:22:35 -08:00
This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

##1. Spring 4.0 Spring4.0是SpringSource从VMWare到了其子公司Pivotal后的第一个发行版本,但对于普通用户来说变化不大,近期的重头戏还是在Spring-Boot上。

  • 支持规范第三方类库的升级并deprecate了一些过时的功能。
  • 支持组合Annotation, 比如@RestController就组合了@Controller和@ReponseBody因此在每个直接返回内容而不是jsp路径的Restful方法里就不需要写@ReponseBody了。
  • 提供了一个Spring的pom文件只要每个项目都在dependencyManagement里import一次就保证所有spring子模块的版本一致不用再一个个穷举。dependencies里声明spring模块的时候也不用再写版本号。但缺点是import好像不能继承必须要每个项目都import一次。
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-framework-bom</artifactId>
				<version>${spring.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

##2.用Profile 多环境配置 Spring 3.1的功能以后就不用为了区分Test, Dev, Production环境搞几个只有细微区别的application.xml, application-test.xml及引用它们的web.xml了。

  1. 首先将applicationContext.xml中的namespace从3.0升级到3.1.xsd 然后就可以在文件末尾加入不同环境的定义比如不同的dataSource
<beans profile="test">
	<jdbc:embedded-database id="dataSource">
		<jdbc:script location="classpath:com/bank/config/sql/schema.sql"/>
	</jdbc:embedded-database>
</beans>
 
<beans profile="production">
	<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/datasource"/>
</beans>

2.在web.xml里你需要定义使用的profile最聪明的做法是定义成context-param注意这里定义的是default值,在非生产环境,可以用系统变量"spring.profiles.active"进行覆盖。

	<context-param>
    	    <param-name>spring.profiles.default</param-name>
    	    <param-value>production</param-value>
	</context-param>

3.在其他地方进行覆盖

3.1 在development和functional test启动Jetty前设置系统变量

	System.setProperty("spring.profiles.active", "development");
	server.start()

3.2 在用到ApplicationContext的单元测试用例中用 @ActiveProfiles定义

    @ContextConfiguration(locations = { "/applicationContext.xml" })
    @ActiveProfiles("test")
    public class AccountDaoTest extends SpringTxTestCase {
    }

在springside里有演示了production,developmenttest,functional三个环境 大家可以根据实际情况组合自己的环境管理。另外可以与Spring的properties文件加载时可顺位覆盖的特性(放一些不在版本管理中的xx.local.properties文件)更好的支持本地开发环境Jenkins上的functional test等其他环境。

##3.配置方式 Spring3.0的精力都在Java Config和Spring EL上但暂时没看到很适合使用的场景。

配置还是建议老规矩DaoManager, Controller, WebService这些类似的配置要求不高的Bean就用@Component标注并使用@Autowired注入依赖.其他独立性比较强的就用XML配置并在XML里显式的说明注入关系。

##4.注入配置? ###用@Autowired还是@Resource? @Autowired是Spring自己定义的按属性的Class注入 @Resource是JSR规范按属性的名称注入个人更喜欢用前者因为@Autowired的耦合度更低而且还有一个required=true/false的有用属性。

###在setter方法上注释还是直接在属性上注释? 有些情况还是需要提供setter方法比如要在XML里显式的配置注入比如要在单元测试用例里向待测试Bean注入Mock对象。除此之外都可以省略掉setter方法。 所以在SpringSide中Web层的POJO一般没有setter对象Business层的就会有。

##5.AOP陷阱

  • 一般用基于CGI-Lib的AOP<tx:annotation-driven proxy-target-class="true" /> 那如果用到<aop:aspectj-autoproxy/>也应该加上proxy-target-class="true"另外shiro的方法级AOP用到的DefaultAdvisorAutoProxyCreator也同样需要设置target-class属性为true。

  • Aspectj最好用1.7.x版

  • Spring AOP几种风格混杂Spring1.2系列的BeanNameAutoProxyCreatorDefaultAdvisorAutoProxyCreator系列Spring2.0时代支持@AspectJ annotation的定义方式值得注意是此时仍然是Spring AOP的runtime在Spring范围内干活而不是纯正AspectJ的runtime虽然也依赖了AspectJWeaver.jar。

##6.参考资料

  • Book:<Pro Spring 3>
  • Book: <Spring in Action 3rd Edition>
  • Book: <Spring Recipes 2nd Edition>