天天看點

主流架構二:Spring(3)Spring基于注解的IOC和案例分析一、案例使用XML方式實作單表的CRUD操作二、Spring中的IOC常用注解和注解方式實作單表的CRUD操作三、基于注解的純注解方式的IOC案例四、Spring和Junit的整合

主流架構二:Spring(3)Spring基于注解的IOC和案例分析

  • 一、案例使用XML方式實作單表的CRUD操作
    • 1、案例準備:
    • 2、Spring的IOC配置
    • 3、測試類
    • 4、分析問題
  • 二、Spring中的IOC常用注解和注解方式實作單表的CRUD操作
    • 1、環境搭建
    • 2、常用注解
    • 3、關于 Spring 注解和 XML 的比較
    • 4、注解方式實作單表的CRUD操作
  • 三、基于注解的純注解方式的IOC案例
    • 1、新增注解
    • 2、案例代碼
  • 四、Spring和Junit的整合
    • 1、解決思路
    • 2、整合步驟

一、案例使用XML方式實作單表的CRUD操作

持久層技術的選擇:dbutils

1、案例準備:

(1)導入包坐标

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-dbutils</groupId>
            <artifactId>commons-dbutils</artifactId>
            <version>1.4</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>

        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>
           

(2)編寫domain實體類

/**
 * 賬戶的實體類
 */
public class Account implements Serializable {

    private Integer id;
    private String name;
    private Float money;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Float getMoney() {
        return money;
    }

    public void setMoney(Float money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}
           

(3)編寫dao和service的接口和實作類

/**
 * 賬戶的業務層接口
 */
public interface IAccountService {

    /**
     * 查詢所有
     * @return
     */
    List<Account> findAllAccount();
    
    ......   
}
           
/**
 * 賬戶的業務層實作類(調用dao層的方式實作CRUD)
 * 需要使用dao對象(注入依賴)
 */
public class AccountServiceImpl implements IAccountService{

    private IAccountDao accountDao;

    // xml配置就需要set方法注入依賴
    public void setAccountDao(IAccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public List<Account> findAllAccount() {
        return accountDao.findAllAccount();
    }

	......
}
           

dao層的實作類:用到DBUtils

/**
 * 賬戶的持久層實作類
 */
public class AccountDaoImpl implements IAccountDao {

	//也是需要引入一個QueryRunner的依賴
    private QueryRunner runner;

    //也要注入set方法
    public void setRunner(QueryRunner runner) {
        this.runner = runner;
    }

