java的Test 如何使用@Autowired注解
1、配置来至 bean.xml
1 2 3 4 5 6 7 8 9 10 11 12 | @RunWith (SpringJUnit4ClassRunner. class ) @ContextConfiguration (locations = "classpath:bean.xml" ) //表示在编译完成后在类路径下的beean.xml文件 public class AccountServiceTest { @Autowired private IAccountService as; @Test public void testFindAll() { } } |
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="com.ruankao.service.Impl.AccountServiceImpl"> <!--注入 dao--> <property name="accountDao" ref="accountDao"></property> </bean> <!--配置 dao 对象--> <bean id="accountDao" class="com.ruankao.dao.Impl.AccountDaoImpl"> <!--注入 QueryRunner--> <property name="runner" ref="runner"></property> </bean> <bean id="runner" class="org.apache.commons.dbutils.QueryRunner"> <!--配置数据源--> <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/ruankaowang?characterEncoding=UTF-8"></property> <property name="user" value="root"></property> <property name="password" value="123456"></property> </bean> </beans>
pom.xml 的配置
<packaging>jar</packaging> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.2.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>5.1.6</version> </dependency> <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> <scope>test</scope> </dependency>
目录的展示
2. 配置来至 Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /**<br> * 使用Junit单元测试,测试我们的配置<br> Spring整合junit的配置<br> 1.导入spring整合junit的jar(坐标)<br> 2.使用Junit提供的一个注解把原有的mian方法替换了,替换成spring提供的@Runwith<br> 3.告知spring的运行期,spring的ioc创建是基于xml还是注解的,并且我说明位置<br> @ContextConfiguration<br> localtions:指定xml文件的位置,加上classpath关键字,表示在类路径下<br> class:注定注解类所在的位置<br><br> 当我们使用spring 5.x版本的时候,要求junit的jar必须是4.12及以上<br>*/ @RunWith (SpringJUnit4ClassRunner. class ) @ContextConfiguration (classes=SpringConfiguration. class ) public class AccountServiceTest { @Autowired private IAccountService as; @Test public void testFindAll() { //init(); //3.执行方法 List<Account> accounts = as.findAllAccount(); for (Account account : accounts){ System.out.println(account); } } |
1 | SpringConfiguration类 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | Configration 作用:指定当前类是一个配置类 细节:当配置类作为AnnotationConfigApplicationContext对象创建的参数时,该注解可用不写 ComponentScan 作用:用于通过注解指定spring在创建容器时要扫描的包 属性: value:它和basePackages的作用是一样的,都是用于指定创建容器时要扫描的包我们使用次注解就等用于在xml中配置了: <context:component-scan base- package = "com.ruankao" ></context:component-scan> Bean 作用:用于把当前方法的返回值作为bean对象存入spring的ioc容器中 属性: name:用于指定bean的id。当不写时,默认值是当前放的名称 细节: 当我们使用注解配置方法时,如果方法有参数,spring框架会去容器中查找有没有可用的bean对象。 查找的方式和Autowired注解的作用是一样的。 Import 作用:用于导入其它的配置类 属性: value:用于指定其他配置类的字节码 当我们使用Import的注解之后,有Import注解的类就是父配置类而,导入的都是子配置类 PropertySource 作用:用于指定properties文件的位置 属性: value:指定文件的名称和路径 关键字:classpath,表示类路径下 */ @ComponentScan (basePackages = "com.ruankao" ) @Import (JdbcConfig. class ) @PropertySource ( "classpath:jdbcConfig.properties" ) public class SpringConfiguration { } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | 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 = "runner" ) public QueryRunner createQueryRunner( @Qualifier ( "dataSource2" ) DataSource dataSource){ return new QueryRunner(dataSource); } /** * 创建数据源对象 */ @Bean (name= "dataSource" ) public ComboPooledDataSource createDataSource() { try { ComboPooledDataSource ds= new ComboPooledDataSource(); ds.setDriverClass(driver); ds.setJdbcUrl(url); ds.setUser(username); ds.setPassword(password); return ds; } catch (PropertyVetoException e) { //e.printStackTrace(); throw new RuntimeException(e); } } /** * 创建数据源对象 */ @Bean (name= "dataSource2" ) public ComboPooledDataSource createDataSource2() { try { ComboPooledDataSource ds= new ComboPooledDataSource(); ds.setDriverClass(driver); ds.setJdbcUrl(url); ds.setUser(username); ds.setPassword(password); return ds; } catch (PropertyVetoException e) { //e.printStackTrace(); throw new RuntimeException(e); } } } |
jdbcConfig.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ruankaowang?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456