spring3.0使用annotation完全代替XML
@Service与@Component有什么不同?那天被问到这个问题,一时之间却想不起来,就利用这篇文章来纪录spring3.0中常用的annotation。
从spring2.5开始,annotation结合BeanPostProcessor成了扩展Spring IoC容器的常用方法。Spring2.5增加了对JSR-250中@Resource, @PostConstruct, @PreDestroy的支持,Spring 3.0又增加了对JSR-330 (Dependency Injection for Java)中 @Inject,@Qualifier, @Named, @Provider的支持。将相关的jsr jar包丢到classpath,并注册相应的BeanPostProcessor,其它的一切spring会帮你完成。spring还提供了一个简便的方法,通过在context的XML配置文件中加入:Xml代码
再回到文章开头的问题吧。spring从2.5开始加入了classpath scanning的功能,来代替之前在xml中定义Bean。首先在context的XML配置文件中加入:Xml代码
Java代码 @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface Service { String value() default ""; }
@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Componentpublic @interface Service {String value() default "";}
@Service本身就是被@Componet这个元注解(meta annotaion)标注的。因此对于这三个层的Bean, spring的文档也推荐将它们标注为@Service, @Controller与@Repository,因为这样更方便其它工具对这些特殊Bean的处理以及为它们加上相关AOP的 aspects,spring在后续版本的升级上也可能对它们增加更多的特殊语义。至于它们到底比普通的@Componet多了哪些 aspects,spring的文档上并没有详细说明,只简单地提到了对于@Repository,spring会自动加上exception translation,用于转化持久层抛出的异常。我google了一下,包括spring论坛的帖子,也没能找到这方面的详细信息,这点希望熟悉spring源代码的朋友能够说说。
现在通过classpath scanning以及@Component,@Autowired等annotation,我们已无须通过xml来定义Bean了。但还必须在application context的xml中添加
Java代码 @Configuration public class AppConfig { @Bean public GreetingDao greetingDao() { return new GreetingDao(); } } public class GreetingDao { public String getGreeting(){ return "Hi"; } }
@Configurationpublic class AppConfig {@Beanpublic GreetingDao greetingDao() {return new GreetingDao();}}
public class GreetingDao {public String getGreeting(){return "Hi";}}
@configuration表明这个类包含Bean的定义,@Bean在这里就相当于原先的配置:
@Testpublic void test_java_config(){AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);GreetingDao dao = ctx.getBean(GreetingDao.class);assertEquals("Hi", dao.getGreeting());}
注意AppConfig里面的greetingDao虽然是是直接new出来的,但它的默认scope仍是singleton,跟采用配置文件的情况是一样的,如果需要prototype,则可以另外为它加上@Scope("prototype")。上面采用的方法是手工将Bean定义在 @Configuration里面,我们同样可以让spring自动检测Bean,比如下面的GreetingSerivce:
Java代码 @Service public class GreetingService { public String sayHello(){ return "Hello"; } }
@Servicepublic class GreetingService {public String sayHello(){return "Hello";}}
将GreetingService标注为@Service,就不需要在AppConfig里面注册了:
Java代码 @Test public void test_service(){ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.scan("septem.demo"); ctx.refresh(); GreetingService service = ctx.getBean(GreetingService.class); assertEquals("Hello", service.sayHello()); }
@Test public void test_service(){AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();ctx.scan("septem.demo");ctx.refresh();
GreetingService service = ctx.getBean(GreetingService.class);assertEquals("Hello", service.sayHello());}
以上的ctx.scan("septem.demo")就相当于配置文件:
声明:希维路由器教程网提供的内容,仅供网友学习交流,如有侵权请与我们联系删除,谢谢。ihuangque@qq.com
本文地址:https://www.ctrlcv.com.cn/diannao/169347960610849.html