`

体验一下Spring2.5 Annotation-based-configration

阅读更多
Spring2.5 Annotation-based-configration大大简化了配置,用一个经典的HelloWorld程序来体验一下:
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 命名空间的事务配置灵活和简单.
7
3
分享到:
评论
2 楼 sweety 2008-07-03  
引用
很有用的功能
1 楼 fuliang 2008-03-17  
貌似如果使用Annotation-based-configration,不能使用XmlBeanFactory来获得bean,获得的bean都是没有注入依赖的bean,可能它不自动注册AutowiredAnnotationBeanPostProcessor的原因吧.记得XmlBeanFactory不自动注册**BeanPostProcessor的.

相关推荐

Global site tag (gtag.js) - Google Analytics