java开源文档大全致力于打造中国最大最全的开源文档,它提供了最全面最权威的开源资料,同时为大家提供一个交流的平台,如果您有好的想法,欢迎您投稿.
对于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" 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="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。 个人认为在大家充分享受新特性带来的便利的同时也要主要到新特性是否会对应用的可维护性和部署时改变注入关系的方便性带来负面影响。
java开源文档研究struts,webwork,spring,tomcat,jboss,lucense,nutch,JUnit,eclipse......,如果您有什么意见,欢迎评论和留言. |