    @Override
    public List<Account> findAllAccount() {
        try{
            return runner.query("select * from account",new BeanListHandler<Account>(Account.class));
        }catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
           

2、Spring的IOC配置

(1)在Spring官網引入xml的頭

<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">
</beans>
           

(2)配置那些依賴對象,例如service和dao以及QueryRunner

<!-- 配置Service -->
    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
        <!-- 注入dao -->
        <property name="accountDao" ref="accountDao"></property>
    </bean>

    <!--配置Dao對象-->
    <bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl">
        <!-- 注入QueryRunner -->
        <property name="runner" ref="runner"></property>
    </bean>

    <!--配置QueryRunner-->
    //多個dao使用runner,則使用多例對象來實作多線程 
    <bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
        <!--注入資料源(連接配接池)-->
        //runner在包中,不能使用set方法注入,隻能使用構造函數方法注入
        <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"></property>
        <property name="user" value="root"></property>
        <property name="password" value="xmgl0609"></property>
    </bean>
           

3、測試類

/**
* 測試類
*/
public class AccountServiceTest {

	/**
	* 測試查詢所有
	*/
	@Test
	public void testFindAllAccount() {
		//1.建立容器
		ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
		//2.得到業務層的對象
		IAccountService as = ac.getBean("accountService",IAccountService.class);
		//3.執行方法
		List<Account> list = as.findAllAccount();
		for(Account account : list) {
			System.out.println(account);
		}
	}

	......
}
           

4、分析問題

通過上面的測試類,我們可以看出,每個測試方法都重新擷取了一次 spring 的核心容器,造成了不必要的重複代碼,增加了我們開發的工作。這種情況,在開發中應該避免發生。

有些同學可能想到了,我們把容器的擷取定義到類中去。例如:

/**
* 測試類
*/
public class AccountServiceTest {

	private ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
	private IAccountService as = ac.getBean("accountService",IAccountService.class);
		.......
}
           

這種方式雖然能解決問題,但是仍然需要我們自己寫代碼來擷取容器。

能不能測試時直接就編寫測試方法,而不需要手動編碼來擷取容器呢?

二、Spring中的IOC常用注解和注解方式實作單表的CRUD操作

學習基于注解的 IoC 配置,大家腦海裡首先得有一個認知,即注解配置和 xml 配置要實作的功能都是一樣的,都是要降低程式間的耦合。隻是配置的形式不一樣。

關于實際的開發中到底使用xml還是注解,每家公司有着不同的使用習慣。是以這兩種配置方式我們都需要掌握。

曾經XML的配置:(注解隻是實作這些功能)

<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"
       scope=""  init-method="" destroy-method="">
      <property name=""  value="" | ref=""></property>
</bean>
           

1、環境搭建

第一步:拷貝必備 jar 包到工程的 lib 目錄。

注意:在基于注解的配置中,我們還要多拷貝一個 aop 的 jar 包。如下圖:

主流架構二:Spring(3)Spring基于注解的IOC和案例分析一、案例使用XML方式實作單表的CRUD操作二、Spring中的IOC常用注解和注解方式實作單表的CRUD操作三、基于注解的純注解方式的IOC案例四、Spring和Junit的整合

第二步:使用@Component 注解配置管理的資源

/**
* 賬戶的業務層實作類
*/
@Component("accountService")
public class AccountServiceImpl implements IAccountService {

	private IAccountDao accountDao;
	public void setAccountDao(IAccountDao accountDao) {
		this.accountDao = accountDao;
	}
}
/**
* 賬戶的持久層實作類
*/
@Component("accountDao")
	public class AccountDaoImpl implements IAccountDao {
	private DBAssit dbAssit;
}
           

注意:當我們使用注解注入時,set 方法不用寫

第三步:建立 spring的xml配置檔案并開啟對注解的支援

注意:基于注解整合時,導入限制時需要多導入一個 context 名稱空間下的限制。

由于我們使用了注解配置,此時不能在繼承 JdbcDaoSupport,需要自己配置一個 JdbcTemplate

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<!-- 告知 spring 建立容器時要掃描的包 -->
<context:component-scan base-package="com.supuEmbedded"></context:component-scan>

</beans>
           

2、常用注解

(1)用于建立對象的

他們的作用就和在XML配置檔案中編寫一個

< bean>

标簽實作的功能是一樣的

@Component:

作用:用于把目前類對象存入spring容器中友善在另一層或類中擷取。

屬性:

value:用于指定bean的id。當我們不寫時,它的預設值是目前類名,且首字母改小寫。

(常用)

@Controller:一般用在表現層

@Service:一般用在業務層

@Repository:一般用在持久層

以上三個注解他們的作用和屬性與Component是一模一樣。他們三個是spring架構為我們提供明确的三層使用的注解,使我們的三層對象更加清晰。

(2)用于注入資料的

他們的作用就和在xml配置檔案中的bean标簽中寫一個

<property>

标簽的作用是一樣的。

細節:在使用注解注入時,set方法就不是必須的了。

[email protected]:

作用:自動按照類型注入。隻要容器中有唯一的一個bean對象類型和要注入的變量類型比對,就可以注入成功

如果ioc容器中沒有任何bean的類型和要注入的變量類型比對,則報錯。

如果Ioc容器中有多個類型比對時:

主流架構二:Spring(3)Spring基于注解的IOC和案例分析一、案例使用XML方式實作單表的CRUD操作二、Spring中的IOC常用注解和注解方式實作單表的CRUD操作三、基于注解的純注解方式的IOC案例四、Spring和Junit的整合

出現位置:

可以是變量上,也可以是方法上

[email protected]:(與Autowired一起使用)

作用:在按照類中注入的基礎之上再按照名稱注入。它在給類成員注入時不能單獨使用。但是在給方法參數注入時可以

屬性:

value:用于指定注入bean的id。

[email protected](常用)

作用:直接按照bean的id注入。它可以獨立使用

屬性:

name:用于指定bean的id。

以上三個注入都隻能注入其他bean類型的資料,而基本類型和String類型無法使用上述注解實作。

另外,集合類型的注入隻能通過XML來實作。

[email protected]

作用:用于注入基本類型和String類型的資料

屬性:

value:用于指定資料的值。它可以使用spring中SpEL(也就是spring的el表達式)

SpEL的寫法:${表達式}從Spring中擷取資料

(3)用于改變作用範圍的

他們的作用就和在bean标簽中使用scope屬性實作的功能是一樣的

@Scope

作用:用于指定bean的作用範圍

屬性:

value:指定範圍的取值。常用取值:singleton prototype(單例多例)

(4)和生命周期相關 了解

他們的作用就和在bean标簽中使用init-method和destroy-methode的作用是一樣的。

@PostConstruct

作用:用于指定初始化方法

@PreDestroy

作用:用于指定銷毀方法

3、關于 Spring 注解和 XML 的比較

注解的優勢:配置簡單,維護友善(我們找到類,就相當于找到了對應的配置)。

XML 的優勢:修改時,不用改源碼。不涉及重新編譯和部署。

Spring 管理 Bean 方式的比較:

主流架構二:Spring(3)Spring基于注解的IOC和案例分析一、案例使用XML方式實作單表的CRUD操作二、Spring中的IOC常用注解和注解方式實作單表的CRUD操作三、基于注解的純注解方式的IOC案例四、Spring和Junit的整合

4、注解方式實作單表的CRUD操作

使用注解方法實作時:

(1)實作類不用寫set方法,直接使用注解引入依賴

@Service(“accountService”)和 @Repository(“accountDao”)代表業務層和持久層

@Autowired表示自動查找注入依賴(隻有一個時使用)

/**
 * 賬戶的業務層實作類
 */
@Service("accountService")
public class AccountServiceImpl implements IAccountService{

    @Autowired
    private IAccountDao accountDao;

    @Override
    public List<Account> findAllAccount() {
        return accountDao.findAllAccount();
    }
    ......
}
           
/**
 * 賬戶的持久層實作類
 */
@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {

    @Autowired
    private QueryRunner runner;

    
    @Override
    public List<Account> findAllAccount() {
        try{
            return runner.query("select * from account",new BeanListHandler<Account>(Account.class));
        }catch (Exception e) {
            throw new RuntimeException(e);
        }
        ......
}
           

(2)IOC配置修改:

1、需要引入注解xml的頭,在Spring官網

2、不需要再注入servcie等的依賴關系,但資料源(連接配接池)的依賴仍然需要注入,不能在包中寫注解

3、需要提前告知spring在建立容器時要掃描的包

<context:component-scan base-package="com.itheima"></context:component-scan>

故bean.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"
       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在建立容器時要掃描的包 -->
    <context:component-scan base-package="com.itheima"></context:component-scan>
    <!--配置QueryRunner-->
    <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"></property>
        <property name="user" value="root"></property>
        <property name="password" value="1234"></property>
    </bean>
</beans>
           

(3)測試即可…

三、基于注解的純注解方式的IOC案例

1、新增注解

(1)@Configuration

作用:指定目前類是一個配置類(此為bean.xml)

細節:當配置類作為AnnotationConfigApplicationContext()對象建立的參數時,該注解可以不寫

(2)@ ConponentScan

作用:用于通過注解指定Spring在建立容器時要掃描的包

屬性:

value:它和basePackages的作用一樣,都是用于指定建立容器時要建立的包

則使用此注解就等同于在xml中配置了 <context:component-scan base-package=“com.itheima”></context:component-scan>

(3) Bean(作用于 包中不能寫注解的對象 )

作用:用于把目前方法的傳回值作為bean對象存入Spring的ioc容器中

屬性:

name:用于指定bean的id,預設值是目前方法的名稱

細節:當使用注解配置方法時,如果方法有參數,spring架構會去容器中查找有沒有可用的bean對象

查找的方式與Autowired注解的作用是一緻的

例如:

1.配置QueryRunner進IOC容器時(操作資料庫對象)

<!--配置QueryRunner-->  
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
	<!--注入資料源-->
	<constructor-arg name="ds" ref="dataSource"></constructor-arg>
</bean>
           

之後就可以直接寫 @Bean(name = “runner”)

@Bean(name = "runner")
@Scope(value = "prototype")
public QueryRunner createQueryRunner(@Qualifier("dataSource")DataSource dataSource) {
        //實際上DataSource參數前面有Autowired
        return new QueryRunner(dataSource);
    }
           

2.配置資料源進IOC容器時

<!-- 配置資料源 -->
<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"></property>
	<property name="user" value="root"></property>
	<property name="password" value="xmgl0609"></property>
</bean>
           

之後就可以直接寫

@Bean(name = "dataSource")
    public DataSource creatDataSource1() {

        ComboPooledDataSource ds = new ComboPooledDataSource();
        try {
            ds.setDriverClass(driver);
            ds.setJdbcUrl(url);
            ds.setUser(user);
            ds.setPassword(password);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
        return ds;
    }
           

(4) AnnotationConfigApplicationContext()

通過注解擷取IOC容器時,測試類必須通過此才能擷取容器,進而得到Service對象

//1.擷取容器
ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
//2.得到業務層對象
IAccountService as = ac.getBean("accountService",IAccountService.class);
           

(5)Import(多個配置類,例如:jdbc,事務配置類)

一個SpringConfiguration,可能有多個小的配置類:jdbc,事務配置類

作用:導入其他的配置類,寫在總的配置類中

屬性:

value:用于指定其他配置類的位元組碼

當我們使用import的注解之後,有import注解的類就父配置類,而導入的都是子配置類

例如:

(6) PropertySource

作用:用于指定properties檔案的位置

可能會将一些配置資訊,存入properties 檔案中,配置類從檔案中擷取。進而讓配置類定位配置資訊檔案。

屬性:

value:指定檔案的名稱和路徑

關鍵字:classpath表示類路徑下

2、案例代碼

(1)結構:

主流架構二:Spring(3)Spring基于注解的IOC和案例分析一、案例使用XML方式實作單表的CRUD操作二、Spring中的IOC常用注解和注解方式實作單表的CRUD操作三、基于注解的純注解方式的IOC案例四、Spring和Junit的整合

(2)代碼如下:

1、配置檔案

/**
 * 該類是一個配置類,作用于bean.xml作用一樣
 * @author Mango
 */
@Configuration
@ComponentScan(basePackages = "com.itheima")
@PropertySource("classpath:jdbcConfig.properties")
public class SpringConfiguration {

	//使用PropertySource擷取配置檔案資訊
    @Value("${jdbc.driver}")
    private String driver;

    @Value("${jdbc.url}")
    private String url;

    @Value("${jdbc.user}")
    private String user;

    @Value("${jdbc.password}")
    private String password;

    /**
     * 用于建立一個QueryRunner對象
     * @param dataSource
     * @return
     */
    @Bean(name = "runner")
    @Scope(value = "prototype")
    //采用多例對象,可能有 多個dao使用多個QueryRunner
    public QueryRunner createQueryRunner(@Qualifier("ds1")DataSource dataSource) {
        //實際上DataSource參數前面有Autowired
        return new QueryRunner(dataSource);
    }

    /**
     * 建立資料源對象
     * @return
     */
    @Bean(name = "dataSource")
    public DataSource creatDataSource() {

        ComboPooledDataSource ds = new ComboPooledDataSource();
        try {
            ds.setDriverClass(driver);
            ds.setJdbcUrl(url);
            ds.setUser(user);
            ds.setPassword(password);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
        return ds;
    }

    /**
     * 建立資料源對象
     * @return
     */
    @Bean(name = "ds1")
    public DataSource creatDataSource1() {

        ComboPooledDataSource ds = new ComboPooledDataSource();
        try {
            ds.setDriverClass(driver);
            ds.setJdbcUrl("jdbc:mysql://localhost:3306/eesy02");
            ds.setUser(user);
            ds.setPassword(password);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
        return ds;
    }
}
           

2、dao和service直接從Spring容器中擷取對象

**
 * 賬戶的持久層實作類
 */
@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {

    @Autowired
    private QueryRunner runner;



    @Override
    public List<Account> findAllAccount() {
        try{
            return runner.query("select * from account",new BeanListHandler<Account>(Account.class));
        }catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
	......
}
           
**
 * 賬戶的業務層實作類
 */
@Service("accountService")
public class AccountServiceImpl implements IAccountService{

    @Autowired
    private IAccountDao accountDao;

    @Override
    public List<Account> findAllAccount() {
        return accountDao.findAllAccount();
    }
    ......
}
           

3、分析測試類中的問題

通過上面的測試類,我們可以看出,每個測試方法都重新擷取了一次 spring 的核心容器,造成了不必要的重複代碼,增加了我們開發的工作量。這種情況,在開發中應該避免發生。

有些同學可能想到了,我們把容器的擷取定義到類中去。例如:

/**
* 測試類
*/
public class AccountServiceTest {
	//1.手動擷取容器
	private ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
	//2.得到業務層對象
	private IAccountService as = ac.getBean("accountService",IAccountService.class);
	......	
}
           

這種方式雖然能解決問題,但是扔需要我們自己寫代碼來擷取容器。能不能測試時直接就編寫測試方法,而不需要手動編碼來擷取容器呢?

我們可以使用junit和spring進行整合(合體)

四、Spring和Junit的整合

1、解決思路

針對上述問題,我們需要的是程式能自動幫我們建立容器。

我們知道,junit 單元測試的原理(在 web 階段課程中講過),但顯然,junit 是無法實作的,因為它自己都無法知曉我們是否使用了 spring 架構,更不用說幫我們建立 spring 容器了。不過好在,junit 給我們暴露了一個注解,可以讓我們替換掉它的運作器。

這時,我們需要依靠 spring 架構,因為它提供了一個運作器,可以讀取配置檔案(或注解)來建立容器。我們隻需要告訴它配置檔案在哪就行了。

2、整合步驟

(1)導入Spring整合junit的jar包(與Spring配套的junit測試類的包)

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.0.2.RELEASE</version>
</dependency>
           

(2)使用junit提供的注解 @Runwith 使得原有的main方法替換成Spring提供的

(3)使用 @ContextConfiguration 告知spring的運作器,spring和ioc建立時基于xml還是注解的,并且說明位置

locations:指定xml檔案的位置,加上classpath關鍵字表示在類路徑下(xml配置)

例如:locations = classpath:bean.xml

classes:指定注解類所在地位置的class檔案(注解配置)

(4)使用**@Autowired** 給測試類中的變量注入資料

直接可以使用@Autowired 來擷取需要的對象(例如:業務層對象)

//自動建立SpringIoc容器
@RunWith(SpringJUnit4ClassRunner.class)
//指定配置類的位置
@ContextConfiguration(classes = SpringConfiguration.class)

public class AccountServiceTest {
	@Autowired
    private IAccountService as ;

    @Test
    public void testFindAll() {
        //3.執行方法
        List<Account> accounts = as.findAllAccount();
        for(Account account : accounts){
            System.out.println(account);
        }
    }
    ......
}