- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > 編程語(yǔ)言 >
- Java基礎之Spring5的核心之一IOC容器
1)控制反轉,把創(chuàng )建對象和對象的調用過(guò)程交給Spring 管理。
2)使用IOC的目的,為了降低耦合度。
XML解析、工廠(chǎng)模式、反射
基于IOC容器完成,IOC容器底層就是對象工廠(chǎng)。
(1)BeanFactory:IOC容器基本實(shí)現,是Spring內部的使用接口,不提供開(kāi)發(fā)人員使用
特點(diǎn):加載配置文件的時(shí)候不會(huì )創(chuàng )建對象,在獲?。ㄊ褂茫ο蟛湃?chuàng )建。
(2)ApplicationContext:BeanFactory接口的子接口。提供開(kāi)發(fā)人員使用
特點(diǎn):在加載配置文件的時(shí)候就創(chuàng )建對象
1、什么是Bean管理:
Bean管理指的是兩個(gè)操作:
Spring創(chuàng )建對象
Spring注入值:手動(dòng)注入、自動(dòng)裝配
<!--創(chuàng )建對象、自動(dòng)裝配 bean標簽里面的autowire屬性: 屬性值:byName:根據屬性名字注入值,id的值必須與類(lèi)里面的屬性名稱(chēng)一樣 byType:根據屬性的類(lèi)型注入值。 --> <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類(lèi):
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對象:
<!--1、配置User對象 id:為創(chuàng )建后的對象名,可自取 class:為類(lèi)的全路徑 --> <bean id="user" class="tianfei.Spring5.User"> <!--2、通過(guò)set方法注入屬性 通過(guò)property注入屬性 name :類(lèi)里面的屬性 value :類(lèi)里面屬性對應的值 --> <property name="id" value="1"></property> <property name="name" value="田飛"></property> <!--通過(guò)有參構造注入屬性--> <constructor-arg name="id" value="2"></constructor-arg> <constructor-arg name="name" value="張三"></constructor-arg> <!--通過(guò)p名稱(chēng)空間注入屬性 使用前,需在xml標簽屬性中加入:xmlns:p="http://www.springframework.org/schema/p" --> <bean id="user" class="tianfei.Spring5.User" p:id="1" p:name="李四"></bean> </bean>
測試類(lèi):
public class testDemo { @Test public void test() { //1、加載Spring5 的xml配置文件 ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml"); //2、獲取配置創(chuàng )建的對象 User user = context.getBean("usera", User.class); System.out.println(user); } }
補充:在XML配置文件中引入外部文件(以jdbc.properties)
<!--引入外部文件 jdbc.properties 需在名稱(chēng)空間中加入: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配置文件內容如下:
prop.driverClassName=com.mysql.jdbc.Driver prop.url=jdbc:mysql://localhost:3306/userdb prop.username=root prop.password=tianfei
2)基于注解方式(以UserDao接口以及其實(shí)現類(lèi)和UserService類(lèi)為例)
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屬性可以省略,如果省略則默認創(chuàng )建的對象名為 類(lèi)名首字母小寫(xiě) 對象 @Service(value = "userService") public class UserService { //基于注解注入基本類(lèi)型數據 @Value(value = "abc") private String str; //基于注解注入對象類(lèi)型值 // @Autowired //根據類(lèi)型注入 // @Qualifier(value = "userDaoImpl") //根據名稱(chēng)注入 需要和 Autowired 一起使用 // private UserDao userDao; // @Resource //根據類(lèi)型注入 @Resource(name = "userDaoImpl") //根據名稱(chēng)注入 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); } }
測試類(lèi):
public class test { @Test public void test1(){ //測試使用注解創(chuàng )建對象 //需要在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(){ //測試使用注解創(chuàng )建對象 //不需要添加set方法 //使用完全注解 ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); UserService userService = context.getBean("userService", UserService.class); System.out.println(userService); userService.add(); userService.test(); } }
使用完全注解時(shí)需要創(chuàng )建一個(gè)類(lèi)作為配置類(lèi):
@Configuration //作為配置類(lèi),代替xml配置文件,進(jìn)行完全注解開(kāi)發(fā) @ComponentScan(value = {"tianfei.Spring5"}) public class SpringConfig { }
3、IOC操作Bean管理(工廠(chǎng)Bean)
Spring有兩種Bean:
1)普通Bean:在配置文件中定義bean類(lèi)型就是返回值類(lèi)型
2)工廠(chǎng)Bean:在配置文件中定義bean類(lèi)型和返回值類(lèi)型可以不一樣
第一步:創(chuàng )建一個(gè)類(lèi),作為工廠(chǎng)bean,實(shí)現接口FactoryBean
第二步:實(shí)現接口中得方法,在實(shí)現的方法中定義返回的bean類(lèi)型
public class MyBean implements FactoryBean<User> { //用來(lái)改變返回對象(返回的對象可以和創(chuàng )建的對象不一樣) @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 )建類(lèi)的對象--> <bean id="myBean" class="tianfei.Spring5.factorybean.MyBean"></bean>
測試類(lèi):
public class test { @Test public void testMyBean() { //測試 FactoryBean 工廠(chǎng) ApplicationContext context = new ClassPathXmlApplicationContext("MyBean.xml"); Book book = context.getBean("myBean",Book.class); System.out.println(book); } }
4、Bean的生命周期(加上后置處理器后共七步):
(1)通過(guò)無(wú)參構造器創(chuàng )建bean實(shí)例(無(wú)參構造)
(2)為 bean的屬性注入值或引用其他bean(set方法)
(3)把 bean 實(shí)例傳遞給 bean 后置處理器方法 postProcessBeforeInitialization
(4)調用 bean的初始化方法(需要自行配置初始化方法)
(5)把 bean 實(shí)例傳遞給 bean 后置處理器方法 postProcessAfterInitialization
(6)獲取 bean實(shí)例對象
(7)當容器關(guān)閉時(shí),調用 bean的銷(xiāo)毀方法(需要自行配置銷(xiāo)毀方法)
到此這篇關(guān)于Java基礎之Spring5的核心之一IOC容器的文章就介紹到這了,更多相關(guān)Java Spring5的核心IOC容器內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站