Spring 2.5 標(biāo)注開發(fā)的簡單例子
研究了很久新出的 Spring 2.5, 總算大致明白了如何用標(biāo)注定義 Bean, 但是如何定義和注入類型為 java.lang.String 的 bean 仍然未解決, 希望得到高人幫助.
總的來看 Java EE 5 的標(biāo)注開發(fā)方式開來是得到了大家的認(rèn)可了.
@Service 相當(dāng)于定義 bean, 自動(dòng)根據(jù) bean 的類名生成一個(gè)首字母小寫的 bean
@Autowired 則是自動(dòng)注入依賴的類, 它會(huì)在類路徑中找成員對(duì)應(yīng)的類/接口的實(shí)現(xiàn)類, 如果找到多個(gè), 需要用 @Qualifier("chineseMan") 來指定對(duì)應(yīng)的 bean 的 ID.
一定程度上大大簡化了代碼的編寫, 例如一對(duì)一的 bean 映射現(xiàn)在完全不需要寫任何額外的 bean 定義了.
下面是代碼的運(yùn)行結(jié)果:
man.sayHello()=你好
SimpleMan said: Hi
org.example.EnglishMan@12bcd4b said: Hello
代碼:
beans.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:annotation-config/> <context:component-scan base-package="org.example"/> </beans>
測(cè)試類:
import org.example.IMan;
import org.example.SimpleMan;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
SimpleMan dao = (SimpleMan) ctx.getBean("simpleMan");
System.out.println(dao.hello());
IMan man = (IMan) ctx.getBean("usMan");
System.out.println(man.sayHello());
}
}
自動(dòng)探測(cè)和注入bean的類:
package org.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
public class SimpleMan {
// 自動(dòng)注入名稱為 Man 的 Bean
@Autowired(required = false)
@Qualifier("chineseMan")
//@Qualifier("usMan")
private IMan man;
/**
* @return the man
*/
public IMan getMan() {
return man;
}
/**
* @param man the man to set
*/
public void setMan(IMan man) {
this.man = man;
}
public String hello() {
System.out.println("man.sayHello()=" + man.sayHello());
return "SimpleMan said: Hi";
}
}
一個(gè)接口和兩個(gè)實(shí)現(xiàn)類:
package org.example;
/**
* 抽象的人接口.
* @author BeanSoft
* @version 1.0
*/
public interface IMan {
/**
* 打招呼的抽象定義.
* @return 招呼的內(nèi)容字符串
*/
public String sayHello();
}
package org.example;
import org.springframework.stereotype.Service;
/**
* 中國人的實(shí)現(xiàn).
* @author BeanSoft
*/
@Service
public class ChineseMan implements IMan {
public String sayHello() {
return "你好";
}
}
package org.example;
import org.springframework.stereotype.Service;
/**
* @author BeanSoft
*
*/
@Service("usMan")
// 這里定義了一個(gè) id 為 usMan 的 Bean, 標(biāo)注里面的屬性是 bean 的 id
public class EnglishMan implements IMan {
public String sayHello() {
return this + " said: Hello!";
}
}
posted on 2009-03-11 16:56 Eric Gu 閱讀(221) 評(píng)論(0) 編輯 收藏 所屬分類: Spring