Java基礎(chǔ)之Spring5的核心之一IOC容器
一、什么是IOC
1)控制反轉(zhuǎn),把創(chuàng)建對(duì)象和對(duì)象的調(diào)用過(guò)程交給Spring 管理。
2)使用IOC的目的,為了降低耦合度。
二、IOC的底層原理
XML解析、工廠模式、反射
三、IOC思想
基于IOC容器完成,IOC容器底層就是對(duì)象工廠。
四、Spring 提供IOC容器實(shí)現(xiàn)兩種方式:(兩個(gè)接口)
(1)BeanFactory:IOC容器基本實(shí)現(xiàn),是Spring內(nèi)部的使用接口,不提供開(kāi)發(fā)人員使用
特點(diǎn):加載配置文件的時(shí)候不會(huì)創(chuàng)建對(duì)象,在獲?。ㄊ褂茫?duì)象才去創(chuàng)建。
(2)ApplicationContext:BeanFactory接口的子接口。提供開(kāi)發(fā)人員使用
特點(diǎn):在加載配置文件的時(shí)候就創(chuàng)建對(duì)象
五、IOC操作之Bean管理
1、什么是Bean管理:
Bean管理指的是兩個(gè)操作:
Spring創(chuàng)建對(duì)象
Spring注入值:手動(dòng)注入、自動(dòng)裝配
<!--創(chuàng)建對(duì)象、自動(dòng)裝配 bean標(biāo)簽里面的autowire屬性: 屬性值:byName:根據(jù)屬性名字注入值,id的值必須與類里面的屬性名稱一樣 byType:根據(jù)屬性的類型注入值。 --> <bean id="employee" class="tianfei.Spring5.autowire.Employee" autowire="byType"> <!--手動(dòng)裝配--> <!--<property name="dept" ref="dept"></property>--> </bean>
2、IOC操作之Bean管理兩種方式
1)基于XML配置文件方式:
首先創(chuàng)建一個(gè)User類:
public class User { private Integer id; private String name; public User() { } public User(Integer id, String name) { this.id = id; this.name = name; } public void setId(Integer id) { this.id = id; } public void setName(String name) { this.name = name; } public void setAddress(String address) { this.address = address; } }
在XML配置文件中配置User對(duì)象:
<!--1、配置User對(duì)象 id:為創(chuàng)建后的對(duì)象名,可自取 class:為類的全路徑 --> <bean id="user" class="tianfei.Spring5.User"> <!--2、通過(guò)set方法注入屬性 通過(guò)property注入屬性 name :類里面的屬性 value :類里面屬性對(duì)應(yīng)的值 --> <property name="id" value="1"></property> <property name="name" value="田飛"></property> <!--通過(guò)有參構(gòu)造注入屬性--> <constructor-arg name="id" value="2"></constructor-arg> <constructor-arg name="name" value="張三"></constructor-arg> <!--通過(guò)p名稱空間注入屬性 使用前,需在xml標(biāo)簽屬性中加入:xmlns:p="http://www.springframework.org/schema/p" --> <bean id="user" class="tianfei.Spring5.User" p:id="1" p:name="李四"></bean> </bean>
測(cè)試類:
public class testDemo { @Test public void test() { //1、加載Spring5 的xml配置文件 ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml"); //2、獲取配置創(chuàng)建的對(duì)象 User user = context.getBean("usera", User.class); System.out.println(user); } }
補(bǔ)充:在XML配置文件中引入外部文件(以jdbc.properties)
<!--引入外部文件 jdbc.properties 需在名稱空間中加入:xmlns:context="http://www.springframework.org/schema/context" --> <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" > <property name="driverClassName" value="${prop.driverClassName}"></property> <property name="url" value="${prop.url}"></property> <property name="username" value="${prop.username}"></property> <property name="password" value="${prop.password}"></property> </bean>
jdbc.properties配置文件內(nèi)容如下:
prop.driverClassName=com.mysql.jdbc.Driver prop.url=jdbc:mysql://localhost:3306/userdb prop.username=root prop.password=tianfei
2)基于注解方式(以UserDao接口以及其實(shí)現(xiàn)類和UserService類為例)
public interface UserDao { public void add(); } @Controller(value = "userDaoImpl") public class UserDaoImpl implements UserDao { @Override public void add() { System.out.println("userdao add......."); } public UserDaoImpl() { } }
//value屬性可以省略,如果省略則默認(rèn)創(chuàng)建的對(duì)象名為 類名首字母小寫 對(duì)象 @Service(value = "userService") public class UserService { //基于注解注入基本類型數(shù)據(jù) @Value(value = "abc") private String str; //基于注解注入對(duì)象類型值 // @Autowired //根據(jù)類型注入 // @Qualifier(value = "userDaoImpl") //根據(jù)名稱注入 需要和 Autowired 一起使用 // private UserDao userDao; // @Resource //根據(jù)類型注入 @Resource(name = "userDaoImpl") //根據(jù)名稱注入 private UserDao userDao; public void add() { System.out.println("service add........"); userDao.add(); } public void test() { System.out.println("userDao = " + userDao); System.out.println(str); } }
測(cè)試類:
public class test { @Test public void test1(){ //測(cè)試使用注解創(chuàng)建對(duì)象 //需要在mxl配置文件中配置:<context:component-scan base-package="tianfei.Spring5"></context:component-scan> ,來(lái)開(kāi)啟注解掃描組件 //不需要添加set方法 ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml"); UserService userService = context.getBean("userService", UserService.class); System.out.println(userService); userService.add(); userService.test(); } @Test public void test2(){ //測(cè)試使用注解創(chuàng)建對(duì)象 //不需要添加set方法 //使用完全注解 ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); UserService userService = context.getBean("userService", UserService.class); System.out.println(userService); userService.add(); userService.test(); } }
使用完全注解時(shí)需要?jiǎng)?chuàng)建一個(gè)類作為配置類:
@Configuration //作為配置類,代替xml配置文件,進(jìn)行完全注解開(kāi)發(fā) @ComponentScan(value = {"tianfei.Spring5"}) public class SpringConfig { }
3、IOC操作Bean管理(工廠Bean)
Spring有兩種Bean:
1)普通Bean:在配置文件中定義bean類型就是返回值類型
2)工廠Bean:在配置文件中定義bean類型和返回值類型可以不一樣
第一步:創(chuàng)建一個(gè)類,作為工廠bean,實(shí)現(xiàn)接口FactoryBean
第二步:實(shí)現(xiàn)接口中得方法,在實(shí)現(xiàn)的方法中定義返回的bean類型
public class MyBean implements FactoryBean<User> { //用來(lái)改變返回對(duì)象(返回的對(duì)象可以和創(chuàng)建的對(duì)象不一樣) @Override public User getObject() throws Exception { return new User(1,"張三"); } @Override public Class<?> getObjectType() { return null; } @Override public boolean isSingleton() { return false; } }
創(chuàng)建 MyBean.xml文件添入:
<!--創(chuàng)建類的對(duì)象--> <bean id="myBean" class="tianfei.Spring5.factorybean.MyBean"></bean>
測(cè)試類:
public class test { @Test public void testMyBean() { //測(cè)試 FactoryBean 工廠 ApplicationContext context = new ClassPathXmlApplicationContext("MyBean.xml"); Book book = context.getBean("myBean",Book.class); System.out.println(book); } }
4、Bean的生命周期(加上后置處理器后共七步):
(1)通過(guò)無(wú)參構(gòu)造器創(chuàng)建bean實(shí)例(無(wú)參構(gòu)造)
(2)為 bean的屬性注入值或引用其他bean(set方法)
(3)把 bean 實(shí)例傳遞給 bean 后置處理器方法 postProcessBeforeInitialization
(4)調(diào)用 bean的初始化方法(需要自行配置初始化方法)
(5)把 bean 實(shí)例傳遞給 bean 后置處理器方法 postProcessAfterInitialization
(6)獲取 bean實(shí)例對(duì)象
(7)當(dāng)容器關(guān)閉時(shí),調(diào)用 bean的銷毀方法(需要自行配置銷毀方法)
到此這篇關(guān)于Java基礎(chǔ)之Spring5的核心之一IOC容器的文章就介紹到這了,更多相關(guān)Java Spring5的核心IOC容器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java添加事件監(jiān)聽(tīng)的四種方法代碼實(shí)例
這篇文章主要介紹了Java添加事件監(jiān)聽(tīng)的四種方法代碼實(shí)例,本文直接給出代碼示例,并用注釋說(shuō)明,需要的朋友可以參考下2014-09-09elasticsearch源碼分析index?action實(shí)現(xiàn)方式
這篇文章主要為大家介紹了elasticsearch源碼分析index?action實(shí)現(xiàn)方式,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04基于@AllArgsConstructor與@Value共用的問(wèn)題解決
這篇文章主要介紹了基于@AllArgsConstructor與@Value共用的問(wèn)題解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09一招教你使用Java執(zhí)行g(shù)roovy腳本的兩種方式
本文主要介紹了一招教你使用Java執(zhí)行g(shù)roovy腳本的兩種方式,一種是通過(guò)腳本引擎ScriptEngine提供的eval(String)方法執(zhí)行腳本內(nèi)容,一種是執(zhí)行g(shù)roovy腳本,感興趣的可以了解一下2023-09-09Java深入了解數(shù)據(jù)結(jié)構(gòu)之二叉搜索樹(shù)增 插 刪 創(chuàng)詳解
二叉搜索樹(shù)是以一棵二叉樹(shù)來(lái)組織的。每個(gè)節(jié)點(diǎn)是一個(gè)對(duì)象,包含的屬性有l(wèi)eft,right,p和key,其中,left指向該節(jié)點(diǎn)的左孩子,right指向該節(jié)點(diǎn)的右孩子,p指向該節(jié)點(diǎn)的父節(jié)點(diǎn),key是它的值2022-01-01Spring Boot和Docker實(shí)現(xiàn)微服務(wù)部署的方法
這篇文章主要介紹了Spring Boot和Docker實(shí)現(xiàn)微服務(wù)部署的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01Java 過(guò)濾器實(shí)現(xiàn)敏感詞匯過(guò)濾功能
通過(guò)使用 Java 過(guò)濾器,我們可以輕松地實(shí)現(xiàn)敏感詞匯過(guò)濾的功能,以保護(hù)用戶免受不良內(nèi)容的侵害,讓我們通過(guò)一個(gè)簡(jiǎn)單的示例來(lái)演示我們的敏感詞匯過(guò)濾器是如何工作的,感興趣的朋友一起看看吧2024-01-01