[spring]2.Spring中的IoC

1. Spring的IoC核心容器

根據xml配置文件,通過id獲取對象
pom.xml文件導入spring依賴:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>springfactory</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.12.RELEASE</version>
        </dependency>
    </dependencies>
</project>

創建bean.xml文件,把對象的創建交給spring來管理:

<?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 id="accountService" class="com.zcj2.service.impl.AccountServiceImpl" ></bean>
    <bean id="accountDao" class="com.zcj2.dao.impl.AccountDaoImpl"></bean>
</beans>

使用步驟:

  • 1.獲取spring的IoC核心容器
  • 2.通過id獲取實例化對象
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
IAccountService as = (IAccountService) ac.getBean("accountService");
IAccountDao adao = ac.getBean("accountDao",IAccountDao.class);
System.out.println(as);
System.out.println(adao);

ApplicationContext的三個常用實現類:

  • ClassPathXmlApplicationContext: 可以加載類路徑下的配置文件,要求配置文件必須在類路徑下。(更常用)
  • FileSystemXmlApplicationContext: 可以加載磁盤任意路徑下的配置文件(需要有訪問權限)
  • AnnotationConfigApplicationContext:讀取註解創建容器

ApplicationContext兩個接口引發出的問題:

  • ApplicationContext:在創建核心容器時,創建對象的策略是:立即加載。一讀取完配置文件就馬上創建配置文中配置的對象。單例對象適用,一般使用此接口。

  • BeanFactory:在創建核心容器時,創建對象的策略是:延遲加載。什麼時候用id獲取對象,就什麼時候創建對象。多例對象適用

    Resource resource = new ClassPathResource("bean.xml");
            BeanFactory factory = new XmlBeanFactory(resource);
            IAccountService as1 = (IAccountService) factory.getBean("accountService");
            System.out.println(as1);
    

2. spring對bean的管理細節

1.創建bean的三種方式

  • 1.使用默認構造函數

    在spring的配置文件中使用bean標籤,配以id和class屬性之後,且沒有其他屬性和標籤時,採用的就是默認構造函數創建bean對象,此時如果該類沒有構造函數,則無法創建對象

    <bean id="accountService" class="com.zcj3.service.impl.AccountServiceImpl" ></bean>
    
    public class AccountServiceImpl implements IAccountService {
        //這麼寫會報錯
        public AccountServiceImpl(String name){
            System.out.println("對象創建了");
        }
        public AccountServiceImpl(){
            System.out.println("對象創建了");
        }
        public void saveAccount() {
            System.out.println("保存賬戶方法執行了");
        }
    }
    
  • 2.使用工廠中的普通方法創建對象(使用某個類中的方法創建對象,並存入spring容器)(一般適用於要創建其他導入的jar包的類,他有已經寫好的構造函數,而我們無法對其源碼進行修改)

    <bean id="instanceFactory" class="com.zcj3.factory.InstanceFactory"></bean>
    <bean id="accountService" factory-bean="instanceFactory" factory-method="getService"></bean>
    

    factory-method : 創建bean對象的方法,factory-bean:爲工廠bean的id

    public class InstanceFactory {
        public IAccountService getService(){
            return new AccountServiceImpl();
        }
    }
    
  • 3.使用工廠中的靜態方法創建對象,並存入spring容器(一般適用於要創建其他導入的jar包的類,他有已經寫好的構造函數,而我們無法對其源碼進行修改)

    <bean id="accountService" class="com.zcj3.factory.StaticFactory" factory-method="getService"></bean>
    
    public class StaticFactory {
        public static IAccountService getService(){
            return new AccountServiceImpl();
        }
    }
    

2.bean的作用範圍

spring創建的bean對象默認是單例的

bean標籤的scope屬性:

  • singletone 單例(默認值)
  • prototype 多例
  • request 作用於web應用的請求範圍
  • session 作用於web應用的會話範圍
  • global-session 作用於集羣環境的會話範圍(全局會話範圍),當不是集羣環境時,他就是session
<bean id="accountService" class="com.zcj3.service.impl.AccountServiceImpl" scope="prototype"></bean>

3. bean對象的生命週期

單例對象:

  • 出生:容器創建,對象出生

  • 活着:容器還在,對象一直活着

  • 死亡:容器銷燬,對象消亡

    容器在,對象就在

多例對象:

  • 出生:當我們使用對象時spring框架爲我們創建
  • 活着:使用過程中一直活着
  • 死亡:spring不知道對象何時沒有使用,所以當對象長時間不用,沒有其他對象引用時,java的垃圾回收器回收
