Spring Bean管理注解方式代碼實(shí)例
1.使用注解的方式需要配置applicationContext.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.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd'> <context:component-scan base-package='org.yzytest1'></context:component-scan> <!--開(kāi)啟包掃描--></beans>
2.將類(lèi)交給Spring管理:
@Component('Demo1') //使用注解Componentpublic class Demo1 { @Value('yzy') private String name; public void say(){ System.out.println('你好呀!'+name); }}
3.Spring的屬性注入:
普通的屬性注入,使用@Value屬性注入:
@Component('Demo1') public class Demo1 { @Value('yzy') //使用注解Value,屬性注入 private String name; public void say(){ System.out.println('你好呀!'+name); }}
復(fù)雜的屬性注入,使用@Resource屬性注入:
import org.springframework.stereotype.Component;import javax.annotation.Resource;@Component('Demo1')public class Demo1 { @Resource(name='User') //使用@Resource,屬性注入對(duì)象 private User user; public void say(){ System.out.println('你好呀!'+user.getUsername()); }}
4.Spring的其他注解:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)2. chat.asp聊天程序的編寫(xiě)方法3. 告別AJAX實(shí)現(xiàn)無(wú)刷新提交表單4. html小技巧之td,div標(biāo)簽里內(nèi)容不換行5. jsp文件下載功能實(shí)現(xiàn)代碼6. XHTML 1.0:標(biāo)記新的開(kāi)端7. CSS3中Transition屬性詳解以及示例分享8. 怎樣才能用js生成xmldom對(duì)象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?9. ASP動(dòng)態(tài)網(wǎng)頁(yè)制作技術(shù)經(jīng)驗(yàn)分享10. CSS Hack大全-教你如何區(qū)分出IE6-IE10、FireFox、Chrome、Opera
