2008-03-17
体验一下Spring2.5 Annotation-based-configration
关键字: spring annotation
Spring2.5 Annotation-based-configration大大简化了配置,用一个经典的HelloWorld程序来体验一下:
我们只要在bean面前使用@Component注解就可以被Spring的IOC容器管理了.
在看看怎么把MessageProvider注入到MessageRender里面的:
使用@Autowired注解,Spring就可以把messageProvider注入到MessageRender里面了,
默认是通过byType来自动织入的,可以结合@Autowired 和 @Qualifier 结合使用时,自动注入的策略就从 byType 转变成 byName 了.
然后在XML中,只需要很少的配置了:
<context:annotationconfig/> 将隐式地向 Spring 容器注册 AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor 以及 equiredAnnotationBeanPostProcessor 这4个 BeanPostProcessor。
<context:component-scan/> 还允许定义过滤器将基包下的某些类纳入或排除
下面是一个测试代码:
可惜在如果 Bean 不是自己编写的类(如 JdbcTemplate、SessionFactoryBean 等),注释配置将无法实施.所以还的使用XML,这样将导致XML和Annotation混用,感觉有点
混乱.个人感觉Annotation能够很大程度减少配置的负担,但没有XML灵活,并且修改配置的
时候需要重新编译代码.有时候也不见得比XML简洁,例如使用 @Transactional 事务注释,没有 aop/tx 命名空间的事务配置灵活和简单.
package edu.jlu.fuliang;
import org.springframework.stereotype.Component;
@Component
public class MessageProvider {
private String message = "Hello World!";
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
我们只要在bean面前使用@Component注解就可以被Spring的IOC容器管理了.
在看看怎么把MessageProvider注入到MessageRender里面的:
package edu.jlu.fuliang;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class MessageRender {
private MessageProvider messageProvider;
@Autowired
public void setMessageProvider(MessageProvider messageProvider) {
this.messageProvider = messageProvider;
}
public void render(){
System.out.println(messageProvider.getMessage());
}
}
使用@Autowired注解,Spring就可以把messageProvider注入到MessageRender里面了,
默认是通过byType来自动织入的,可以结合@Autowired 和 @Qualifier 结合使用时,自动注入的策略就从 byType 转变成 byName 了.
然后在XML中,只需要很少的配置了:
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="edu.jlu.fuliang"/>
</beans>
<context:annotationconfig/> 将隐式地向 Spring 容器注册 AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor 以及 equiredAnnotationBeanPostProcessor 这4个 BeanPostProcessor。
<context:component-scan/> 还允许定义过滤器将基包下的某些类纳入或排除
<context:component-scan base-package="com.baobaotao">
<context:include-filter type="regex"
expression="edu\.jlu\.fuliang\.service\..*"/>
<context:exclude-filter type="aspectj"
expression="edu.jlu.fuliang.util..*"/>
</context:component-scan>
下面是一个测试代码:
package edu.jlu.fuliang;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloWorld {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});
MessageRender render = (MessageRender) context.getBean("messageRender");
render.render();
}
}
可惜在如果 Bean 不是自己编写的类(如 JdbcTemplate、SessionFactoryBean 等),注释配置将无法实施.所以还的使用XML,这样将导致XML和Annotation混用,感觉有点
混乱.个人感觉Annotation能够很大程度减少配置的负担,但没有XML灵活,并且修改配置的
时候需要重新编译代码.有时候也不见得比XML简洁,例如使用 @Transactional 事务注释,没有 aop/tx 命名空间的事务配置灵活和简单.
评论
fuliang
2008-03-17
貌似如果使用Annotation-based-configration,不能使用XmlBeanFactory来获得bean,获得的bean都是没有注入依赖的bean,可能它不自动注册AutowiredAnnotationBeanPostProcessor的原因吧.记得XmlBeanFactory不自动注册**BeanPostProcessor的.
发表评论
- 浏览: 51304 次
- 性别:

- 来自: 长春

- 详细资料
搜索本博客
我的相册
RSS Reader1
共 6 张
共 6 张
链接
最新评论
-
写了一个支持搜索并下载歌 ...
引用 为什么要配置成legal_music_link=http://202.10 ...
-- by fuliang -
使用Struts2+Hibernate+Sp ...
很好很强大
-- by andy54321 -
Java Persistence with Hi ...
昨天买的, 不错
-- by lklkdawei -
使用Struts2+Spring+Hiber ...
不过整个工程都没有一条注释啊。。。 这个比较郁闷,万一以后你写了个框架,那下面 ...
-- by yyphzc -
使用Struts2+Spring+Hiber ...
总体感觉还行,不过部分代码需要优化为好 1.DAO既然使用泛型,那就干脆点。想想 ...
-- by yeshucheng






评论排行榜