电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

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

Spring bean 四種注入方式詳解

瀏覽:46日期:2023-07-01 17:11:48
目錄一、Set方式注入pojo層:1.xml 文件test測試二、構造函數方式注入pojo層2.xml文件test測試三、注解注入pojo層3.xml文件test測試四、JavaConfig 方式注入pojo層JavaConfig 類xml文件 掃描包測試:五、Service層注入詳解serviceserviceImplxml配置文件總結一、Set方式注入pojo層:

/** * @Author: crush * @Date: 2021-06-17 16:57 * version 1.0 * xml 配置注入版本 set 方式 */public class Student1 { public String name; public String school; public void setName(String name) {this.name = name; } public void setSchool(String school) {this.school = school; } @Override public String toString() {return 'Student1{' +'name=’' + name + ’’’ +', school=’' + school + ’’’ +’}’; }}1.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' xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd'> <!--set方式注入id是注入bean中的名字class 是全限定類名property 是按照set方式注入 --> <bean class='com.crush.pojo.Student1'><property name='name' value='wyh1'/><property name='school' value='hngy1'/> </bean></beans>test測試

@Test public void student1(){ApplicationContext context = new ClassPathXmlApplicationContext('student1.xml');Student1 student1 = context.getBean('student1', Student1.class);System.out.println(student1); }二、構造函數方式注入pojo層

/** * @Author: crush * @Date: 2021-06-17 17:02 * version 1.0 * xml 配置 構造函數方式注入 */public class Student2 { private String name; private String school; public Student2(String name, String school) {this.name = name;this.school = school; } @Override public String toString() {return 'Student2{' +'name=’' + name + ’’’ +', school=’' + school + ’’’ +’}’; }}2.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' xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd'> <!--set方式注入id是注入bean中的名字class 是全限定類名constructor 是按照構造方式注入index 是按照成員變量在構造函數中的參數的第幾個name 表示成員變量名type 表示類型value 表示值ref 表示引用 可引用另外一個注入到Spring的中的值 --> <bean class='com.crush.pojo.Student2'><constructor-arg index='0' name='name' type='java.lang.String' value='wyh2'/><constructor-arg name='school' value='hngy2'/> </bean></beans>test測試

@Test public void student2(){ApplicationContext context = new ClassPathXmlApplicationContext('student2.xml');Student2 student2 = context.getBean('student2', Student2.class);System.out.println(student2); }三、注解注入pojo層

/** * @Author: crush * @Date: 2021-06-17 17:08 * version 1.0 */@Componentpublic class Student3 { @Value('wyh3') private String name; @Value('hngy3') private String school; @Override public String toString() {return 'Student3{' +'name=’' + name + ’’’ +', school=’' + school + ’’’ +’}’; }}3.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='com.crush.pojo'/></beans>test測試

@Test public void student3(){ApplicationContext context = new ClassPathXmlApplicationContext('student3.xml');Student3 student3 = context.getBean('student3', Student3.class);System.out.println(student3); }四、JavaConfig 方式注入pojo層

/** * @Author: crush * @Date: 2021-06-17 17:16 * version 1.0 * JavaConfig 配置 */public class Student4 { @Value('wyh4') private String name; @Value('hngy4') private String school; @Override public String toString() {return 'Student4{' +'name=’' + name + ’’’ +', school=’' + school + ’’’ +’}’; }}JavaConfig 類

@Configurationpublic class Student4Config { @Bean public Student4 student4(){return new Student4(); }}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='com.crush.config'/></beans>測試:

@Test public void student4(){ApplicationContext context = new ClassPathXmlApplicationContext('student4.xml');Student4 student4 = context.getBean('student4', Student4.class);System.out.println(student4); }五、Service層注入詳解service

/** * @Author: crush * @Date: 2021-06-17 17:27 * version 1.0 * xml 配置 */public interface StudentService1 { void test();}serviceImpl

/** * @Author: crush * @Date: 2021-06-17 17:29 * version 1.0 * xml 配置 */public class StudentService1Impl implements StudentService1{ @Override public void test() {System.out.println('===StudentDao1Impl==='); }}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' xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd'> <bean /></beans>總結

本篇文章就到這里了,希望能給你帶來幫助,也希望能夠您能夠關注好吧啦網的更多內容!

