文章目錄
spring_2 註解、整合 Junit
一、基於註解使用 spring 的 IOC
1、 註解的入門案例
(1) 導入座標
<!--只需要spring-context-->
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
</dependencies>
(2) 持久層接口及其實現類
/**
* 賬戶的持久層接口
*/
public interface IAccountDao {
/**
* 模擬保存賬戶
*/
void saveAccount();
}
/**
* 賬戶的持久層實現類
*/
@Repository("accountDao1")
public class AccountDaoImpl implements IAccountDao {
/**
* 模擬保存賬戶
*/
public void saveAccount() {
System.out.println("保存賬戶11111111111111");
}
}
(3) 業務層接口及其實現類
/**
* 業務層接口
*/
public interface IAccountService {
public void saveAccount();
}
/**
* 業務層接口實現類
*/
@Service(value = "accountService")
public class AccountServiceImpl implements IAccountService {
// @Autowired
// @Qualifier(value = "accountDao2")
@Resource(name = "accountDao1")
private IAccountDao accountDao;
@PostConstruct
private void init(){
System.out.println("初始化方法執行了...");
}
@PreDestroy
private void destory(){
System.out.println("銷燬方法執行了...");
}
public void saveAccount(){
accountDao.saveAccount();
}
}
(4) 類路徑下創建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在創建容器時要掃描的包,它就會掃描這個包及其子包下的所有類上或接口上的註解。配置所需要的標籤不是在beans的約束中,而是一個名稱爲context名稱空間和約束中-->
<context:component-scan base-package="com.yzx"/>
</beans>
(5) 測試配置是否成功
/**
* 模擬一個表現層,用於調用業務層
*/
public class Client {
public static void main(String[] args) {
//1.獲取核心容器對象
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.根據id獲取Bean對象,兩種方式。@Service(value = "accountService"),value就是id
//value不定義就默認值是當前類名,且首字母改小寫accountServiceImpl
IAccountService as = (IAccountService) ac.getBean("accountService");
// System.out.println(as);
//
// IAccountDao adao = ac.getBean("accountDao", IAccountDao.class);
// System.out.println(adao);
as.saveAccount();
ac.close();
}
}
2、入門案例中的一些註解說明
(0) 曾經的XML配置:
<bean id="accountService" class="top.zoick.service.impl.AccountServiceImpl">
scope="" init-method="" destroy-method="">
<property name="" value="" | ref=""> </property>
</bean>
(1) 用於創建對象的
他們的作用就和在XML配置文件中編寫一個標籤實現的功能是一樣的
相當於:
@Component:
作用:用於把當前類對象存入spring容器中
屬性:
value:用於指定bean的id。當我們不寫時,它的默認值是當前類名,且首字母改小寫。
@Controller: 一般用在表現層
@Service: 一般用在業務層
@Repository: 一般用在持久層
以上三個註解他們的作用和屬性與Component是一模一樣。
他們三個是spring框架爲我們提供明確的三層使用的註解,使我們的三層對象更加清晰
(2) 用於注入數據的
他們的作用就和在xml配置文件中的bean標籤中寫一個標籤的作用是一樣的
@Autowired:
作用: 自動按類型注入。只要容器中有唯一的一個bean對象類型和要注入的變量類型匹配,就可以注入成功
如果ioc容器中沒有任何bean的類型和要注入的變量類型匹配,則報錯。
如果Ioc容器中有多個類型匹配時:參考@Qualifier
出現位置:
可以是變量上,也可以是方法上
細節:
在使用註解注入時,set方法就不是必須的了。
@service("accountService")
public class AccountServiceImpl implements IAccountService {
@Autowired//根據IAccountDao前往springioc容器中找對應類型,然後注入到這麼變量中來,從而使變量accountDao有值
private IAccountDao accountDao;// private IAccountDao accountDao = null;
// ....
}
@Qualifier:
作用: 在按照類中注入的基礎之上再按照名稱注入。它在給類成員注入時不能單獨使用,必須和 @Autowired
一起使用。但是在給方法參數注入時可以(後面會寫)
屬性:
value:用於指定注入bean的id。
@service("accountService")
public class AccountServiceImpl implements IAccountService {
@Autowired
@Qualifier("accountDao1")
private IAccountDao accountDao;
// ...
}
@Resource:
作用: 直接按照bean的id注入。它可以獨立使用
屬性:
name: 用於指定bean的id。
import javax.annotation.Resource;
@service("accountService")
public class AccountServiceImpl implements IAccountService {
// @Autowired
// @Qualifier("accountDao")
@Resource(name = "accountDao")
private IAccountDao accountDao;
// ...
}
以上三個注入都只能注入其他bean類型的數據,而基本類型和String類型無法使用上述註解實現。
另外,集合類型的注入只能通過XML來實現。
@Value:
作用: 用於注入基本類型和String類型的數據
屬性:
value:用於指定數據的值。它可以使用spring中SpEL(也就是spring的el表達式)
SpEL的寫法:${表達式}
(3) 用於改變作用範圍的
他們的作用就和在bean標籤中使用scope屬性實現的功能是一樣的
相當於:
Scope:
作用: 用於指定bean的作用範圍
屬性:
value:指定範圍的取值。常用取值:singleton
,prototype
(單例多例)
如@scope("prototype")
(4) 和生命週期相關
他們的作用就和在bean標籤中使用init-method
和destroy-methode
的作用是一樣的
相當於:
PreDestroy:
作用:用於指定銷燬方法
PostConstruct:
作用:用於指定初始化方法
二、使用 spring 的 IOC 的實現賬戶的CRUD (XML配置)
1、相關案例
框架結構
和上基本差不多
(1) 導入座標
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.6.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>8.0.15</version>
</dependency>
<!--c3p0連接池-->
<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>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
</dependency>
</dependencies>
(2) 創建數據庫和編寫實體類
create table account(
id int primary key auto_increment,
name varchar(40),
money float
)character set utf8 collate utf8_general_ci;
insert into account(name,money) values('aaa',1000);
insert into account(name,money) values('bbb',1000);
insert into account(name,money) values('ccc',1000);
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 Double getBalance() {
return balance;
}
public void setBalance(Double balance) {
this.balance = balance;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", balance=" + balance +
'}';
}
}
(3) 持久層接口及其實現類
/**
* @author zoick
* @date 2019/7/30 19:51
*/
public interface IAccountDao {
/**
* 查詢所有
* @return
*/
List<Account> findAllAccount();
/**
* 查詢一個
* @param accountId
* @return
*/
Account findAccountByID(Integer accountId);
/**
* 保存
* @param account
*/
void saveAccount(Account account);
/**
* 更新
* @param account
*/
void updateAccount(Account account);
/**
* 刪除
* @param accountId
*/
void deleteAccount(Integer accountId);
}
public class AccountDaoImpl implements IAccountDao {
private QueryRunner runner;
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);
}
}
@Override
public Account findAccountByID(Integer accountId) {
try{
return runner.query("select * from account where id = ? ",new BeanHandler<Account>(Account.class),accountId);
}catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void saveAccount(Account account) {
try{
runner.update("insert into account(name,money)values(?,?)",account.getName(),account.getMoney());
}catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void updateAccount(Account account) {
try{
runner.update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());
}catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void deleteAccount(Integer accountId) {
try{
runner.update("delete from account where id=?",accountId);
}catch (Exception e) {
throw new RuntimeException(e);
}
}
}
(4) 業務層接口及其實現類
/**
* 賬戶的業務層接口
* @author zoick
* @date 2019/7/30 19:38
*/
public interface IAccountService {
/**
* 查詢所有
* @return
*/
List<Account> findAllAccount();
/**
* 查詢一個
* @param accountId
* @return
*/
Account findAccountByID(Integer accountId);
/**
* 保存
* @param account
*/
void saveAccount(Account account);
/**
* 更新
* @param account
*/
void updateAccount(Account account);
/**
* 刪除
* @param accountId
*/
void deleteAccount(Integer accountId);
}
//賬戶的業務層實現類
public class AccountServiceImpl implements IAccountService {
private IAccountDao accountDao;
public void setAccountDao(IAccountDao accountDao) {
this.accountDao = accountDao;
}
/**
* 查詢所有
*
* @return
*/
@Override
public List<Account> findAllAccount() {
return accountDao.findAllAccount();
}
/**
* 查詢一個
*
* @param accountId
* @return
*/
@Override
public Account findAccountByID(Integer accountId) {
return accountDao.findAccountByID(accountId);
}
/**
* 保存
*
* @param account
*/
@Override
public void saveAccount(Account account) {
accountDao.saveAccount(account);
}
/**
* 更新
*
* @param account
*/
@Override
public void updateAccount(Account account) {
accountDao.updateAccount(account);
}
/**
* 刪除
*
* @param accountId
*/
@Override
public void deleteAccount(Integer accountId) {
accountDao.deleteAccount(accountId);
}
}
(4) 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置Service-->
<bean id="accountService" class="top.zoick.service.impl.AccountServiceImpl">
<!--注入dao-->
<property name="accountDao" ref="accountDao"></property>
</bean>
<!--配置dao對象-->
<bean id="accountDao" class="top.zoick.dao.impl.AccountDaoImpl">
<!--注入QueryRunner-->
<property name="runner" ref="runner"></property>
</bean>
<!--配置QueryRunner-->
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
<!--注入數據源-->
<constructor-arg name="ds" ref="dataSource" ></constructor-arg>
</bean>
<!--配置數據源-->
<bean name="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="root"></property>
</bean>
</beans>
(5) 測試
/**
* 使用Junit單元測試:測試我們的配置
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:bean.xml")
public class AccountServiceTest {
@Autowired
private IAccountService as;
@Test
public void testFindAll() {
//1.獲取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.得到業務層對象
IAccountService as = ac.getBean("accountService", IAccountService.class);
//3.執行方法
List<Account> accounts = as.findAllAccount();
for(Account account : accounts){
System.out.println(account);
}
}
@Test
public void testFindOne() {
//1.獲取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.得到業務層對象
IAccountService as = ac.getBean("accountService", IAccountService.class);
//3.執行方法
Account account = as.findAccountByID(1);
System.out.println(account);
}
@Test
public void testSave() {
Account account = new Account();
account.setName("test");
account.setMoney(12345f);
//1.獲取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.得到業務層對象
IAccountService as = ac.getBean("accountService", IAccountService.class);
//3.執行方法
as.saveAccount(account);
}
@Test
public void testUpdate() {
//1.獲取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.得到業務層對象
IAccountService as = ac.getBean("accountService", IAccountService.class);
//3.執行方法
Account account = as.findAccountByID(4);
account.setMoney(23456f);
as.updateAccount(account);
}
@Test
public void testDelete() {
//1.獲取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.得到業務層對象
IAccountService as = ac.getBean("accountService", IAccountService.class);
//3.執行方法
as.deleteAccount(4);
}
}
2、 半註解(XML + 註解)方式
如果想使用註解進行配置,那麼就可以用到上述註解
- 業務層代碼
@Service("accountService")
public class AccountServiceImpl implements IAccountService {
@Autowired
private IAccountDao accountDao;
// 不需要 set 方法...
// 業務方法省略...
}
- 持久層代碼
@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {
@Autowired
private QueryRunner runner;
// 不需要 set 方法...
// 持久化方法省略...
}
- 配置文件
<?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">
<!-- 配置註解掃描的包 -->
<context:component-scan base-package="cn.ykf"></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/db_demo"></property>
<property name="user" value="root"></property>
<property name="password" value="773620096zxc"></property>
</bean>
</beans>
無法在
QueryRunner
和ComboPooledDataSource
等第三方類上加註解,所以還是需要配置文件。
三、賬戶的CRUD ( 純註解無XML)
1、相關案例
(1) 無xml純註解配置思路
使用配置類來配置bean.xml中的相關配置
(2) 待改造的問題
我們發現,之所以我們現在離不開 xml 配置文件,是因爲我們有一句很關鍵的配置:
<!-- 告知spring框架在,讀取配置文件,創建容器時,掃描註解,依據註解創建對象,並存入容器中 -->
<context:component-scan base-package="com.itheima"></context:component-scan>
如果他要也能用註解配置,那麼我們就離脫離 xml 文件又進了一步。
另外,數據源和 QueryRunner 的配置也需要靠註解來實現。
<!--配置QueryRunner-->
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
<!--注入數據源-->
<constructor-arg name="ds" ref="dataSource" ></constructor-arg>
</bean>
<bean name="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="root"></property>
</bean>
(3) 新註解說明
@Configuration
作用:
用於指定當前類是一個 spring 配置類,它的作用和bean.xml一樣,當創建容器時會從該類上加載註解。獲取容器時需要使用
AnnotationApplicationContext(有@Configuration 註解的類.class)。
屬性:
value: 用於指定配置類的字節碼
示例:
@Configuration
public class SpringConfiguration {
}
注意:
已經把配置文件用類來代替了,但是如何配置創建容器時要掃描的包呢?
請看下一個註解 @ComponentScan。
@ComponentScan
作用:
用於指定 spring 在初始化(創建)容器時要掃描的包。作用和在 spring 的 xml 配置文件中的:
<context:component-scan base-package="com.itheima"></context:component-scan
>是一樣的。
屬性:
basePackages:用於指定要掃描的包。和該註解中的 value 屬性作用一樣。
示例:
@Configuration
@ComponentScan(basePackages = {"com.itheima"})//這是標準寫法,如果註解屬性有且只有一個值,數組類型可以省略{},且前面的value也可省略。
//@ComponentScan("com.itheima")
public class SpringConfiguration {
}
注意:
我們已經配置好了要掃描的包,但是數據源和 Runner 對象如何從配置文件中移除呢?
請看下一個註解 @Bean。
@Bean
作用:
該註解只能寫在方法上,表明使用此方法創建一個對象(把當前方法的返回值作爲bean對象存入spring ioc容器中),並且放入 spring 容器。
屬性:
name:用於指定bean的id,也就是給當前@Bean 註解方法創建的對象指定一個名稱(即 bean 的 id),默認值是當前方法名稱。id對應ioc(map結構)容器中的key,創建的對象就是value
對應bean.xml中
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
示例:
/**
* 和spring連接數據庫相關的配置類
*/
//@Configuration //當配置類作爲AnnotationConfigApplicationContext對象創建的參數時,該註解可以不寫。但是後面test時並沒有將其作爲參數,故此處不可省略
public class JdbcConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
/**
* 用於創建一個QueryRunner對象也存入容器中
* @param dataSource
* @return
*/
@Bean(name = "ruuner")
@Scope("prototype")
public QueryRunner createQueryRunner(DataSource dataSource){
return new QueryRunner(dataSource);
}
/**
* 創建數據源對象並存入spring容器中
* @return
*/
@Bean(name = "dataSource")
public DataSource createDataSource() {
try {
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setDriverClass("com.mysql.cj.jdbc.Driver");
ds.setJdbcUrl("jdbc:mysql://localhost:3306/eesy");
ds.setUser("root");
ds.setPassword("root");
return ds;
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
}
@Bean(name="ds1")
public DataSource createDataSource1(){
try {
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setDriverClass(driver);
ds.setJdbcUrl("jdbc:mysql://localhost:3306/eesy02");
ds.setUser(username);
ds.setPassword(password);
return ds;
}catch (Exception e){
throw new RuntimeException(e);
}
}
}
注意:
我們已經把數據源和 Runner 從配置文件中移除了,此時可以刪除 bean.xml 了。
但是由於沒有了配置文件,創建數據源的配置又都寫死在類中了。如何把它們配置出來呢?
請看下一個註解 @PropertySource。
@PropertySource
作用:
用於加載.properties 文件中的配置。例如我們配置數據源時,可以把連接數據庫的信息寫到
properties 配置文件中,就可以使用此註解指定 properties 配置文件的位置。
屬性:
value[]:用於指定 properties 文件位置。如果是在類路徑下,需要寫上 classpath:
示例:
上一個示例已經使用了,這個註解寫在主配置文件上
jdbc.properties 文件:
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/eesy
jdbc.username=root
jdbc.password=root
注意:
此時我們已經有了兩個配置類,但是他們還沒有關係。如何建立他們的關係呢?
請看下一個註解 @Import。
@Import
作用:
用於導入其他配置類,在引入其他配置類時,可以不用再寫@Configuration 註解。寫上也沒問題。
屬性:
value[]:用於指定其他配置類的字節碼。
@Configuration
@ComponentScan(basePackages = {"top.zoick"})
@Import(JdbcConfig.class)
@PropertySource("classpath:jdbcConfig.properties")
public class SpringConfiguration {
}
注意:
我們已經把要配置的都配置好了,但是新的問題產生了,由於沒有配置文件了,如何獲取容器呢?
請看下一小節。
(4) 通過註解獲取容器
ApplicationContext ac =
new AnnotationConfigApplicationContext(SpringConfiguration.class);
2、Spring 整合 Junit
(1) 問題
在測試類中,每個測試方法都有以下兩行代碼:
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
IAccountService as = ac.getBean("accountService",IAccountService.class);
這兩行代碼的作用是獲取容器,如果不寫的話,直接會提示空指針異常。所以又不能輕易刪掉。
(2) 解決思路分析
針對上述問題,我們需要的是程序能自動幫我們創建容器。一旦程序能自動爲我們創建 spring 容器,就
無須手動創建了,問題也就解決了。
這時,我們需要依靠 spring 框架,因爲它提供了一個運行器,可以讀取配置文件(或註解)來創建容器。我
們只需要告訴它配置文件在哪就行了。
我們是測試類,應該專注於測試功能,所以需要程序可以幫我們自己創建容器並且注入 service 對象。這時候就需要使用Spring 來整合 JUnit,步驟如下
(3) 步驟
a、導入spring-test依賴
<!-- 導入spring整合junit的jar(座標)
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
b、使用junit提供的@RunWith 註解替換原有運行器(main方法),換成spring提供的
@RunWith(SpringJUnit4ClassRunner.class)
public class IAccountServiceTest {
// ....
}
c、使用@ContextConfiguration 指定 spring 配置文件的位置
@ContextConfiguration 註解:
locations 屬性:用於指定配置文件的位置。如果是類路徑下,需要用 classpath:表明
classes 屬性:用於指定註解的類。當不使用 xml 配置時,需要用此屬性指定註解類的位置。
@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);
}
}
}