Java基礎之Spring5的核心之一IOC容器
1)控制反轉,把創建對象和對象的調用過程交給Spring 管理。
2)使用IOC的目的,為了降低耦合度。
二、IOC的底層原理XML解析、工廠模式、反射
基于IOC容器完成,IOC容器底層就是對象工廠。
四、Spring 提供IOC容器實現兩種方式:(兩個接口)(1)BeanFactory:IOC容器基本實現,是Spring內部的使用接口,不提供開發人員使用
特點:加載配置文件的時候不會創建對象,在獲取(使用)對象才去創建。
(2)ApplicationContext:BeanFactory接口的子接口。提供開發人員使用
特點:在加載配置文件的時候就創建對象
五、IOC操作之Bean管理1、什么是Bean管理:
Bean管理指的是兩個操作:Spring創建對象Spring注入值:手動注入、自動裝配
<!--創建對象、自動裝配 bean標簽里面的autowire屬性:屬性值:byName:根據屬性名字注入值,id的值必須與類里面的屬性名稱一樣 byType:根據屬性的類型注入值。--><bean autowire='byType'> <!--手動裝配--> <!--<property name='dept' ref='dept'></property>--></bean>
2、IOC操作之Bean管理兩種方式
1)基于XML配置文件方式:
首先創建一個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對象:
<!--1、配置User對象 id:為創建后的對象名,可自取 class:為類的全路徑 --><bean class='tianfei.Spring5.User'> <!--2、通過set方法注入屬性 通過property注入屬性 name :類里面的屬性 value :類里面屬性對應的值 --> <property name='id' value='1'></property> <property name='name' value='田飛'></property> <!--通過有參構造注入屬性--> <constructor-arg name='id' value='2'></constructor-arg> <constructor-arg name='name' value='張三'></constructor-arg> <!--通過p名稱空間注入屬性 使用前,需在xml標簽屬性中加入:xmlns:p='http://www.springframework.org/schema/p' --> <bean p: p:name='李四'></bean></bean>
測試類:
public class testDemo { @Test public void test() {//1、加載Spring5 的xml配置文件ApplicationContext context = new ClassPathXmlApplicationContext('bean1.xml');//2、獲取配置創建的對象User user = context.getBean('usera', User.class);System.out.println(user); }}
補充:在XML配置文件中引入外部文件(以jdbc.properties)
<!--引入外部文件 jdbc.properties需在名稱空間中加入:xmlns:context='http://www.springframework.org/schema/context' --> <context:property-placeholder location='classpath:jdbc.properties'></context:property-placeholder> <bean ><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.Driverprop.url=jdbc:mysql://localhost:3306/userdbprop.username=rootprop.password=tianfei
2)基于注解方式(以UserDao接口以及其實現類和UserService類為例)
public interface UserDao { public void add(); }@Controller(value = 'userDaoImpl') public class UserDaoImpl implements UserDao { @Overridepublic void add() { System.out.println('userdao add.......');} public UserDaoImpl() {} }
//value屬性可以省略,如果省略則默認創建的對象名為 類名首字母小寫 對象@Service(value = 'userService')public class UserService { //基于注解注入基本類型數據 @Value(value = 'abc') private String str; //基于注解注入對象類型值// @Autowired //根據類型注入// @Qualifier(value = 'userDaoImpl') //根據名稱注入 需要和 Autowired 一起使用// private UserDao userDao;// @Resource //根據類型注入 @Resource(name = 'userDaoImpl') //根據名稱注入 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); }}
測試類:
public class test { @Test public void test1(){//測試使用注解創建對象//需要在mxl配置文件中配置:<context:component-scan base-package='tianfei.Spring5'></context:component-scan> ,來開啟注解掃描組件//不需要添加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(){//測試使用注解創建對象//不需要添加set方法//使用完全注解ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);UserService userService = context.getBean('userService', UserService.class);System.out.println(userService);userService.add();userService.test(); }}
使用完全注解時需要創建一個類作為配置類:
@Configuration //作為配置類,代替xml配置文件,進行完全注解開發@ComponentScan(value = {'tianfei.Spring5'})public class SpringConfig {}
3、IOC操作Bean管理(工廠Bean)
Spring有兩種Bean:
1)普通Bean:在配置文件中定義bean類型就是返回值類型
2)工廠Bean:在配置文件中定義bean類型和返回值類型可以不一樣
第一步:創建一個類,作為工廠bean,實現接口FactoryBean
第二步:實現接口中得方法,在實現的方法中定義返回的bean類型
public class MyBean implements FactoryBean<User> { //用來改變返回對象(返回的對象可以和創建的對象不一樣) @Override public User getObject() throws Exception {return new User(1,'張三'); } @Override public Class<?> getObjectType() {return null; } @Override public boolean isSingleton() {return false; }}
創建 MyBean.xml文件添入:
<!--創建類的對象--><bean class='tianfei.Spring5.factorybean.MyBean'></bean>
測試類:
public class test { @Test public void testMyBean() {//測試 FactoryBean 工廠ApplicationContext context = new ClassPathXmlApplicationContext('MyBean.xml');Book book = context.getBean('myBean',Book.class);System.out.println(book); }}
4、Bean的生命周期(加上后置處理器后共七步):
(1)通過無參構造器創建bean實例(無參構造)(2)為 bean的屬性注入值或引用其他bean(set方法)(3)把 bean 實例傳遞給 bean 后置處理器方法 postProcessBeforeInitialization(4)調用 bean的初始化方法(需要自行配置初始化方法)(5)把 bean 實例傳遞給 bean 后置處理器方法 postProcessAfterInitialization(6)獲取 bean實例對象(7)當容器關閉時,調用 bean的銷毀方法(需要自行配置銷毀方法)
到此這篇關于Java基礎之Spring5的核心之一IOC容器的文章就介紹到這了,更多相關Java Spring5的核心IOC容器內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: