﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-szhswl-文章分类-SPRING</title><link>http://www.blogjava.net/szhswl/category/27848.html</link><description>宋针还的个人空间</description><language>zh-cn</language><lastBuildDate>Wed, 05 Dec 2007 18:24:41 GMT</lastBuildDate><pubDate>Wed, 05 Dec 2007 18:24:41 GMT</pubDate><ttl>60</ttl><item><title>Spring2.5的组件自动搜索</title><link>http://www.blogjava.net/szhswl/articles/165389.html</link><dc:creator>宋针还</dc:creator><author>宋针还</author><pubDate>Wed, 05 Dec 2007 01:59:00 GMT</pubDate><guid>http://www.blogjava.net/szhswl/articles/165389.html</guid><wfw:comment>http://www.blogjava.net/szhswl/comments/165389.html</wfw:comment><comments>http://www.blogjava.net/szhswl/articles/165389.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/szhswl/comments/commentRss/165389.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/szhswl/services/trackbacks/165389.html</trackback:ping><description><![CDATA[<p>姓名： 蔡超 生日： 1976-09-03 总结： 六年大企业软件开发经验（其中两年任软件架构师），精通J2EE相关技术及架构设计，是SUN及Microsoft培训中心特邀高端讲师 SUN认证的J2EE架构师(SCEA) SUN认证的商务组件开发员(SCBCD) IBM认证的RUP专家 IBM认证的OOA&amp;D (UML v2)专家 <br />
<br />
<br />
<br />
<br />
对于annotation的支持和组件的配置方式是扩充是spring2.5的主要提升之一。其中组件在类路径下的自动搜索功能也是一项值得注意的新增特性。 <br />
现来看个示例： <br />
<br />
图表 1 示例中涉及的类的关系 <br />
Command.java: <br />
package org.ccsoft.commands.imp; <br />
<br />
public interface Command { <br />
public void execute(); <br />
} <br />
<br />
CommandExecuter.java: <br />
package org.ccsoft.commands.imp; <br />
<br />
import org.springframework.beans.factory.annotation.Autowired; <br />
import org.springframework.beans.factory.annotation.Qualifier; <br />
import org.springframework.stereotype.Component; <br />
@Component("commandExecuter") <br />
public class CommandExecuter { <br />
private Command command; <br />
@Autowired <br />
<br />
public void setCommand(/*@Qualifier("helloCommand")*/ Command command) { <br />
this.command = command; <br />
} <br />
public void executeCommand(){ <br />
command.execute(); <br />
} <br />
} <br />
<br />
HelloCommand.java <br />
package org.ccsoft.commands.imp; <br />
<br />
import org.springframework.stereotype.Component; <br />
<br />
<br />
@Component <br />
public class HelloCommand implements Command{ <br />
public void execute() { <br />
System.out.println("Hello World"); <br />
} <br />
} <br />
<br />
配置文件： <br />
&lt;?xml version="1.0" encoding="UTF-8"?&gt; <br />
&lt;beans xmlns="<a href="http://www.blogjava.net/chaocai/archive/2007/12/04/<a%20href=" target="_new" http: www.springframework.org schema beans?>http://www.springframework.org/schema/beans</a>"" target="_new"&gt;<a href="http://www.springframework.org/schema/beans" target="_new">http://www.springframework.org/schema/beans</a>"</a> <br />
xmlns:xsi="<a href='http://www.w3.org/2001/XMLSchema-instance"' target="_new" href_cetemp='http://www.w3.org/2001/XMLSchema-instance"'>http://www.w3.org/2001/XMLSchema-instance"</a> <br />
xmlns:context="<a href="http://www.blogjava.net/chaocai/archive/2007/12/04/<a%20href=" target="_new" http: www.springframework.org schema context?>http://www.springframework.org/schema/context</a>"" target="_new"&gt;<a href="http://www.springframework.org/schema/context" target="_new">http://www.springframework.org/schema/context</a>"</a> <br />
xsi:schemaLocation="<a href="http://www.springframework.org/schema/beans" target="_new">http://www.springframework.org/schema/beans</a> <br />
<a href="http://www.springframework.org/schema/beans" target="_new">http://www.springframework.org/schema/beans</a>/spring-beans-2.5.xsd <br />
<a href="http://www.springframework.org/schema/context" target="_new">http://www.springframework.org/schema/context</a> <br />
<a href="http://www.springframework.org/schema/context" target="_new">http://www.springframework.org/schema/context</a>/spring-context-2.5.xsd"&gt; <br />
<br />
&lt;context:component-scan base-package="org.ccsoft"/&gt; <br />
&lt;/beans&gt; <br />
<br />
测试程序如下： <br />
package org.ccsoft; <br />
<br />
import org.ccsoft.commands.imp.CommandExecuter; <br />
import org.springframework.context.ApplicationContext; <br />
import org.springframework.context.support.ClassPathXmlApplicationContext; <br />
<br />
import junit.framework.TestCase; <br />
<br />
public class TestCommandExecuter extends TestCase { <br />
public void testExecuteCommand(){ <br />
ApplicationContext ctx=new ClassPathXmlApplicationContext("application.xml"); <br />
CommandExecuter exe=(CommandExecuter) ctx.getBean("commandExecuter"); <br />
exe.executeCommand(); <br />
} <br />
} <br />
可以从配置文件中发现，这次并没有像常规的方式那样，声明所使用的bean，只是说明了组件搜索的基路径（便于减少搜索范围）&#8220;&lt;context:component-scan base-package="org.ccsoft"/&gt;&#8221;。 <br />
要点： <br />
1 用@Component来声明每个组件，而不是通过配置文件中的bean元素 <br />
2 在需要注入其他组件的地方使用@Autowired，默认情况下会按类型进行依赖关系的查找和注入 <br />
3 可以指定组件的Id，通过@Component的值，如：@Component("commandExecuter")，这样类似于&lt;bean id=&#8221; commandExecuter&#8221;&#8230;，注意如果不注明该值，spring将使用默认的命名规则，即类的名字但首字母小写，所以在上例中将@Component("commandExecuter")改写为@Component同样是可以运行的。 <br />
<br />
代码试验二： <br />
在上面工程中加入新的Command接口实现类SorryCommand. <br />
package org.ccsoft.commands.imp; <br />
<br />
import org.springframework.stereotype.Component; <br />
@Component <br />
public class SorryCommand implements Command { <br />
<br />
public void execute() { <br />
// TODO Auto-generated method stub <br />
System.out.println("Sorry"); <br />
} <br />
<br />
} <br />
再运行测试程序，会发现程序无法运行，抛出以下异常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] <br />
这是因为根据类型的方式搜索，找到了两个相符的依赖类，这是我们必须指明使用那个组件。 <br />
@Autowired <br />
<br />
public void setCommand(@Qualifier("helloCommand") Command command) { <br />
this.command = command; <br />
} <br />
要点： <br />
4 在多个同类组件存在于查找路径时必须通过@Qualifier指明 <br />
<br />
不知为什么，spring的这一新特性总让我联想起OSGi DS，不过与OSGi DS相比个人认为spring的IoC还是有些不便的地方，OSGi DS的Service的引用策略中的Cardinality可以指定组件的注入策略，可以支持将多个符合条件的组件多次注入，这为编写组件容器这样的应用提供了很大的便利。 <br />
如： <br />
&#8230; <br />
private List&lt;Command&gt; commands； <br />
&#8230; <br />
public void setCommand(Command cmd){ <br />
commands.add(cmd); <br />
} <br />
<br />
&#8230;. <br />
<br />
最后，这里只是涉及了spring2.5中相关内容中非常少的一点，如果大家有兴趣深入了解可以参考spring reference。 <br />
个人认为在大家充分享受新特性带来的便利的同时也要主要到新特性是否会对应用的可维护性和部署时改变注入关系的方便性带来负面影响。 <br />
<br />
<br />
<br />
蔡超 <br />
JavaEE 咨询顾问 <br />
SCEA <br />
IBM Certified Solution Designer for OOA&amp;D UML2 <br />
<br />
转自：<a href="http://dev2dev.bea.com.cn/blog/chaocai/200712/spring_osgi_04_719.html">http://dev2dev.bea.com.cn/blog/chaocai/200712/spring_osgi_04_719.html</a></p>
<img src ="http://www.blogjava.net/szhswl/aggbug/165389.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/szhswl/" target="_blank">宋针还</a> 2007-12-05 09:59 <a href="http://www.blogjava.net/szhswl/articles/165389.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>