新近接触了SpringMVC3.0,从个人感觉来讲我非常喜欢。之所以从3.0版本才开始学习,也是对于旧版本的XML配置有所忌惮。SpringMVC从2.5版本引入了annotation,到了3.0有了进一步增强。
参考了文章http://bulargy.iteye.com/blog/179781的一些观点,我也觉得有时候Annotation较之于XML-configuration有其固有的限制。混搭比较好,比如说想在发布环境上动态修改接口的具体实现,但是不想重新编译Java代码的情况下。
为了同时能够利用到XML和Annotation的优点,我做了一个试验,想验证当我同时在XML和Annotation中声明了同样id的一个bean组件,spring会优先为我找哪一个?
在我的试验中,有一个接口,以及两个实现类,分别如下:
public interface CircularReferenceService {public Set<Long> getAncestorTrxIDs(Long linkStartFrom); ... ...
@Service("circularCheckingService")
@Transactional(readOnly=true)
public class CircularReferenceServiceImpl implements CircularReferenceService {private GenericDao<TrxDef, Long> trxDefDao; private GenericDao<TrxDefLink, Long> trxDefLinksDao; @Autowired public void setDao(GenericDao<TrxDef, Long> trxDefDao, GenericDao<TrxDefLink, Long> trxDefLinksDao) { this.trxDefDao = trxDefDao; this.trxDefLinksDao = trxDefLinksDao; } public List<TrxDef> getAllLinkableTrxDef(Long linkStartFrom) { Set<Long> excludeIDs = getAncestorTrxIDs(linkStartFrom); if(linkStartFrom != null) { ... ...
@Service("circularCheckingOracleNativeService")
@Transactional(readOnly=true)
public class CircularReferenceServiceOracleNativeImpl implements CircularReferenceService {private NativeDao nativeDao; @Autowired public void setDao(NativeDao nativeDao) { this.nativeDao = nativeDao; } public List<TrxDef> getAllLinkableTrxDef(Long linkStartFrom) { ... ...
......
@Resource(name="circularCheckingService")
public void setCircularCheckingService(CircularReferenceService circularService) {
this.circularService = circularService;
}
......
<bean id="circularCheckingService"
class="foo.bar.CircularReferenceServiceOracleNativeImpl" />
Spring和EJB3的规则是只有注解的时候用注解配置。注解和XML同时存在的时候,以XML为准。
先注释, 后xml