锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
瀵逛簬annotation鐨勬敮鎸佸拰緇勪歡鐨勯厤緗柟寮忔槸鎵╁厖鏄痵pring2.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();
}
}
鍙互浠庨厤緗枃浠朵腑鍙戠幇錛岃繖嬈″茍娌℃湁鍍忓父瑙勭殑鏂瑰紡閭f牱錛屽0鏄庢墍浣跨敤鐨刡ean錛屽彧鏄鏄庝簡緇勪歡鎼滅儲鐨勫熀璺緞錛堜究浜庡噺灝戞悳绱㈣寖鍥達級“<context:component-scan base-package="org.ccsoft"/>”銆?
瑕佺偣錛?
1 鐢ˊComponent鏉ュ0鏄庢瘡涓粍浠訛紝鑰屼笉鏄氳繃閰嶇疆鏂囦歡涓殑bean鍏冪礌
2 鍦ㄩ渶瑕佹敞鍏ュ叾浠栫粍浠剁殑鍦版柟浣跨敤@Autowired錛岄粯璁ゆ儏鍐典笅浼氭寜綾誨瀷榪涜渚濊禆鍏崇郴鐨勬煡鎵懼拰娉ㄥ叆
3 鍙互鎸囧畾緇勪歡鐨処d錛岄氳繃@Component鐨勫鹼紝濡傦細@Component("commandExecuter")錛岃繖鏍風被浼間簬<bean id=” commandExecuter”…錛屾敞鎰忓鏋滀笉娉ㄦ槑璇ュ鹼紝spring灝嗕嬌鐢ㄩ粯璁ょ殑鍛藉悕瑙勫垯錛屽嵆綾葷殑鍚嶅瓧浣嗛瀛楁瘝灝忓啓錛屾墍浠ュ湪涓婁緥涓皢@Component("commandExecuter")鏀瑰啓涓篅Component鍚屾牱鏄彲浠ヨ繍琛岀殑銆?
浠g爜璇曢獙浜岋細
鍦ㄤ笂闈㈠伐紼嬩腑鍔犲叆鏂扮殑Command鎺ュ彛瀹炵幇綾籗orryCommand.
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]
榪欐槸鍥犱負鏍規嵁綾誨瀷鐨勬柟寮忔悳绱紝鎵懼埌浜嗕袱涓浉絎︾殑渚濊禆綾伙紝榪欐槸鎴戜滑蹇呴』鎸囨槑浣跨敤閭d釜緇勪歡銆?
@Autowired
public void setCommand(@Qualifier("helloCommand") Command command) {
this.command = command;
}
瑕佺偣錛?
4 鍦ㄥ涓悓綾葷粍浠跺瓨鍦ㄤ簬鏌ユ壘璺緞鏃跺繀欏婚氳繃@Qualifier鎸囨槑
涓嶇煡涓轟粈涔堬紝spring鐨勮繖涓鏂扮壒鎬ф昏鎴戣仈鎯寵搗OSGi DS錛屼笉榪囦笌OSGi DS鐩告瘮涓漢璁や負spring鐨処oC榪樻槸鏈変簺涓嶄究鐨勫湴鏂癸紝OSGi DS鐨凷ervice鐨勫紩鐢ㄧ瓥鐣ヤ腑鐨凜ardinality鍙互鎸囧畾緇勪歡鐨勬敞鍏ョ瓥鐣ワ紝鍙互鏀寔灝嗗涓鍚堟潯浠剁殑緇勪歡澶氭娉ㄥ叆錛岃繖涓虹紪鍐欑粍浠跺鍣ㄨ繖鏍風殑搴旂敤鎻愪緵浜嗗緢澶х殑渚垮埄銆?
濡傦細
…
private List<Command> commands錛?
…
public void setCommand(Command cmd){
commands.add(cmd);
}
….
鏈鍚庯紝榪欓噷鍙槸娑夊強浜唖pring2.5涓浉鍏沖唴瀹逛腑闈炲父灝戠殑涓鐐癸紝濡傛灉澶у鏈夊叴瓚f繁鍏ヤ簡瑙e彲浠ュ弬鑰僺pring reference銆?
涓漢璁や負鍦ㄥぇ瀹跺厖鍒嗕韓鍙楁柊鐗規у甫鏉ョ殑渚垮埄鐨勫悓鏃朵篃瑕佷富瑕佸埌鏂扮壒鎬ф槸鍚︿細瀵瑰簲鐢ㄧ殑鍙淮鎶ゆу拰閮ㄧ講鏃舵敼鍙樻敞鍏ュ叧緋葷殑鏂逛究鎬у甫鏉ヨ礋闈㈠獎鍝嶃?
钄¤秴
JavaEE 鍜ㄨ欏鵑棶
SCEA
IBM Certified Solution Designer for OOA&D UML2
杞嚜錛?a >http://dev2dev.bea.com.cn/blog/chaocai/200712/spring_osgi_04_719.html