java的Test 如何使用@Autowired注解
1、配置来至bean.xml
@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
/**
* 使用Junit单元测试,测试我们的配置
Spring整合junit的配置
1.导入spring整合junit的jar(坐标)
2.使用Junit提供的一个注解把原有的mian方法替换了,替换成spring提供的@Runwith
3.告知spring的运行期,spring的ioc创建是基于xml还是注解的,并且我说明位置
@ContextConfiguration
localtions:指定xml文件的位置,加上classpath关键字,表示在类路径下
class:注定注解类所在的位置
当我们使用spring 5.x版本的时候,要求junit的jar必须是4.12及以上
*/
@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);
}
}
SpringConfiguration类
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 { }
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
java的Test 如何使用@Autowired注解的更多相关文章
- Spring5:@Autowired注解、@Resource注解和@Service注解
什么是注解 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点: 1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分 ...
- @Autowired注解的使用
使用Spring时,通过Spring注入的Bean一般都被定义成private,并且要有getter和setter方法,显得比较繁琐,增加了代码量,而且有时会搞忘造成错误. 可以使用@Autowire ...
- Spring中@Autowired注解与自动装配
1 使用配置文件的方法来完成自动装配我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. ...
- Spring学习(9)--- @Autowired注解(二)
可以使用@Autowired注解那些众所周知的解析依赖性接口,比如:BeanFactory,ApplicationContext,Environment,ResourceLoader,Applicat ...
- (转)java之Spring(IOC)注解装配Bean详解
java之Spring(IOC)注解装配Bean详解 在这里我们要详细说明一下利用Annotation-注解来装配Bean. 因为如果你学会了注解,你就再也不愿意去手动配置xml文件了,下面就看看 ...
- Spring @Autowired注解用在集合上面,可以保持接口的所有实现类
CourseService课程接口有2个子类,HistroyCourseServiceImpl和MathsCourseServiceImpl public interface CourseServic ...
- 04 Spring的@Autowired注解、@Resource注解、@Service注解
什么是注解 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事务,这么做有两个缺点: 1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分 ...
- Spring中Autowired注解,Resource注解和xml default-autowire工作方式异同
前面说到了关于在xml中有提供default-autowire的配置信息,从spring 2.5开始,spring又提供了一个Autowired以及javaEE中标准的Resource注释,都好像可以 ...
- @Resource与@Autowired注解的区别
一.写本博文的原因 年初刚加入到现在的项目时,在使用注解时我用的@Resource.后来,同事:你怎么使用@Resource注解?我:使用它有错吗?同事:没错,但是现在都使用@Autowired.我: ...
随机推荐
- ChCore Lab4 多核处理 实验笔记
本文为上海交大 ipads 研究所陈海波老师等人所著的<现代操作系统:原理与实现>的课程实验(LAB)的学习笔记的第四篇:多核处理.所有章节的笔记可在此处查看:chcore | 康宇PL' ...
- [LBS学习笔记4]地理特征POI、AOI、路径轨迹
1 简述 今天继续LBS地理信息的学习,目标是写到10篇博客的时候,做出一个地图工具页面用,包含地图空间索引Geohash.S2.H3的可视化展示. 地理特征分为点(POI).线(路径).面(AOI) ...
- 深度好文:Linux文件系统剖析
一个执着于技术的公众号 Linux 文件系统体系结构是一个对复杂系统进行抽象化的有趣例子.通过使用一组通用的 API 函数,Linux 可以在许多种存储设备上支持许多种文件系统.例如,read 函数调 ...
- Docker的三种网络代理配置
开源Linux 长按二维码加关注~ 上一篇:IPv6技术白皮书(附PDF下载) 有时因为网络原因,比如公司NAT,或其它啥的,需要使用代理.Docker的代理配置,略显复杂,因为有三种场景.但基本原理 ...
- 通过Go实现AES加密和解密工具
本文包含如下两个内容: AES加密介绍及实现原理 Go实现AES加密和解密工具 AES加密介绍及实现原理 AES( advanced encryption standard)使用相同密钥进行加密和解密 ...
- (数据科学学习手札136)Python中基于joblib实现极简并行计算加速
本文示例代码及文件已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 我们在日常使用Python进行各种数据计算 ...
- Vue组件之间通信
vue组件传值有以下几种情况: 父组件向子组件传值.子组件向父组件传值.兄弟组件之间传值等 一.父组件向子组件传值: 传值方式: props <father> // 动态传递值 <s ...
- Spring Boot+微信小程序_保存微信登录者的个人信息
1. 前言 微信小程序开发平台,提供有一类 API,可以让开发者获取到微信登录用户的个人数据.这类 API 统称为开放接口. Tip:微信小程序开发平台,会把微信登录用户的个人信息分为明文数据和敏感数 ...
- 2021夏季学期华清大学EE数算OJ1:算数问题
第一次写博客,有点紧张... 也许格式也没有特别丑吧 先看原题( 此题做法众多,这里仅仅介绍蒟蒻的一种很复杂的思路(但最后还是喜提0ms的好成绩) 读完这道题,不难发现,此题不过是一个质因数分解+一堆 ...
- 590. N-ary Tree Postorder Traversal - LeetCode
Question 590. N-ary Tree Postorder Traversal Solution 题目大意:后序遍历一个树 思路: 1)递归 2)迭代 Java实现(递归): public ...
