一、Spring中的注解(用于替换xml文件中的配置内容)
Configuration:
作用:指定当前类是一个配置类
细节:当配置类作为AnnotationConfigApplicationContext对象创建的参数时,该注解可以不写
ComponentScan:
作用:用于指定通过注解指定spring在创建容器时要扫描的包
属性:value:它和basePackages的作用是一样的,都是用于指定创建容器时要扫描的包。使用此注解,就等同于在xml中配置了
<context:component-scan base-package="com.ycl"></context:component-scan>
Bean:
作用:用于把当前方法的返回值作为bean对象存入spring的ioc容器中
属性:name用于指定bean的id,不写时默认值是当前方法的名称
细节:当我们使用注解配置方法时,如果方法有参数,spring框架会去容器中查找有没有可用的对象,有多个时,查找方式和Autowired注解的一样
Import:
作用:用于导入其他的配置类
属性:value用于指定其他配置类的字节码,当我们使用import注解后,该类就为父配置类,导入的为子配置类
PropertySource:
作用:用于指定properties文件的位置
属性:value指定文件的名称和路径,加classpath注明是类路径下
Qualifier:当相同bean类型有多个时,用于指定使用哪个
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import javax.sql.DataSource;
/**
* 这是一个配置类,作用与bean.xml相同
*/
@Configuration
@ComponentScan("com.ycl")
@PropertySource("classpath:jdbcConfig.properties")
public class SpringConfiguration {
@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 = "runner")
@Scope("prototype")
public QueryRunner queryRunner(@Qualifier(value = "dataSource") DataSource dataSource){
return new QueryRunner(dataSource);
}
@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();
}
}
}
jdbcConfig.properties文件:
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/db3
jdbc.username = root
jdbc.password = root
二、Spring 整合junit的配置
1.导入spring整合junit的jar坐标
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
2.使用Junit提供的一个注解把原有的main方法替换了,替换成spring提供的@RunWith
@RunWith(SpringJUnit4ClassRunner.class)
3.告知spring的运行器,spring和ioc创建是基于xml还是注解的,并说明位置
@ContextConfiguration
locations:指定xml文件的位置,加上classpath关键字,表示在类路径下
locations = “classpath:bean.xml”
classes:指定注解类所在位置
当使用spring 5.x版本的时候,要求junit的jar必须是4.12及以上
import com.ycl.domain.Account;
import com.ycl.service.IAccountService;
import config.SpringConfiguration;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
/**
* 使用junit单元测试我们的配置
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
public class AccountServiceTest {
@Autowired
IAccountService as = null;
@Test
public void testFindAll(){
List<Account> accounts = as.findAllAccount();
for (Account account : accounts) {
System.out.println(account);
}
}
}