本文是在spring入门4的基础上,采用注解进行改进的。

基本配置

在bean.xml配置中,下面这条语句是指配置spring扫描注解的文件夹。

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

我们可以新建一个SpringConfiguration类,@Configuration指明这是一个配置类,与bean.xml等效。@ComponentScan(basePackages = {"com.itheima"})配置spring扫描注解的文件夹。

@Configuration
@ComponentScan(basePackages = {"com.itheima"})
public class SpringConfiguration {

    @Bean(name="runner")
    @Scope("prototype")
    public QueryRunner createQueryRunner(DataSource dataSource){
        return new QueryRunner(dataSource);

    }
}

配置容器对象
下面的代码是bean.xml中把QueryRunner 放入容器中的实现方式,并且用constrouctor-arg为其配置构造方法注入dataSource。
上面SpringConfiguration类中的createQueryRunner方法通过接收一个dataSource参数返回QueryRunner对象,只要再把返回对象放到spring容器中即可。 @Bean可以实现Bean化。

 <bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
        <constructor-arg name="ds" ref="dataSource"></constructor-arg>
    </bean>

事实上,上面需要接收一个dataSource参数,spring框架会去容器中查找该参数对应的bean对象,查找方式与AutoWried一样。因此我们需要把dataSource放入spring容器

@Bean(name="dataSource")
    public DataSource createDataSource(){
        try {
            ComboPooledDataSource ds = new ComboPooledDataSource();
            ds.setDriverClass("com.mysql.cj.jdbc.Driver");
            ds.setJdbcUrl("jdbc:mysql://localhost:3306/myspring?serverTimezone=GMT%2B8");
            ds.setUser("root");
            ds.setPassword("52wendyma");
            return ds;
        }catch(Exception e){
            throw new RuntimeException(e);
        }
    }

测试

注意此时需要用AnnotationConfigApplicationContext对象读取配置文件对象。

 public void testFindAll() {
        //ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
        IAccountService as = ac.getBean("accountService",IAccountService.class);
        //3.执行方法
        List<Account> accounts = as.findAllAccounts();
        for(Account account : accounts){
            System.out.println(account);
        }
    }

注意

1 Configuration可以不加
只要

  ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class, JdbcConfig.class);

在此,我们把数据库连接信息单独放到一个配置类。

public class JdbcConfig {
    @Bean(name="dataSource")
    public DataSource createDataSource(){
        try {
            ComboPooledDataSource ds = new ComboPooledDataSource();
            ds.setDriverClass("com.mysql.cj.jdbc.Driver");
            ds.setJdbcUrl("jdbc:mysql://localhost:3306/myspring?serverTimezone=GMT%2B8");
            ds.setUser("root");
            ds.setPassword("52wendyma");
            return ds;
        }catch(Exception e){
            throw new RuntimeException(e);
        }
    }

2 import导入子配置类

@Configuration
@ComponentScan(basePackages = {"com.itheima"})
@Import(JdbcConfig.class)
public class SpringConfiguration {

    @Bean(name="runner")
    @Scope("prototype")
    public QueryRunner createQueryRunner(DataSource dataSource){
        return new QueryRunner(dataSource);
    }
}

此时读取配置文件

ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);

把数据库信息提取成文件

这是对子配置类的改造。使用Value对定义的基本类型注入数据,数据源来自于jntl表达式${jdbc.driver}从配置文件jdbcConfig.properties获取的。

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

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

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

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

    @Bean(name="dataSource")
    public DataSource createDataSource() {
        try {
            ComboPooledDataSource ds = new ComboPooledDataSource();
            ds.setDriverClass(driver);
            ds.setJdbcUrl(url);
            ds.setUser(username);
            ds.setPassword(password);
            return ds;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

为了获取文件中的数据库连接信息,需要用@PropertySource配置类路径,它指明配置文件的位置。

@Configuration
@ComponentScan(basePackages = {"com.itheima"})
@Import(JdbcConfig.class)
@PropertySource("classpath:jdbcConfig.properties")//类路径
public class SpringConfiguration {

    @Bean(name="runner")
    @Scope("prototype")
    public QueryRunner createQueryRunner(@Qualifier("ds1") DataSource dataSource){
        return new QueryRunner(dataSource);
    }
}

jdbcConfig.properties文件信息如下:

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/myspring?serverTimezone=GMT%2B8
jdbc.username=root
jdbc.password=52wendyma

spring整合junit单元测试

Spring整合junit的配置
1、导入spring整合junit的jar(坐标)
2、使用Junit提供的一个注解把原有的main方法替换了,替换成spring提供的 @Runwith
3、告知spring的运行器,spring和ioc创建是基于xml还是注解的,并且说明位置

@ContextConfiguration
  locations:指定xml文件的位置,加上classpath关键字,表示在类路径下
  classes:指定注解类所在地位置

导包
首先需要导入spring-test这个jar包(导这个会出错java.lang.NoClassDefFoundError: org/springframework/core/annotation/MergedAnnotations)

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

导入下面这个

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

测试类
使用spring框架整合junit之后,测试的时候会自动注入业务层对象,无需再写重复的测试代码块(读取配置文件获得容器,从容器获取业务层对象)。可以直接进行单元测试,极大加快类开发速率。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
public class AccountServiceTest {

    @Autowired
    private IAccountService as = null;

    @Test
    public void testFindAll() {
        List<Account> accounts = as.findAllAccounts();
        for(Account account : accounts){
            System.out.println(account);
        }
    }
    @Test
    public void testFindOne() {
        Account account = as.findAccountById(1);
        System.out.println(account);
    }
    @Test
    public void testSave() {
        Account account = new Account();
        account.setName("test");
        account.setMoney(12345f);
        //3.执行方法
        as.saveAccount(account);
    }
    @Test
    public void testUpdate() {
        //3.执行方法
        Account account = as.findAccountById(4);
        account.setMoney(23456f);
        as.updateAccount(account);
    }
    @Test
    public void testDelete() {
        as.deleteAccount(4);
    }
}

wendyma
4 声望0 粉丝