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

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

Spring bean 四種注入方式詳解

瀏覽:44日期: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
相關文章:
主站蜘蛛池模板: 德国GMN轴承,GMN角接触球轴承,GMN单向轴承,GMN油封,GMN非接触式密封 | 上海冠顶工业设备有限公司-隧道炉,烘箱,UV固化机,涂装设备,高温炉,工业机器人生产厂家 | 在线PH计-氧化锆分析仪-在线浊度仪-在线溶氧仪- 无锡朝达 | 东莞市踏板石餐饮管理有限公司_正宗桂林米粉_正宗桂林米粉加盟_桂林米粉加盟费-东莞市棒子桂林米粉 | 除尘布袋_液体过滤袋_针刺毡滤料-杭州辉龙过滤技术有限公司 | 塑料熔指仪-塑料熔融指数仪-熔体流动速率试验机-广东宏拓仪器科技有限公司 | MVR蒸发器厂家-多效蒸发器-工业废水蒸发器厂家-康景辉集团官网 | 破碎机锤头_耐磨锤头_合金锤头-鼎成机械一站式耐磨铸件定制服务 微型驱动系统解决方案-深圳市兆威机电股份有限公司 | 超声波清洗机_细胞破碎仪_实验室超声仪器_恒温水浴-广东洁盟深那仪器 | 锡膏喷印机-全自动涂覆机厂家-全自动点胶机-视觉点胶机-深圳市博明智控科技有限公司 | 聚氨酯保温钢管_聚氨酯直埋保温管道_聚氨酯发泡保温管厂家-沧州万荣防腐保温管道有限公司 | 高防护蠕动泵-多通道灌装系统-高防护蠕动泵-www.bjhuiyufluid.com慧宇伟业(北京)流体设备有限公司 | 隐形纱窗|防护纱窗|金刚网防盗纱窗|韦柏纱窗|上海青木装潢制品有限公司|纱窗国标起草单位 | 济南品牌包装设计公司_济南VI标志设计公司_山东锐尚文化传播 | 金属雕花板_厂家直销_价格低-山东慧诚建筑材料有限公司 | 冰晶石|碱性嫩黄闪蒸干燥机-有机垃圾烘干设备-草酸钙盘式干燥机-常州市宝康干燥 | 刑事律师_深圳著名刑事辩护律师_王平聚【清华博士|刑法教授】 | LCD3D打印机|教育|桌面|光固化|FDM3D打印机|3D打印设备-广州造维科技有限公司 | 搜活动房网—活动房_集装箱活动房_集成房屋_活动房屋 | MOOG伺服阀维修,ATOS比例流量阀维修,伺服阀维修-上海纽顿液压设备有限公司 | 高压负荷开关-苏州雷尔沃电器有限公司 | UV固化机_UVLED光固化机_UV干燥机生产厂家-上海冠顶公司专业生产UV固化机设备 | 政府回应:200块在义乌小巷能买到爱情吗?——揭秘打工族省钱约会的生存智慧 | 哈希PC1R1A,哈希CA9300,哈希SC4500-上海鑫嵩实业有限公司 | 马尔表面粗糙度仪-MAHR-T500Hommel-Mitutoyo粗糙度仪-笃挚仪器 | 户外健身路径_小区健身器材_室外健身器材厂家_价格-浩然体育 | CXB船用变压器-JCZ系列制动器-HH101船用铜质开关-上海永上船舶电器厂 | 比亚迪叉车-比亚迪电动叉车堆垛车托盘车仓储叉车价格多少钱报价 磁力去毛刺机_去毛刺磁力抛光机_磁力光饰机_磁力滚抛机_精密金属零件去毛刺机厂家-冠古科技 | 北京森语科技有限公司-模型制作专家-展览展示-沙盘模型设计制作-多媒体模型软硬件开发-三维地理信息交互沙盘 | 【官网】博莱特空压机,永磁变频空压机,螺杆空压机-欧能优 | T恤衫定做,企业文化衫制作订做,广告T恤POLO衫定制厂家[源头工厂]-【汉诚T恤定制网】 | 上海网站建设-上海网站制作-上海网站设计-上海做网站公司-咏熠软件 | SMC-SMC电磁阀-日本SMC气缸-SMC气动元件展示网 | 筛分机|振动筛分机|气流筛分机|筛分机厂家-新乡市大汉振动机械有限公司 | 胶泥瓷砖胶,轻质粉刷石膏,嵌缝石膏厂家,腻子粉批发,永康家德兴,永康市家德兴建材厂 | IHDW_TOSOKU_NEMICON_EHDW系列电子手轮,HC1系列电子手轮-上海莆林电子设备有限公司 | LED显示屏_LED屏方案设计精准报价专业安装丨四川诺显科技 | 航空障碍灯_高中低光强航空障碍灯_民航许可认证航空警示灯厂家-东莞市天翔航天科技有限公司 | 武汉印刷厂-不干胶标签印刷厂-武汉不干胶印刷-武汉标签印刷厂-武汉标签制作 - 善进特种标签印刷厂 | 丁基胶边来料加工,医用活塞边角料加工,异戊二烯橡胶边来料加工-河北盛唐橡胶制品有限公司 | 钢托盘,铁托盘,钢制托盘,镀锌托盘,饲料托盘,钢托盘制造商-南京飞天金属13260753852 |