<bean id="accountService" class="com.zcj3.service.impl.AccountServiceImpl" scope="singleton" init-method="init" destroy-method="destory"></bean>
public class AccountServiceImpl implements IAccountService {
    public AccountServiceImpl(){
        System.out.println("對象創建了");
    }
    public void saveAccount() {
        System.out.println("保存賬戶方法執行了");
    }
    public void init(){
        System.out.println("對象初始化了");
    }
    public void destory(){
        System.out.println("對象銷燬了");
    }
/**
* 獲取spring的IOC核心容器,並根據id獲取對象
* @param args
*/
public static void main(String[] args) {
	ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
	IAccountService as = (IAccountService) ac.getBean("accountService");
	IAccountService as1 = (IAccountService) ac.getBean("accountService");
	System.out.println(as==as1);
}
對象創建了
對象初始化了
true

這是由於對象還來不及銷燬,內存就已經釋放了

我們可以手動關閉容器:

/**
* 獲取spring的IOC核心容器,並根據id獲取對象
* @param args
*/
public static void main(String[] args) {
	ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
	IAccountService as = (IAccountService) ac.getBean("accountService");
	IAccountService as1 = (IAccountService) ac.getBean("accountService");
	System.out.println(as==as1);
    ac.close();
}
對象創建了
對象初始化了
true
對象銷燬了

3. Spring中的依賴注入

依賴注入:Dependency Injection

IOC的作用:降低程序間的耦合(依賴關係)

依賴關係的管理,以後都交給spring來管理,當前類需要用到其他類的對象,由spring爲我們提供,我們只需要在配置文件中說明,而依賴關係的維護,就稱之爲依賴注入

注入數據的類型:

  • 基本類型和string
  • 其他bean類型(在配置文件中或者註解配置過的bean)
  • 複雜類型、集合類型

注入數據的方式:

1.使用構造函數

constructor-arg標籤,在bean標籤的內部,標籤中的屬性有:

  • type 用於指定要注入的數據的類型,該數據類型也是構造函數中某個或某些參數的類型

  • index 用於指定要注入的數據給構造函數中指定索引位置的參數賦值,索引的位置是從0開始‘

  • name 用於指定給構造函數中指定名稱的參數賦值 (常用)

    以上三個用於指定給構造函數中哪個參數賦值

  • value 用於提供基本類型和string類型的數據

  • ref 用於指定其他的bean類型數據,他指的就是在spring的ioc核心容器中出現過的bean對象

<bean id="accountService" class="com.zcj4.service.impl.AccountServiceImpl">
    <constructor-arg name="name" value="test"></constructor-arg>
    <constructor-arg name="age" value="18"></constructor-arg>
    <constructor-arg name="birthday" ref="now"></constructor-arg>
</bean>
<!--配置一個日期對象-->
<bean id="now" class="java.util.Date"></bean>
public class AccountServiceImpl implements IAccountService {
    //如果是經常變化的數據並不適用於注入的方式
    private String name;
    private Integer age;
    private Date birthday;
    public AccountServiceImpl(String name,Integer age,Date birthday){
        this.name =name;
        this.age = age;
        this.birthday =birthday;
    }
    public void saveAccount() {
        System.out.println("保存賬戶方法執行了"+name+","+age+","+birthday);
    }
}
public static void main(String[] args) {
	ApplicationContext ac = new ClassPathXmlApplicationContext("bean_zcj4.xml");
	IAccountService as = (IAccountService) ac.getBean("accountService");
	as.saveAccount();
}
保存賬戶方法執行了test,18,Wed Mar 11 14:24:08 CST 2020

優點:在獲取bean對象時,注入數據是必須的操作,否則對象無法創建成功

弊端:改變了bean對象的實例化方式,我們在創建對象時,如果用不到這些數據,也必須提供

2.使用set方法(更常用)

property標籤,在bean標籤的內部,標籤中的屬性有:

  • name:用於指定注入時所調用的set方法名稱,除去set後剩下的字段
  • value:提供基本類型和string類型的數據
  • ref:用於指定其他的bean類型數據,他指的就是在spring的ioc核心容器中出現過的bean對象
<bean id="accountService" class="com.zcj4.service.impl.AccountServiceImpl">
	<property name="name" value="test"></property>
	<property name="age" value="18"></property>
	<property name="birthday" ref="now"></property>
</bean>
public class AccountServiceImpl implements IAccountService {
    //如果是經常變化的數據並不適用於注入的方式
    private String name;
    private Integer age;
    private Date birthday;
    public void setName(String name) {
        this.name = name;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public void saveAccount() {
        System.out.println("保存賬戶方法執行了"+name+","+age+","+birthday);
    }
}

優點:創建對象時沒有明確的限制,可以直接使用默認構造函數

弊端:如果某個成員必須有值,則獲取對象的set方法可能沒有執行

複雜類型、集合類型的注入:

