spring_2 - 注解、整合 Junit

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-methoddestroy-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>

无法在 QueryRunnerComboPooledDataSource 等第三方类上加注解,所以还是需要配置文件。

三、账户的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);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值