Java 中的控制反轉(zhuǎn)(IOC)詳解
IOC理論推導(dǎo)
Dao層
1.UserDao 接口
2.UserDaoImpl 實(shí)現(xiàn)類
Service層
3.UserService 業(yè)務(wù)接口
4.UserServiceImpl 業(yè)務(wù)實(shí)現(xiàn)類
用戶實(shí)際調(diào)用service層 不會(huì)動(dòng)dao層!
Dao層接口創(chuàng)建
package com.ckm.dao; public interface UserDao { void getUser(); }
Dao層實(shí)現(xiàn)類1創(chuàng)建
package com.ckm.dao; public class UserDaoImpl implements UserDao{ @Override public void getUser() { System.out.println("獲取了User數(shù)據(jù)"); } }
Dao層實(shí)現(xiàn)類2創(chuàng)建
package com.ckm.dao; public class UserDaoMysqlImpl implements UserDao{ @Override public void getUser() { System.out.println("mysql使用了數(shù)據(jù)"); } }
Service層接口創(chuàng)建
package com.ckm.service; public interface UserService { void getUser(); }
Service層接口實(shí)現(xiàn)類創(chuàng)建
package com.ckm.service; import com.ckm.dao.UserDao; import com.ckm.dao.UserDaoImpl; public class UserServiceImpl implements UserService{ // private UserDao userDao = new UserDaoImpl(); //利用set進(jìn)行動(dòng)態(tài)實(shí)現(xiàn)值的注入 private UserDao userDao; public void setUserDao(UserDao userDao){ this.userDao = userDao; } @Override public void getUser() { userDao.getUser(); } }
單元測(cè)試
import com.ckm.dao.UserDaoImpl; import com.ckm.dao.UserDaoMysqlImpl; import com.ckm.service.UserServiceImpl; public class TestIOC { public static void main(String[] args) { //用戶實(shí)際調(diào)用service層 不會(huì)動(dòng)dao層! UserServiceImpl userService = new UserServiceImpl(); userService.setUserDao(new UserDaoMysqlImpl()); userService.getUser(); } }
Spring管理對(duì)象
resources中新建beans.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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <!--使用Spring來(lái)創(chuàng)建對(duì)象,在Spring這些都稱為Bean--> <!--bean = 對(duì)象--> <!--id = 變量名--> <!--class = new的對(duì)象--> <!--property 相當(dāng)于給對(duì)象中的屬性設(shè)值--> <bean id="DaoImpl" class="com.ckm.dao.UserDaoImpl" /> <bean id="MysqlImpl" class="com.ckm.dao.UserDaoMysqlImpl" /> <bean id="UserServiceImpl" class="com.ckm.service.UserServiceImpl"> <!-- value具體的值,基本數(shù)據(jù)類型 ref是引用Spring中創(chuàng)建好的對(duì)象 用戶需要什么 就直接修改ref就ok --> <property name="userDao" ref="MysqlImpl" /> </bean> </beans>
dao層、Service層代碼不變
測(cè)試類
import com.ckm.service.UserServiceImpl; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestIOC { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("UserServiceImpl"); userServiceImpl.getUser(); } }
Spring管理對(duì)象的簡(jiǎn)單例子
User類
package com.ckm.pojo; public class Hello { private String str; public String getStr() { return str; } public void setStr(String str) { this.str = str; } @Override public String toString() { return "Hello{" + "str='" + str + '\'' + '}'; } }
beans.xml
package com.ckm.pojo; public class Hello { private String str; public String getStr() { return str; } public void setStr(String str) { this.str = str; } @Override public String toString() { return "Hello{" + "str='" + str + '\'' + '}'; } }
測(cè)試類
import com.ckm.pojo.Hello; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { public static void main(String[] args) { //獲取spring的上下文對(duì)象 //xml加載必須new ClassPathXmlApplicationContext() ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); //我們的對(duì)象現(xiàn)在都在spring中管理,我們要使用,直接去里面取出來(lái) Hello hello = (Hello) context.getBean("hello"); System.out.println(hello.getStr()); } }
Bean無(wú)參構(gòu)造類創(chuàng)建和有參構(gòu)造類創(chuàng)建
當(dāng)getBean的時(shí)候,這個(gè)類就已經(jīng)被實(shí)例化了,就會(huì)執(zhí)行無(wú)參構(gòu)造方法
有參構(gòu)造的User類
package com.ckm.pojo; public class User { private String name; // public User() { // System.out.println("無(wú)參構(gòu)造"); // } public User(String name){ this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void show(){ System.out.println("name="+name); } }
編寫B(tài)eans.xml
<!--第一種 下標(biāo)賦值--> <bean id="user" class="com.ckm.pojo.User"> <constructor-arg index="0" value="這個(gè)是第一種有參設(shè)置的name"/> </bean>
<!--第二種 直接通過(guò)參數(shù)名--> <bean id="user" class="com.ckm.pojo.User"> <constructor-arg name="name" value="這個(gè)是第二種方法是有參設(shè)置的name"/> </bean>
Spring的一些配置
別名
<!--alias是別名,我們可以使用這個(gè)別名來(lái)獲取這個(gè)對(duì)象--> <alias name="user" alias="userNew"/>
Bean的配置
<!--在bean中name也是別名,可以同時(shí)取多個(gè)別名--> <bean id="user" class="com.ckm.pojo.User" name="user1,user2,user3,user4"/>
import
<!--這個(gè)import,一般用于團(tuán)隊(duì)開發(fā),他可以將多個(gè)配置文件導(dǎo)入合并成一個(gè)--> <import resource="beans1.xml"/> <import resource="beans2.xml"/>
總結(jié)
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更對(duì)內(nèi)容!
相關(guān)文章
SpringBoot JSON全局日期格式轉(zhuǎn)換器實(shí)現(xiàn)方式
這篇文章主要介紹了SpringBoot JSON全局日期格式轉(zhuǎn)換器,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04Spring?Cloud?Alibaba負(fù)載均衡實(shí)現(xiàn)方式
這篇文章主要為大家介紹了Spring?Cloud?Alibaba負(fù)載均衡實(shí)現(xiàn)方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10在IDEA中 實(shí)現(xiàn)給main方法附帶參數(shù)的操作
這篇文章主要介紹了在IDEA中 實(shí)現(xiàn)給main方法附帶參數(shù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01Java詳細(xì)分析sleep和wait方法有哪些區(qū)別
這篇文章主要介紹了Java中wait與sleep的講解(wait有參及無(wú)參區(qū)別),通過(guò)代碼介紹了wait()?與wait(?long?timeout?)?區(qū)別,wait(0)?與?sleep(0)區(qū)別,需要的朋友可以參考下2022-04-04Java如何通過(guò)反射獲取私有構(gòu)造、私有對(duì)象、私有字段、私有方法
這篇文章主要介紹了Java如何通過(guò)反射獲取私有構(gòu)造、私有對(duì)象、私有字段、私有方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12java父子節(jié)點(diǎn)parentid樹形結(jié)構(gòu)數(shù)據(jù)的規(guī)整
這篇文章主要介紹了java父子節(jié)點(diǎn)parentid樹形結(jié)構(gòu)數(shù)據(jù)的規(guī)整,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07