szhswl
宋针还的个人空间

姓名: 蔡超 生日: 1976-09-03 总结: 六年大企业软件开发经验(其中两年任软件架构师),精通J2EE相关技术及架构设计,是SUN及Microsoft培训中心特邀高端讲师 SUN认证的J2EE架构师(SCEA) SUN认证的商务组件开发员(SCBCD) IBM认证的RUP专家 IBM认证的OOA&D (UML v2)专家




对于annotation的支持和组件的配置方式是扩充是spring2.5的主要提升之一。其中组件在类路径下的自动搜索功能也是一项值得注意的新增特性。
现来看个示例:

图表 1 示例中涉及的类的关系
Command.java:
package org.ccsoft.commands.imp;

public interface Command {
public void execute();
}

CommandExecuter.java:
package org.ccsoft.commands.imp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component("commandExecuter")
public class CommandExecuter {
private Command command;
@Autowired

public void setCommand(/*@Qualifier("helloCommand")*/ Command command) {
this.command = command;
}
public void executeCommand(){
command.execute();
}
}

HelloCommand.java
package org.ccsoft.commands.imp;

import org.springframework.stereotype.Component;


@Component
public class HelloCommand implements Command{
public void execute() {
System.out.println("Hello World");
}
}

配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"" target="_new">http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"" target="_new">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="org.ccsoft"/>
</beans>

测试程序如下:
package org.ccsoft;

import org.ccsoft.commands.imp.CommandExecuter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import junit.framework.TestCase;

public class TestCommandExecuter extends TestCase {
public void testExecuteCommand(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("application.xml");
CommandExecuter exe=(CommandExecuter) ctx.getBean("commandExecuter");
exe.executeCommand();
}
}
可以从配置文件中发现,这次并没有像常规的方式那样,声明所使用的bean,只是说明了组件搜索的基路径(便于减少搜索范围)“<context:component-scan base-package="org.ccsoft"/>”。
要点:
1 用@Component来声明每个组件,而不是通过配置文件中的bean元素
2 在需要注入其他组件的地方使用@Autowired,默认情况下会按类型进行依赖关系的查找和注入
3 可以指定组件的Id,通过@Component的值,如:@Component("commandExecuter"),这样类似于<bean id=” commandExecuter”…,注意如果不注明该值,spring将使用默认的命名规则,即类的名字但首字母小写,所以在上例中将@Component("commandExecuter")改写为@Component同样是可以运行的。

代码试验二:
在上面工程中加入新的Command接口实现类SorryCommand.
package org.ccsoft.commands.imp;

import org.springframework.stereotype.Component;
@Component
public class SorryCommand implements Command {

public void execute() {
// TODO Auto-generated method stub
System.out.println("Sorry");
}

}
再运行测试程序,会发现程序无法运行,抛出以下异常org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.ccsoft.commands.imp.Command] is defined: expected single matching bean but found 2: [helloCommand, sorryCommand]
这是因为根据类型的方式搜索,找到了两个相符的依赖类,这是我们必须指明使用那个组件。
@Autowired

public void setCommand(@Qualifier("helloCommand") Command command) {
this.command = command;
}
要点:
4 在多个同类组件存在于查找路径时必须通过@Qualifier指明

不知为什么,spring的这一新特性总让我联想起OSGi DS,不过与OSGi DS相比个人认为spring的IoC还是有些不便的地方,OSGi DS的Service的引用策略中的Cardinality可以指定组件的注入策略,可以支持将多个符合条件的组件多次注入,这为编写组件容器这样的应用提供了很大的便利。
如:

private List<Command> commands;

public void setCommand(Command cmd){
commands.add(cmd);
}

….

最后,这里只是涉及了spring2.5中相关内容中非常少的一点,如果大家有兴趣深入了解可以参考spring reference。
个人认为在大家充分享受新特性带来的便利的同时也要主要到新特性是否会对应用的可维护性和部署时改变注入关系的方便性带来负面影响。



蔡超
JavaEE 咨询顾问
SCEA
IBM Certified Solution Designer for OOA&D UML2

转自:http://dev2dev.bea.com.cn/blog/chaocai/200712/spring_osgi_04_719.html



---------------------------------------------------------------------------------------------------------------------------------
说人之短,乃护己之短。夸己之长,乃忌人之长。皆由存心不厚,识量太狭耳。能去此弊,可以进德,可以远怨。
http://www.blogjava.net/szhswl
------------------------------------------------------------------------------------------------------ ----------------- ---------
posted on 2007-12-05 09:59 宋针还 阅读(768) 评论(0)  编辑  收藏 所属分类: SPRING

只有注册用户登录后才能发表评论。


网站导航: