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

本文介绍Spring 2.5中的注解配置方式,通过一个HelloWorld实例展示了如何使用@Component和@Autowired进行依赖注入,并探讨了注解配置与XML配置的优缺点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring2.5 Annotation-based-configration大大简化了配置,用一个经典的HelloWorld程序来体验一下:
[code]
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;
}
}
[/code]
我们只要在bean面前使用@Component注解就可以被Spring的IOC容器管理了.
在看看怎么把MessageProvider注入到MessageRender里面的:
[code]
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());
}
}
[/code]
使用@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>

下面是一个测试代码:
[code]
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();
}
}
[/code]
可惜在如果 Bean 不是自己编写的类(如 JdbcTemplate、SessionFactoryBean 等),注释配置将无法实施.所以还的使用XML,这样将导致XML和Annotation混用,感觉有点
混乱.个人感觉Annotation能够很大程度减少配置的负担,但没有XML灵活,并且修改配置的
时候需要重新编译代码.有时候也不见得比XML简洁,例如使用 @Transactional 事务注释,没有 aop/tx 命名空间的事务配置灵活和简单.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值