  • 用於給list結構集合注入的標籤:
    list array set
  • 用於給map結構集合注入的標籤:
    map props
  • 結構相同,標籤可以互換
public class AccountServiceImpl2 implements IAccountService {
    private String[] myStrs;
    private List<String> myList;
    private Set<String> mySet;
    private Map<String,String> myMap;
    private Properties myProps;
    public void setMyStrs(String[] myStrs) {
        this.myStrs = myStrs;
    }
    public void setMyList(List<String> myList) {
        this.myList = myList;
    }
    public void setMySet(Set<String> mySet) {
        this.mySet = mySet;
    }
    public void setMyMap(Map<String, String> myMap) {
        this.myMap = myMap;
    }
    public void setMyProps(Properties myProps) {
        this.myProps = myProps;
    }
    public void saveAccount() {
        System.out.println(Arrays.toString(myStrs));
        System.out.println(myList);
        System.out.println(mySet);
        System.out.println(myMap);
        System.out.println(myProps);
    }
}
<bean id="accountService2" class="com.zcj4.service.impl.AccountServiceImpl2">
	<property name="myStrs">
		<array>
			<value>AAA</value>
            <value>BBB</value>
            <value>AAA</value>
            <value>CCC</value>
        </array>
    </property>
    <property name="myList">
        <list>
            <value>AAA</value>
            <value>BBB</value>
            <value>AAA</value>
            <value>CCC</value>
        </list>
     </property>
     <property name="mySet">
         <set>
            <value>AAA</value>
            <value>BBB</value>
            <value>AAA</value>
            <value>CCC</value>
         </set>
     </property>
     <property name="myMap">
         <map>
             <entry key="testA" value="aaa"></entry>
             <entry key="testB">
                <value>bbbb</value>
             </entry>
         </map>
     </property>
     <property name="myProps">
         <props>
             <prop key="testc">cccc</prop>
             <prop key="testd">dddd</prop>
         </props>
     </property>
</bean>
public static void main(String[] args) {
	ApplicationContext ac = new ClassPathXmlApplicationContext("bean_zcj4.xml");
	IAccountService as = (IAccountService) ac.getBean("accountService2");
	as.saveAccount();
}
[AAA, BBB, AAA, CCC]
[AAA, BBB, AAA, CCC]
[AAA, BBB, CCC]
{testA=aaa, testB=bbbb}
{testd=dddd, testc=cccc}

3.使用註解

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
<!--    告知spring在創建容器時要掃描的包,配置所需要的標籤不是在beans的約束中,而是一個名詞爲context名稱空間和約束中-->
    <context:component-scan base-package="com.zcj"></context:component-scan>
</beans>
@Component(value = "accountService")
public class AccountServiceImpl implements IAccountService {
    private IAccountDao accountDao;
    public AccountServiceImpl(){
        System.out.println("對象創建了");
    }
}
public static void main(String[] args) {
	ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
	IAccountService as = (IAccountService) ac.getBean("accountService");
	as.saveAccount();
}

創建對象的註解:

  • Component

    作用:用於把類對象存入spring容器中

    屬性

    • value :用於指定beanid,當我們不寫value時,他的默認值是當前首字母小寫的類名。如果Component只需要這個一個屬性時,可以這樣簡寫:@Component("claaName")
  • Controller 用在表現層

  • Service用在業務層

  • Repository用在持久層

至於@Controller、@Service、@Repository他們三個是spring框架爲我們提供明確的三層使用的註解,使我們的三層對象更加清晰!

實際上這四個註解實際上沒有任何本質區別,如果你喜歡,你完全可以把這四個註解當成一個註解用

注入對象的註解:

  • Autowired:自動按照類型注入,只要容器中有唯一的一個bean對象和要注入的變量類型匹配,就可以注入成功。如果IOC容器中沒有任何bean的類型和要注入的類型匹配,則報錯。如果IOC容器中有多個類型匹配時,則根據要注入的變量名字來匹配。
    在這裏插入圖片描述

    出現的位置:可以是變量上,也可以是方法上

    在使用註解時,set方法不是必須的

    @Component
    public class AccountDaoImpl implements IAccountDao {
        public void saveAccount() {
            System.out.println("保存賬戶 ");
        }
    }
    
    @Component(value = "accountService")
    public class AccountServiceImpl implements IAccountService {
        @Autowired
        private IAccountDao accountDao;
        public AccountServiceImpl(){
            System.out.println("對象創建了");
        }
        public void saveAccount() {
            accountDao.saveAccount();
        }
    }
    

    如果出現了下面的類:

    @Component
    public class AccountDaoImpl2 implements IAccountDao {
        public void saveAccount() {
            System.out.println("保存賬戶 ");
        }
    }
    

    由於有兩個類都實現了同一個接口IAccountDao,繼續使用這樣的註解就會報錯:

    @Autowired
    private IAccountDao accountDao;
    

    可以通過修改變量名字來指定要創建的對象:

    @Autowired
    private IAccountDao accountDaoImpl;
    

    或者使用Qualifier來指定

