久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

Spring Bean管理注解方式代碼實例

瀏覽:4日期:2023-09-15 08:35:59

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> <!--開啟包掃描--></beans>

2.將類交給Spring管理:

@Component('Demo1') //使用注解Componentpublic class Demo1 { @Value('yzy') private String name; public void say(){ System.out.println('你好呀!'+name); }}

Spring Bean管理注解方式代碼實例

3.Spring的屬性注入:

普通的屬性注入,使用@Value屬性注入:

@Component('Demo1') public class Demo1 { @Value('yzy') //使用注解Value,屬性注入 private String name; public void say(){ System.out.println('你好呀!'+name); }}

復雜的屬性注入,使用@Resource屬性注入:

import org.springframework.stereotype.Component;import javax.annotation.Resource;@Component('Demo1')public class Demo1 { @Resource(name='User') //使用@Resource,屬性注入對象 private User user; public void say(){ System.out.println('你好呀!'+user.getUsername()); }}

4.Spring的其他注解:

Spring Bean管理注解方式代碼實例

Spring Bean管理注解方式代碼實例

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 湖州市| 凌海市| 文水县| 包头市| 阳信县| 荥经县| 祁连县| 武定县| 大兴区| 鹤峰县| 拉萨市| 汉川市| 合江县| 木里| 分宜县| 黄梅县| 宜良县| 桂林市| 平果县| 平原县| 内丘县| 广宁县| 民丰县| 陇西县| 陈巴尔虎旗| 崇礼县| 行唐县| 波密县| 西峡县| 大姚县| 肇源县| 丘北县| 璧山县| 潼关县| 府谷县| 红安县| 盖州市| 蓝田县| 柯坪县| 鄂托克旗| 竹山县|