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

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

Spring(二):Spring通過IOC來創建對象

瀏覽:11日期:2023-07-04 09:18:55
目錄一、IOC如何獲取對象1.1 Spring是如何獲取對象的?1.2 改造案例由xml選擇創建對象二、IOC是通過什么方式來創建對象的?2.1 通過無參構造函數來創建對象2.2 通過有參構造方法來創建對象三、Spring的配置3.1 alias(別名):3.2 Bean的配置:3.3 import(團隊合作之導入)總結一、IOC如何獲取對象1.1 Spring是如何獲取對象的?

①新建一個maven項目后導入webmvc的依賴:因為webmvc包含了很多其他依賴,為了省事,干脆導入一個總的,方便省事!版本嘛!個人比較喜歡用最新版。

<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.5</version> </dependency>

②新建實體測試類:

public class Person { private String name; private int age; private String like; private String high; //get、set、tostring方法為了篇幅省略,可以自己加或者使用lombok}

③在resources目錄下新建ContextAplication.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 class='entity.Person'><property name='age' value='23'></property><property name='name' value='丁大大'></property><property name='like' value='釣魚'></property><property name='high' value='173'></property> </bean></beans>

④以上前提之后,你會發現你的測試Person類種發生了變化:點擊可以跳轉到指定的xml位置哦~

Spring(二):Spring通過IOC來創建對象

⑤測試:

Context.getBean() 不指定類時,需要強制轉換,所以建議使用第二種方式來獲取對象

public class Test { public static void main(String[] args) {ApplicationContext Context = new ClassPathXmlApplicationContext('ContextAplication.xml');//Person person = (Person) Context.getBean('Person');//這里不指定的話需要強轉,建議用下面的方式來拿對象Person person = Context.getBean('Person',Person.class);System.out.println(person); }}

⑥執行結果如下:成功拿到值!

Spring(二):Spring通過IOC來創建對象

⑦總結:

控制: 傳統的程序對象的創建是由程序來控制創建的。 反轉: 交給Spring容器來創建對象,而程序只負責被動的接收對象。這就是反轉。 依賴注入: 就是通過set方法來注入的。1.2 改造案例由xml選擇創建對象

①xml:

<bean /> <bean /> <bean class='service.impl.PersonServiceImpl'><property name='studentMapper' ref='StudentMapperImpl'/> </bean>

②測試:

ApplicationContext Context1 = new ClassPathXmlApplicationContext('ContextAplication.xml');PersonServiceImpl personServiceImpl = Context1.getBean('PersonServiceImpl', PersonServiceImpl.class);personServiceImpl.getPersonInfo();

③執行結果:

Spring(二):Spring通過IOC來創建對象

⑤總結:

對象由Spring 來創建 , 管理 , 裝配 !這就是 IOC!

二、IOC是通過什么方式來創建對象的?2.1 通過無參構造函數來創建對象

①以Person類為例子,但是加上一個無參構造函數!

public class Person { private String name; private int age; private String like; private String high; public Person() {//輸出一句話證明自己被調用了!System.out.println('我是Person類的無參構造函數!我被調用了!!!!'); } //set、get、tostring方法因為篇幅原因省略,請手動加上!}

②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 class='entity.Person'><property name='age' value='23'></property><property name='name' value='丁大大'></property><property name='like' value='釣魚'></property><property name='high' value='173'></property> </bean></beans>

③測試類:

public class Test { public static void main(String[] args) {ApplicationContext Context = new ClassPathXmlApplicationContext('ContextAplication.xml');Person person = Context.getBean('Person', Person.class);System.out.println(person); }}

④執行結果:

Spring(二):Spring通過IOC來創建對象

⑤去除無參構造,增加有參構造:

Spring(二):Spring通過IOC來創建對象

xml配置程序直接報錯:

Spring(二):Spring通過IOC來創建對象

⑥總結:

Spring創建對象默認是通過無參構造函數創建的!能通過有參構造函數來創建對象嘛?能!看下面!

2.2 通過有參構造方法來創建對象

①前提于 2.1 一致,新增有參構造函數:(因為類中,默認的也就是不寫構造參數就是無參構造,寫了有參構造才能真正意義上去除無參構造,這個不用解釋太多吧,java基礎的內容了~!)

public Person(String name, int age, String like, String high) {this.name = name;this.age = age;this.like = like;this.high = high; }

②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 class='entity.Person'><!--<property name='name' value='丁大大'></property>--><!--<property name='age' value='23'></property>--><!--<property name='like' value='釣魚'></property>--><!--<property name='high' value='173'></property>--><constructor-arg index='0' value='丁大大'/><constructor-arg name='age' value='23'/><constructor-arg type='java.lang.String' value='釣魚'/><constructor-arg type='java.lang.String' value='173'/> </bean></beans>

③執行結果:

Spring(二):Spring通過IOC來創建對象

⑤總結:

無參構造函數指定值時使用 propert 標簽 有參構造函數指定值時使用 constructor-arg 標簽,三種寫法 index --通過下標來給屬性賦值name --通過屬性名稱來給屬性賦值type -- 指定屬性的類型來給屬性賦值 基本類型可以直接寫 引用類型得加上全稱,如:java.lang.String 位置跟index差不多,依次從上到下對應屬性的從上到下。 在配置文件加載的時候。其中管理的對象都已經初始化了!三、Spring的配置3.1 alias(別名): 為bean設置別名,可設置多個!

①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'> <alias name='Person' alias='personAlias1'/> <alias name='Person' alias='personAlias2'/> <alias name='Person' alias='personAlias3'/> <bean class='entity.Person'><constructor-arg index='0' value='丁大大'/><constructor-arg name='age' value='23'/><constructor-arg type='java.lang.String' value='釣魚'/><constructor-arg type='java.lang.String' value='173'/> </bean></beans>

②測試類:

public class Test { public static void main(String[] args) {ApplicationContext Context = new ClassPathXmlApplicationContext('ContextAplication.xml');Person person = Context.getBean('personAlias1', Person.class);System.out.println(person); }}

③執行結果:

Spring(二):Spring通過IOC來創建對象

④總結:講實話,這玩意用處不大,因為還有更好的方式來設置別名!

3.2 Bean的配置: bean就相當于java對象,由Spring創建和管理

①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'> <alias name='Person' alias='personAlias1'/> <alias name='Person' alias='personAlias2'/> <alias name='Person' alias='personAlias3'/> <bean name='person1,person2 person3;person4' class='entity.Person'><constructor-arg index='0' value='丁大大'/><constructor-arg name='age' value='23'/><constructor-arg type='java.lang.String' value='釣魚'/><constructor-arg type='java.lang.String' value='173'/> </bean></beans>

②測試類:

public class Test { public static void main(String[] args) {ApplicationContext Context = new ClassPathXmlApplicationContext('ContextAplication.xml');Person person = Context.getBean('person4', Person.class);System.out.println(person); }}

③執行結果:

Spring(二):Spring通過IOC來創建對象

④總結:

id是bean的唯一標識符 如果沒有配置id,那么name相當于標識符,并且可以設置多個 name也是別名,可多個,并且可以通過 逗號 空格 分號 來分隔,是不是比alias別名方便?所以設置別名我們一般使用name id和name同時存在,name只是別名,不是標識符 class是類的全限定名 包名+類名

Spring(二):Spring通過IOC來創建對象

3.3 import(團隊合作之導入)

①在實際工作的開發過程中,一個項目可能由多個程序員來進行開發,所以為了解決共性問題,比如:同一文件提交時都進行了修改可能引起沖突,所以我們使用import來解耦!

②新建多個xml配置文件:

Spring(二):Spring通過IOC來創建對象

ContextAplication.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'> <import resource='dyj1.xml'/> <import resource='dyj3.xml'/> <import resource='dyj2.xml'/></beans>

dyj1.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 name='person1,person2 person3;person4' class='entity.Person'><constructor-arg index='0' value='丁大大1'/><constructor-arg name='age' value='23'/><constructor-arg type='java.lang.String' value='釣魚1'/><constructor-arg type='java.lang.String' value='173'/> </bean></beans>

dyj2.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 name='person1,person2 person3;person4' class='entity.Person'><constructor-arg index='0' value='丁大大2'/><constructor-arg name='age' value='23'/><constructor-arg type='java.lang.String' value='釣魚2'/><constructor-arg type='java.lang.String' value='173'/> </bean></beans>

dyj3.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 name='person1,person2 person3;person4' class='entity.Person'><constructor-arg index='0' value='丁大大3'/><constructor-arg name='age' value='23'/><constructor-arg type='java.lang.String' value='釣魚3'/><constructor-arg type='java.lang.String' value='173'/> </bean></beans>

③執行:

Spring(二):Spring通過IOC來創建對象

④總結:

如果三個文件都是對同一個操作同一個類,或者說內容一致,那么就以主xml中從上到下最后一個impot為準。 語法格式: 優點: 每個人開發的都是獨立的,如果重復的內容,Spring會幫我們自動合并!降低了程序的沖突性!大大提高了后期代碼的可維護性!總結

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

標簽: Spring
相關文章:
主站蜘蛛池模板: 磁力反应釜,高压釜,实验室反应釜,高温高压反应釜-威海自控反应釜有限公司 | 德国GMN轴承,GMN角接触球轴承,GMN单向轴承,GMN油封,GMN非接触式密封 | 小型UV打印机-UV平板打印机-大型uv打印机-UV打印机源头厂家 |松普集团 | 精益专家 - 设备管理软件|HSE管理系统|设备管理系统|EHS安全管理系统 | 河南卓美创业科技有限公司-河南卓美防雷公司-防雷接地-防雷工程-重庆避雷针-避雷器-防雷检测-避雷带-避雷针-避雷塔、机房防雷、古建筑防雷等-山西防雷公司 | 洁净棚-洁净工作棚-无菌室-净化工程公司_北京卫护科技有限公司 | 京港视通报道-质量走进大江南北-京港视通传媒[北京]有限公司 | 购买舔盐、舔砖、矿物质盐压块机,鱼饵、鱼饲料压块机--请到杜甫机械 | 电缆接头-防爆电缆接头-格兰头-金属电缆接头-防爆填料函 | 工业插头-工业插头插座【厂家】-温州罗曼电气 | 防爆电机生产厂家,YBK3电动机,YBX3系列防爆电机,YBX4节防爆电机--河南省南洋防爆电机有限公司 | 细沙回收机-尾矿干排脱水筛设备-泥石分离机-建筑垃圾分拣机厂家-青州冠诚重工机械有限公司 | 橡胶电子拉力机-塑料-微电脑电子拉力试验机厂家-江苏天源 | 皮带输送机-大倾角皮带输送机-皮带输送机厂家-河南坤威机械 | 报警器_家用防盗报警器_烟雾报警器_燃气报警器_防盗报警系统厂家-深圳市刻锐智能科技有限公司 | 志高装潢官网-苏州老房旧房装修改造-二手房装修翻新 | 熔体泵_熔体出料泵_高温熔体泵-郑州海科熔体泵有限公司 | 开平机_纵剪机厂家_开平机生产厂家|诚信互赢-泰安瑞烨精工机械制造有限公司 | 艾默生变频器,艾默生ct,变频器,ct驱动器,广州艾默生变频器,供水专用变频器,风机变频器,电梯变频器,艾默生变频器代理-广州市盟雄贸易有限公司官方网站-艾默生变频器应用解决方案服务商 | 贝壳粉涂料-内墙腻子-外墙腻子-山东巨野七彩贝壳漆业中心 | 河南砖机首页-全自动液压免烧砖机,小型砌块水泥砖机厂家[十年老厂] | 耐高温电缆厂家-远洋高温电缆| 精密模具加工制造 - 富东懿 | FAG轴承,苏州FAG轴承,德国FAG轴承-恩梯必传动设备(苏州)有限公司 | 游戏版号转让_游戏资质出售_游戏公司转让-【八九买卖网】 | 权威废金属|废塑料|废纸|废铜|废钢价格|再生资源回收行情报价中心-中废网 | 细胞染色-流式双标-试剂盒免费代做-上海研谨生物科技有限公司 | 海鲜池-专注海鲜鱼缸、移动海鲜缸、饭店鱼缸设计定做-日晟水族厂家 | 数显恒温油浴-电砂浴-高温油浴振荡器-常州迈科诺仪器有限公司 | 济南冷库安装-山东冷库设计|建造|冷库维修-山东齐雪制冷设备有限公司 | 优秀的临床医学知识库,临床知识库,医疗知识库,满足电子病历四级要求,免费试用 | 武汉高低温试验机-现货恒温恒湿试验箱-高低温湿热交变箱价格-湖北高天试验设备 | 大学食堂装修设计_公司餐厅效果图_工厂食堂改造_迈普装饰 | 石家庄救护车出租_重症转院_跨省跨境医疗转送_活动赛事医疗保障_康复出院_放弃治疗_腾康26年医疗护送转诊团队 | 氟氨基酮、氯硝柳胺、2-氟苯甲酸、异香兰素-新晨化工 | 诸城网站建设-网络推广-网站优化-阿里巴巴托管-诸城恒泰互联 | 六自由度平台_六自由度运动平台_三自由度摇摆台—南京全控科技 | 缠膜机|缠绕包装机|无纺布包装机-济南达伦特机械设备有限公司 | 上海恒驭仪器有限公司-实验室平板硫化机-小型平板硫化机-全自动平板硫化机 | MVR蒸发器厂家-多效蒸发器-工业废水蒸发器厂家-康景辉集团官网 | 南溪在线-南溪招聘找工作、找房子、找对象,南溪综合生活信息门户! |