  • Qualifier:在按照類中注入的基礎之上再按照名稱注入。它在給類成員注入時不能單獨使用(要和Autowired一起使用),但是在給方法參數注入時可以。

    屬性:value,用於指定注入bean的id

    @Autowired
    @Qualifier(value = "accountDaoImpl2")
    private IAccountDao accountDao;
    
  • Resource:直接按照bean的id注入,可以獨立使用

    屬性:name,用於指定注入bean的id

    Autowired Qualifier Resource都只能注入其他bean類型的數據,基本類型和String無法實現,集合類型的注入只能通過xml實現

  • Value:用於注入基本類型和String類型的數據

    屬性:value,用於指定數據的值,可以使用spring中的SpEL(也就是spring的el表達式)

    ​ SpEL的寫法:$(表達式)

用於改變作用範圍:

  • Scope指定bean的作用範圍

    屬性:value,取值:singletonprototype

    @Component
    @Scope("prototype")
    public class AccountDaoImpl2 implements IAccountDao {
        public void saveAccount() {
            System.out.println("保存賬戶 ");
        }
    }
    

和生命週期相關:

  • PreDestory:用於指定銷燬方法

  • PostConstruct:用於指定初始化方法

    @Component(value = "accountService")
    public class AccountServiceImpl implements IAccountService {
        @PostConstruct
        public void init() {
            accountDao.saveAccount();
        }
        @PreDestroy
        public void destory() {
            accountDao.saveAccount();
        }
    }
    

4.Spring的配置註解

  • Configuration:指定當前類是配置類

​ 當配置類作爲AnnotationConfigApplicationContext對象創建的參數時,該註解可以不寫。

  • ComponentScan:用於通過註解指定spring在創建容器時要掃描的包

​ 屬性:value:它和basePackages的作用是一樣的,都是用於制定創建容器時要掃描的包。

  • Bean:把當前的方法的返回值作爲bean對象存入spring的IOC容器中

​ 屬性:name:用於指定bean的id,當不寫時,默認值是當前方法的名稱,當用此註解配置方法時,如果方法有參數,spring框架會去容器中查找有沒有可用的bean對象,查找的方式和Autowried的方式一樣.

@Configuration
@ComponentScan("com.zcj")
public class SpringConfig {
    @Bean(name = "runner")
    public QueryRunner createQueryRunner(DataSource dataSource){
        return new QueryRunner(dataSource);
    }
    @Bean(name = "dataSource")
    public DataSource createDataSource(){
        try {
            ComboPooledDataSource ds = new ComboPooledDataSource();
            ds.setDriverClass("com.mysql.jdbc.Driver");
            ds.setJdbcUrl("jdbc:mysql://localhost:3306/eesy");
            ds.setUser("root");
            ds.setPassword("123456");
            return ds;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}

其作用相當於如下:

<bean id="accountService" class="com.zcj.service.impl.AccountServiceImpl">
    <property name="accountDao" ref="accountDao"></property>
</bean>
<bean id="accountDao" class="com.zcj.dao.impl.AccountDaoImpl">
    <property name="runner" ref="runner"></property>
</bean>
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
    <constructor-arg name="ds" ref="dataSource"></constructor-arg>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/eesy?3useUnicode=true&amp;characterEncoding=utf8"></property>
    <property name="user" value="root"></property>
    <property name="password" value="123456"></property>
</bean>

在獲取對象時,如下:

ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfig.class);
  • Import:用於導入其他的配置類

​ 屬性: value:用於指定其他配置類的字節碼。當我們使用Import的註解之後,有Import註解的類就父配置類,而導入的都是子配置類

  • PropertySource:用於指定properties文件的位置

​ 屬性:value:指定文件的名稱和路徑,關鍵字:classpath,表示類路徑下

@Import(JdbcConfig.class)
@PropertySource("classpath:jdbcConfig.properties")
public class xxx{}

5.Spring 整合junit的配置

  • 1.導入spring整合junit的jar(座標)

    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-test</artifactId>
    	<version>5.0.2.RELEASE</version>
    </dependency>
    
  • 2.使用Junit提供的一個註解 @Runwith把原有的main方法替換了,替換成spring提供的

  • 3.告知spring的運行器,spring和ioc創建是基於xml還是註解的,並且說明位置:

    @ContextConfiguration:有兩種方式:

    locations:指定xml文件的位置,加上classpath關鍵字,表示在類路徑下

    classes:指定註解類所在地位置

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = SpringConfiguration.class)
    public class AccountServiceTest {
        @Autowired
        private IAccountService as = null;
        @Test
        public void testFindAll() {
            //3.執行方法
            List<Account> accounts = as.findAllAccount();
            for(Account account : accounts){
                System.out.println(account);
            }
        }
    }
    

來源:學習–張陽2018黑馬Spring教程IDEA版

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章