標簽: Spring
相關文章:
主站蜘蛛池模板: 过跨车_过跨电瓶车_过跨转运车_横移电动平车_厂区转运车_无轨转运车 | 房间温控器|LonWorks|海思 | 爱德华真空泵油/罗茨泵维修,爱发科-比其尔产品供应东莞/杭州/上海等全国各地 | 澳门精准正版免费大全,2025新澳门全年免费,新澳天天开奖免费资料大全最新,新澳2025今晚开奖资料,新澳马今天最快最新图库 | 太空舱_民宿太空舱厂家_移动房屋太空舱价格-豪品建筑 | 中空玻璃生产线,玻璃加工设备,全自动封胶线,铝条折弯机,双组份打胶机,丁基胶/卧式/立式全自动涂布机,玻璃设备-山东昌盛数控设备有限公司 | SMN-1/SMN-A ABB抽屉开关柜触头夹紧力检测仪-SMN-B/SMN-C-上海徐吉 | 北京西风东韵品牌与包装设计公司,创造视觉销售力! | 众品地板网-地板品牌招商_地板装修设计_地板门户的首选网络媒体。 | 济南铝方通-济南铝方通价格-济南方通厂家-山东鲁方通建材有限公司 | 轻型地埋电缆故障测试仪,频响法绕组变形测试仪,静荷式卧式拉力试验机-扬州苏电 | 篮球架_乒乓球台_足球门_校园_竞技体育器材_厂家_价格-沧州浩然体育器材有限公司 | 干粉砂浆设备_干混砂浆生产线_腻子粉加工设备_石膏抹灰砂浆生产成套设备厂家_干粉混合设备_砂子烘干机--郑州铭将机械设备有限公司 | 校服厂家,英伦校服定做工厂,园服生产定制厂商-东莞市艾咪天使校服 | 石栏杆_青石栏杆_汉白玉栏杆_花岗岩栏杆 - 【石雕之乡】点石石雕石材厂 | 承插管件_不锈钢承插管件_锻钢高压管件-温州科正阀门管件有限公司 | 电动葫芦|防爆钢丝绳电动葫芦|手拉葫芦-保定大力起重葫芦有限公司 | 干培两用箱-细菌恒温培养箱-菲斯福仪器 | 咖啡加盟-咖啡店加盟-咖啡西餐厅加盟-塞纳左岸咖啡西餐厅官网 | 不锈钢螺丝,不锈钢螺栓,不锈钢标准件-江苏百德特种合金有限公司 交变/复合盐雾试验箱-高低温冲击试验箱_安奈设备产品供应杭州/江苏南京/安徽马鞍山合肥等全国各地 | 剪刃_纵剪机刀片_分条机刀片-南京雷德机械有限公司 | 喷播机厂家_二手喷播机租赁_水泥浆洒布机-河南青山绿水机电设备有限公司 | 99文库_实习生实用的范文资料文库站 | 高铝矾土熟料_细粉_骨料_消失模_铸造用铝矾土_铝酸钙粉—嵩峰厂家 | 塑料检查井_双扣聚氯乙烯增强管_双壁波纹管-河南中盈塑料制品有限公司 | 托盘租赁_塑料托盘租赁_托盘出租_栈板出租_青岛托盘租赁-优胜必达 | 沈阳真空机_沈阳真空包装机_沈阳大米真空包装机-沈阳海鹞真空包装机械有限公司 | 中空玻璃生产线,玻璃加工设备,全自动封胶线,铝条折弯机,双组份打胶机,丁基胶/卧式/立式全自动涂布机,玻璃设备-山东昌盛数控设备有限公司 | 北京征地律师,征地拆迁律师,专业拆迁律师,北京拆迁律师,征地纠纷律师,征地诉讼律师,征地拆迁补偿,拆迁律师 - 北京凯诺律师事务所 | 土壤水分自动监测站-SM150便携式土壤水分仪-铭奥仪器 | 健康管理师报考条件,考试时间,报名入口—首页 | 999范文网_优质范文下载写作帮手 | 学考网学历中心| 锯边机,自动锯边机,双面涂胶机-建业顺达机械有限公司 | 步进_伺服_行星减速机,微型直流电机,大功率直流电机-淄博冠意传动机械 | 防火卷帘门价格-聊城一维工贸特级防火卷帘门厂家▲ | 塑钢课桌椅、学生课桌椅、课桌椅厂家-学仕教育设备首页 | 陶氏道康宁消泡剂_瓦克消泡剂_蓝星_海明斯德谦_广百进口消泡剂 | 安全阀_弹簧式安全阀_美标安全阀_工业冷冻安全阀厂家-中国·阿司米阀门有限公司 | 北京宣传片拍摄_产品宣传片拍摄_宣传片制作公司-现像传媒 | 专业生物有机肥造粒机,粉状有机肥生产线,槽式翻堆机厂家-郑州华之强重工科技有限公司 |