谈谈Spring 2.x中简化配置的问题

来源:互联网 发布:java速成班 编辑:程序博客网 时间:2024/05/29 03:18

谈谈Spring 2.x中简化配置的问题 Spring 2.x在配置文件的简化的方面做了很多工作,原来1.x中比较麻烦的配置都已经拥有了比较完美的解决方案。最近刚看完《精通Spring 2.x --企业应用开发精解》的书,结合自己的经验整理一下简化配置的内容。

一、关于集合的配置

1.List

>1.x版本的
 

>2.x版本的
 

2.Set

> 1.x
 

> 2.x
 

    3.Map

    > 1.x
     

    > 2.x
     

    4. Properties

    > 1.x
      

    > 2.x
        

    可以在一个属性文件中直接配置属性,这比较符合一般的项目习惯。

    二、 关于事务配置

    1.  1.x
      

      2.  2.x

      有两种新方法:

       a)使用@Transactional注解

      在需要的服务类或服务方法处直接打上@Transactional注解,然后在Spring配置文件中启用注解事务驱动就可以了:
       

      在Spring配置文件中相应添加上:
        

      这样就OK了,简单吧:)

      b)使用aop/tx

      如果你的Service服务类都很规范,我觉得使用aop/tx更方便,因为不用到处打注解,在一处集中配置就OK了,可谓运筹帷幄之中,决胜于千里之外:)
        

      三、关于AOP配置

      原来1.x的AOP太麻烦了,提都不想提,直接说一下2.x的AOP。 Spring 2.x使用@AspectJ来描述切面,由于@AspectJ的语法描述能力超强,因此在Spring 2.x中使用AOP真的非常方便。 在使用@AspectJ之前,首先你得保证你所使用的JDK的版本是5.0及以上版本,否则无法使用注解技术。 Spring在处理@Aspect注解表达式时,需要使用位于spring/lib/asm下asm关联类库,将该类库的三个类包加入到类路径中:asm-2.2.2.jarasm-commons-2.2.2.jarasm-util-2.2.2.jar。asm类库是轻量级的字节码处理框架,因为Java的反射机制无法获取入参名,Spring就利用asm处理@AspectJ中所描述的方法入参名。 此外,Spring采用AspectJ提供的@AspectJ注解类库及相应的解析类库,它位于spring/lib/aspectj目录下,将目录下的aspectjrt.jar和aspectjweaver.jar类包加入类路径中。 在做好上节中所提到的前置工作后,我们就可以开始编写一个基于@AspectJ的切面了,首先来看一个简单的例子,以便对@AspectJ有一个切身的认识。 @AspectJ采用不同的方式对AOP进行描述, 我们使用NaiveWaiter的例子来说明,这是一个希望引入切面的目标类:
        

        下面使用@AspectJ来定义一下切面:

         

          然后启动@AspectJ的注解切面驱动就可以了!
            

          四、关于Spring 2.1添加的新功能

          1.原来引入一个外面属性配置文件需要使用以下的方式:
            

          使用Spring 2.1后,你只需要象下面这样配置就可以了:
             

          2.注解驱动的Bean注入 大家看看这段E文就OK了

          <context:component-scan>: scans classpath using one or more "include"/"exclude" filters, and automatically registers beans In essence, this is a third way to define beans (1: classic xml, 2:javaconfig (code), 3:<context:component-scan>, matching on annotations or types) Default naming strategy is based on short classname of discovered bean @Component and @Repository annotations, when used, can optionally specify a bean name to use For filter type "annotation", the value of "expression" attribute should resolve to a Java annotation type For filter type "assignable", the value of "expression" attribute should resolve to a Java type For filter type "aspectj", the value of "expression" should be an "type expression" (in Pointcut language, perhaps it could be injected?) Relevant documentation can be found in preliminary spring 2.1 manual, sections 3.10 and 3.11 In addition, this last JIRA comment for http://www.jetbrains.net/jira/browse/IDEADEV-16886#action_163502 contains two links to articles showing example usage of <context:component-scan> <context:annotation-config> (described in 3.10 in spring 2.1 manual linked above): allows autowiring to be defined using @Resource or @Autowired annotations.

          五、关于Spring 2.5添加的新功能

          Spring 2.5继续对context命名空间进行了扩充,添加了好用而强大的context:load-time-weaver,可以让我们更方便地应用AspectJ。大家可以看TSS上的这篇文章,它全面讲解了Spring 2.5的新特性。 http://www.theserverside.com/tt/articles/article.tss?l=IntrotoSpring25

          注:以上大部分代码来直接引用自 《精通Spring 2.x--企业应用开发精解》

          原创粉丝点击