---
title: "IOC Annotations"
author: "yaofeng911"
date: "2010-05-12T14:54:01Z"
tags: ["Bean", "DAO", "IOC", "Spring", "配置管理"]
categories: ["企业架构"]
slug: "ioc-annotations"
draft: false
lastmod: "2010-05-12T15:03:11Z"
id: 664773
url: "https://yaofeng911.iteye.com/blog/664773"
---

**Spring2.5的新特性：**

[**http://www.infoq.com/cn/articles/spring-2.5-part-1**](http://www.infoq.com/cn/articles/spring-2.5-part-1)

[http://www.ibm.com/developerworks/cn/java/j-lo-spring25-ioc/](http://www.ibm.com/developerworks/cn/java/j-lo-spring25-ioc/)

 

**1.1： 实现零配置的适用场合**

 

IOC  Annotations 可以实现零配置，但也因为不用在applicationContext.xml中定义了，也就缺乏了灵活的，不修改代码的IOC手段，也无法做一些属性值的配置。所以，IOC annotation 常用于Service,Dao,Controller 这些依赖关系比较固定，约定俗成大于配置的地方。 而XML 配置文件则用于如Database/SessionFactory,Spring  Security这些需要配置的地方。

 

**1.2：applicationContext.xml的定义**

 

定义自动扫描Classpath路径，扫描带有@Component/@Service/@Repository的pojo加入applicationContext，

 并根据@Autoired/@Qualifier  与 @Resource进行IOC配置，并对@PostConstruct 和 @PreDestroy和@Requried进行处理

 

```
<context:component-scan base-package="org.springside.examples.miniweb" />
```

 

以下配置除不会扫描@Component/@Service/@Repository外，其他作用同上

 

```
<context:annotation-config />
```

 

@Component/@Service 和 @Repository

 

Spring扫描指定的classpath时，会含以上Annotation的类将加其加入ApplicaitonContext中。

 

    一般@Repository用于DAO层， 而@Service层用于Service层，@Component用于其他，三者在Spring当前版本并没有本质区别，建议只使用@Component。

 

Spring扫描指定的classpath时，会含以上Annotation的类将加其加入ApplicaitonContext中。

 

    一般@Repository用于DAO层， 而@Service层用于Service层，@Component用于其他，三者在Spring当前版本并没有本质区别，建议只使用@Component。

 

**1.3: @Autoired/@Qualifier  与 @Resource**

 

    @Autoried 与 @Resource都比较灵活，不只能声明于setter函数，还能声明于不按setter规则命名的函数上，以及 私有属性上（不建议使用，因为在单元测试时需要以反射注入)

  @Autoried 默认byType，如果需要byName，在变量上增加@Qualifier，另外@Autoried默认隐含了@Required，保证变量一定被注入，如果不需要，用@Autowired(required=false)取消。

  @Resource 默认byName，如果name找不到就会byType， 另可以用name与type属性设定。

 

**1.4: @Required**

  

      对于非@autowired标注的属性，声明@Required表明明属性必须被注入。

 

**1.5: 生命周期Annotation**

 

      JSR250的@PostConstruct 和 `@PreDestroy，比以往的实现接口或者在applicationContext.xml中配置init-method的方式更为方便和标准。` [{{@PostContruct](mailto:%7B%7B@PostContruct)在所有注入函数执行完毕后执行，而@PreDestroy则在JVM退出的ShutdonwHook中拦截，注意，ShutdownHook对kill -9这种暴烈的关闭无效。  
}}

 

**2.属性文件**

 

在配置文件中，很有一些属性会在团队的开发环境，个人的开发环境，集成测试环境，生产环境，甚至生产环境集群的每台服务器中都有所不同的。为了将这些易变的属性抽从applicationContext取出来，Spring 做了一些特殊的支持，

在SpringSide里配置如下:

 

```
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
		<property name="ignoreResourceNotFound" value="true" />
		<property name="locations">
			<list>
				<value>classpath:application.properties</value>
				<value>classpath:application.local.properties</value>
                                <value>file:/var/myapp/application.server.properties</value>
			</list>
		</property>
	</bean>
```

 

团队公用的开发环境配置在application.properties中，放入版本控制系统管理中。

  个人的开发环境，集成测试环境中与公共环境不相同的部分，放入application.local.properties中，会覆盖application.properties中的设置，由个人管理，不放入版本管理系统。

  在打包部署到生产环境时，可将生产环境每台服务器的特殊属性可放在服务器的某个固定目录，如/var/myapplicaiton/application.server.properties中(而不放在项目jar内)，   每台服务器的配置文件分开不同服务器目录放在版本管理系统中。也可以在启动命令中加入参数来覆盖，如-Djdbc.username=aaa。

 

**3: 事务**

 

    因为不喜欢在配置文件里用AspectJ定义事务，宁愿使用Spring的@Transactional annotation在service层上进行标注。

    对于只读的方法，可以加入@Transactional (readOnly=true) 标注以提高性能。

    因为Spring默认只对RuntimeException进行rollback，所以：

    1.所有在service层方法中用throws定义的checkedException，都必须在@Transactional中定义rollbackclass。

    2.所有在service层方法中catch处理了的Exception，又希望Spring辅助rollback的话，必须重抛一个RunTimeException，SpringSide里预定义了一个统一的ServiceException。

   可特别配置事务类型，见

 

[JavaEE事务资料](/display/calvin/Transaction "Transaction") 六种事务模型与四种事务隔离级别介绍。

 

转自: [http://wiki.springside.org.cn/display/calvin/Spring+Notes](http://wiki.springside.org.cn/display/calvin/Spring+Notes)