在上一篇文章我们对 Spring 基于注解的 IoC有了一定的了解,现在我们来看看一个简单的案例。这个案例将有三种方式实现,分别是XML,半注解,纯注解。看完案例之后就了解一下Spring 整合 JUnit。
<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.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>Spring_Day02_anno_iocartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>jarpackaging>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.0.2.RELEASEversion>
dependency>
<dependency>
<groupId>commons-dbutilsgroupId>
<artifactId>commons-dbutilsartifactId>
<version>1.4version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.20version>
dependency>
<dependency>
<groupId>c3p0groupId>
<artifactId>c3p0artifactId>
<version>0.9.1.2version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.10version>
dependency>
dependencies>
project>
导入过程有错的,大家可以去看看我的这两篇文章。传送门1 传送门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('张三',1000);
insert into account(name,money) values('李四',1000);
insert into account(name,money) values('一个Java小白',1000);
package com.cz.domain;
import javax.annotation.PreDestroy;
import java.io.Serializable;
/**
* 账户的实体类
*/
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 +
'}';
}
}
业务层接口:
package com.cz.service;
import com.cz.domain.Account;
import java.util.List;
/**
* 账户业务层接口
*/
public interface AccountService {
/**
* 查询所有账户
* @return
*/
List<Account> findAllAccount();
/**
* 根据id查询账户
* @param accountId
* @return
*/
Account findAccountById(Integer accountId);
/**
* 保存
* @param account
* @return
*/
int saveAccount(Account account);
/**
* 更新
* @param account
* @return
*/
int updateAccount(Account account);
/**
* 删除
* @param accountId
* @return
*/
int removeAccount(Integer accountId);
}
实现类:
package com.cz.service.impl;
import com.cz.dao.AccountDao;
import com.cz.domain.Account;
import com.cz.service.AccountService;
import java.util.List;
/**
* 账户的业务层实现类
*/
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
public List<Account> findAllAccount() {
return accountDao.findAllAccount();
}
public Account findAccountById(Integer accountId) {
return accountDao.findAccountById(accountId);
}
public int saveAccount(Account account) {
return accountDao.saveAccount(account);
}
public int updateAccount(Account account) {
return accountDao.updateAccount(account);
}
public int removeAccount(Integer accountId) {
return accountDao.removeAccount(accountId);
}
}
持久层接口:
package com.cz.dao;
import com.cz.domain.Account;
import java.util.List;
/**
* 账户的持久层接口
*/
public interface AccountDao {
/**
* 查询所有账户
* @return
*/
List<Account> findAllAccount();
/**
* 根据id查询账户
* @param accountId
* @return
*/
Account findAccountById(Integer accountId);
/**
* 保存
* @param account
* @return
*/
int saveAccount(Account account);
/**
* 更新
* @param account
* @return
*/
int updateAccount(Account account);
/**
* 删除
* @param accountId
* @return
*/
int removeAccount(Integer accountId);
}
实现类:
package com.cz.dao.impl;
import com.cz.dao.AccountDao;
import com.cz.domain.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.util.List;
/**
* 账户的持久层实现类
*/
public class AccountDaoImpl implements AccountDao {
private QueryRunner runner;
public void setRunner(QueryRunner runner) {
this.runner = runner;
}
public List<Account> findAllAccount() {
try{
return runner.query("select * from account",new BeanListHandler<Account>(Account.class));
}catch (Exception e){
throw new RuntimeException(e);
}
}
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);
}
}
public int saveAccount(Account account) {
try{
return runner.update("insert into account(name,money)values(?,? )",account.getName(),account.getMoney());
}catch (Exception e){
throw new RuntimeException(e);
}
}
public int updateAccount(Account account) {
try{
return runner.update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());
}catch (Exception e){
throw new RuntimeException(e);
}
}
public int removeAccount(Integer accountId) {
try{
return runner.update("delete from account where id=?",accountId);
}catch (Exception e){
throw new RuntimeException(e);
}
}
}
bean.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">
<bean id="accountService" class="com.cz.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao">property>
bean>
<bean id="accountDao" class="com.cz.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.cj.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring?serverTimezone=UTC&characterEncoding=utf-8"/>
<property name="user" value="root"/>
<property name="password" value="123456"/>
bean>
beans>
package com.cz.test;
import com.cz.domain.Account;
import com.cz.service.AccountService;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
/**
* 使用JUnit单元测试,测试我们的配置
*/
public class AccountServiceTest {
private ApplicationContext ac;
private AccountService accountService;
@Before
public void init(){
//1.获取容器
ac = new ClassPathXmlApplicationContext("bean.xml");
//2.得到业务层service对象
accountService = ac.getBean("accountService",AccountService.class);
}
@Test
public void testFindAll(){
//执行方法
List<Account> accounts = accountService.findAllAccount();
for (Account account : accounts){
System.out.println(account);
}
}
@Test
public void testFindOne(){
//执行方法
Account account = accountService.findAccountById(1);
System.out.println(account);
}
@Test
public void testSave(){
//执行方法
Account account = new Account();
account.setName("王五");
account.setMoney(800000f);
accountService.saveAccount(account);
//下面这个方法也是可以的,下面的更新,删除也适用
// int row = accountService.saveAccount(account);
// Assert.assertEquals(1,row);
}
@Test
public void testUpDate(){
//执行方法
Account account = accountService.findAccountById(3);
account.setMoney(9000f);
accountService.updateAccount(account);
}
@Test
public void testDelete(){
//执行方法
accountService.removeAccount(4);
}
}
通过上面的测试类,我们把容器的获取定义到类中去。但仍需要我们自己写代码来获取容器。能不能测试时直接就编写测试方法,而不需要手动编码来获取容器呢?
如果想使用注解进行配置,那么就可以用到上篇文章讲解到的注解传送门。
/**
* 账户的业务层实现类
*/
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
// 不需要 set 方法...
// 业务方法跟上面一样...
**
* 账户的持久层实现类
*/
@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {
@Autowired
private QueryRunner runner;
// 不需要 set 方法...
// 业务方法跟上面一样...
<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="com.cz">context:component-scan>
<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.cj.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring?serverTimezone=UTC&characterEncoding=utf-8"/>
<property name="user" value="root"/>
<property name="password" value="7107883"/>
bean>
beans>
在这个方法中,无法在 QueryRunner 和 ComboPooledDataSource 等第三方类上加注解,所以还是需要配置文件。
如果想要使用纯注解,那么我们就需要将配置文件中还存在的配置也使用注解配置,这时候就需要一些新的注解了。
@Configuration 注解
Spring 配置类
,当创建容器时会从该类上加载注解value
: 用于指定配置类的字节码AnnotationApplicationContext(有 @Configuration 注解的类.class)
,写了这个之后可以不用写@Configuration
注解/**
* 该类是一个配置类,他的作用和bean.xml是一样的
*/
@Configuration
public class SpringConfiguration {
// ...
}
我们已经把配置文件用类来代替了,但是如何配置创建容器时要扫描的包呢?请看下一个注解。
@ComponentScan 注解
value / basePackages
:两者都是用来指定要扫描的包。使用此注解等同于在xml中配置了:
/**
* 该类是一个配置类,他的作用和bean.xml是一样的
*/
@Configuration
@ComponentScan(basePackages = "com.cz")
public class SpringConfiguration {
}
我们已经配置好了要扫描的包,但是
数据源和 QueryRunner
对象如何从配置文件中移除呢?请看下一个注解。
@Bean
name
:用于指定 Bean的id
,默认值是当前方法的名称@Autowired
的作用是一样package config;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import javax.sql.DataSource;
import java.awt.color.ProfileDataException;
import java.beans.PropertyVetoException;
import java.util.Date;
/**
* 该类是一个配置类,他的作用和bean.xml是一样的
*/
@Configuration
@ComponentScan(basePackages = "com.cz")
public class SpringConfiguration {
/**
* 用于创建一个QueryRunner对象并且放入 ioc 容器中,多例
* @param dataSource
* @return
*/
@Bean(name = "runner")
@Scope("prototype")
public QueryRunner creatQueryRunner(DataSource dataSource){
return new QueryRunner(dataSource);
}
/**
* 创建一个数据源并且放入 ioc 容器中
*
* @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/spring?serverTimezone=UTC&characterEncoding=utf-8");
ds.setUser("root");
ds.setPassword("7107883");
return ds;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
到这里我们可以删除
bean.xml
了。但是由于没有了配置文件,创建数据源的配置又都写死在类中了。如何把它们配置出来呢?请看下一个注解。
@PropertySource 注解
xxxxx.properties
文件中的配置value[]
:用于指定配置文件的名称和位置classpath:
配置文件jdbcConfig.properties
:
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring?serverTimezone=UTC&characterEncoding=utf-8
jdbc.user=root
jdbc.password=123456
package config;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Scope;
import javax.sql.DataSource;
/**
*Spring 连接 数据库的配置类
*/
@PropertySource("classpath:jdbcConfig.properties")
public class JdbcConfig {
@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对象并且放入 ioc 容器中,多例
*
* @param dataSource
* @return
*/
@Bean(name = "runner")
@Scope("prototype")
public QueryRunner creatQueryRunner(DataSource dataSource) {
return new QueryRunner(dataSource);
}
/**
* 创建一个数据源并且放入 ioc 容器中
*
* @return
*/
@Bean(name = "dataSource")
public DataSource createDataSource() {
try {
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setDriverClass(driver);
ds.setJdbcUrl(url);
ds.setUser(user);
ds.setPassword(password);
return ds;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
此时我们已经有了两个配置类,但是他们还没有关系。如何建立他们的关系呢?请看下一个注解。
@Import
@Configuration
注解均可value[]
:用于指定其他配置类的字节码/**
* 该类是一个配置类,他的作用和bean.xml是一样的
*/
@Configuration
@ComponentScan(basePackages = "com.cz")
@Import(JdbcConfig.class)
public class SpringConfiguration{
}
我们已经把要配置的都配置好了,但是新的问题产生了,由于没有配置文件了,如何获取容器呢?
ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
//1.获取容器
ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
//2.得到业务层service对象
accountService = ac.getBean("accountService", AccountService.class);
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>5.0.2.RELEASEversion>
dependency>
当我们使用spring 5.x版本的时候,要求junit的版本必须是4.12及以上
@RunWith
注解替换 JUnit 原本的运行器@RunWith(SpringJUnit4ClassRunner.class)
public class AccountServiceTest {
// ....
}
@ContextConfiguration
指定 Spring 配置文件或者配置类的位置@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
public class AccountServiceTest {
}
@ContextConfiguration 注解
locations 属性
:指定 XML 文件的位置,加上classpath:
关键字表示在类路径下classes 属性
:指定注解类所在的位置@Autowired
注入 service 对象,最终代码如下@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
public class AccountServiceTest {
@Autowired
private AccountService service;
// 测试方法...
}