IOC注解以及spring整合JUnit单元测试

IOC 注解的方式依赖没有变化
编写接口和实现类
package com.qcbyjy.demo2;
public interface UserService {
public void hello();
}
package com.qcbyjy.demo2;
import org.springframework.stereotype.Component;
/**
* 
*/
// 组件,作用:把当前类使用 IOC 容器进行管理,如果没有指定名称,默认使用类名,
首字母是小写。userServiceImpl。或者自己指定名称
@Component(value = "us")
public class UserServiceImpl implements UserService {
@Override
public void hello() {
System.out.println("Hello IOC 注解...");
}
}
在需要管理的类上添加@Component 注解
@Component(value = "us")
public class UserServiceImpl implements UserService {
@Override
public void hello() {
System.out.println("Hello IOC 注解...");
}
}
编写配置文件,重点是开启注解扫描





编写测试方法

package com.qcbyjy.test;
import com.qcbyjy.demo2.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class Demo2 {
/**
* IOC 注解方式的入门
*/
@Test
public void run1(){
// 工厂
ApplicationContext ac = new
ClassPathXmlApplicationContext("applicationContext_anno.xml");
// 获取对象
UserService userService = (UserService) ac.getBean("us");
userService.hello();
}
}
常用的注解
bean 管理类常用的 4 个注解(作用相同,推荐使用在不同分层上)
@Component 普通的类
@Controller 表现层@Service 业务层
@Repository 持久层
依赖注入常用的注解
@Value 用于注入普通类型(Stringintdouble 等类型)
@Autowired 默认按类型进行自动装配(引用类型)
@Qualifier @Autowired 一起使用,强制使用名称注入
@Resource Java 提供的注解,也被支持。使用 name 属性,按名称注入
对象生命周期(作用范围)注解
@Scope 生命周期注解,取值 singleton(默认值,单实例)和 prototype(多
例)
初始化方法和销毁方法注解(了解)
@PostConstruct 相当于 init-method
@PreDestroy 相当于 destroy-method
package com.qcbyjy.demo3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
// 默认当前类名就是 ID 名称,首字母小写
@Component(value = "c")
// @Controller
// @Service(value = "c")
// @Repository(value = "c")
// @Scope(value = "singleton")
// 默认值,单例的// @Scope(value = "prototype")
// 多例的
public class Car {
// 注解注入值,属性 set 方法是可以省略不写的。
// 只有一个属性,属性的名称是 valuevalue 是可以省略不写的
@Value("大奔 2")
private String cname;
@Value(value = "400000")
private Double money;
// 也不用提供 set 方法
// 按类型自动装配的注解,和 id 名称没有关系
@Autowired
// id 的名称注入,Qualifier 不能单独使用,需要 Autowired 一起使用。
// @Qualifier(value = "person")
// @Resource Java 提供的注解,按名称注入对象,属性名称是 name
// @Resource(name = "person")
private Person person;
/**
* Car 对象创建完成后,调用 init 方法进行初始化操作
*/
@PostConstruct
public void init(){
System.out.println("操作...");
}
/*
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}*/
@Override
public String toString() {
return "Car{" +
"cname='" + cname + '\'' +
", money=" + money +
", person=" + person +
'}';
}
}
package com.qcbyjy.demo3;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component(value = "person")
public class Person {
@Value("张三")
private String pname;
@Override
public String toString() {
return "Person{" +
"pname='" + pname + '\'' +
'}';
}
}
package com.qcbyjy.test;
import com.qcbyjy.demo2.UserService;
import com.qcbyjy.demo3.Car;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;public class Demo3 {
@Test
public void run1(){
// 工厂
ApplicationContext ac = new
ClassPathXmlApplicationContext("applicationContext_anno.xml");
// 获取对象
Car car = (Car) ac.getBean("c");
System.out.println(car);
}
}

IOC 纯注解的方式

编写实体类
package com.qcbyjy.demo4;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Order {
@Value("北京")
private String address;
@Override
public String toString() {
return "Order{" +
"address='" + address + '\'' +
'}';
}
}

编写配置类,替换掉 applicationContext.xml 配置文件

package com.qcbyjy.demo4;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* Spring 的配置类,替换掉 applicationContext.xml
*
*/
// 声明当前类是配置类
@Configuration
// 扫描指定的包结构
@ComponentScan(value = "com.qcbyjy.demo4")
public class SpringConfig {
}
测试方法的编写

package com.qcbyjy.test;
import com.qcbyjy.demo4.Order;
import com.qcbyjy.demo4.SpringConfig;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.annotation.AnnotationConfigApplicationCo
ntext;
public class Demo4 {
/**
* 编写程序,需要加载配置类
*/
@Test
public void run1(){
// 创建工厂,加载配置类
ApplicationContext ac = new
AnnotationConfigApplicationContext(SpringConfig.class);
// 获取到对象
Order order = (Order) ac.getBean("order");
System.out.println(order);
}
}
常用的注解总结
@Configuration 声明是配置类
@ComponentScan 扫描具体包结构的
@Import 注解 Spring 的配置文件可以分成多个配置的,编写多个配置类。用于
导入其他配置类

package com.qcbyjy.demo4;
import org.springframework.context.annotation.Configuration;
/**
* 新的配置类
*
*/
@Configuration
// 声明配置类
public class SpringConfig2 {
}
// 声明当前类是配置类
@Configuration
// 扫描指定的包结构
@ComponentScan(value = "com.qcbyjy.demo4")
// @ComponentScan(value = {"com.qcbyjy.demo4","com.qcbyjy.demo3"})
// 引入新的配置类
@Import(value = {SpringConfig2.class})
public class SpringConfig {
@Bean 注解 只能写在方法上,表明使用此方法创建一个对象,对象创建完成保
存到 IOC 容器中
package com.qcbyjy.demo4;import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import javax.sql.DataSource;
/**
*
* Spring 的配置类,替换掉 applicationContext.xml
*
*/
// 声明当前类是配置类
@Configuration
// 扫描指定的包结构
@ComponentScan(value = "com.qcbyjy.demo4")
// @ComponentScan(value = {"com.qcbyjy.demo4","com.qcbyjy.demo3"})
// 引入新的配置类
@Import(value = {SpringConfig2.class})
public class SpringConfig {
/**
* 创建连接池对象,返回对象,把该方法创建后的对象存入到连接池中,使用
@Bean 注解解决
class="com.alibaba.druid.pool.DruidDataSource">
value="com.mysql.jdbc.Driver" />
*
* @return
*/
@Bean(name="dataSource")
public DataSource createDataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql:///spring_db");dataSource.setUsername("root");
dataSource.setPassword("root");
return dataSource;
}
}

Spring 框架整合 JUnit 单元测

每次进行单元测试的时候,都需要编写创建工厂,加载配置文件等代码,比较繁
琐。Spring 提供了整合 Junit 单元测试的技术,可以简化测试开发。下面开始学
习该技术。
必须先有 Junit 单元测试的环境,也就是说已经导入 Junit 单元测试的 jar 包。咱
们已经导入过了。使用的是 4.12 版本
再导入 spring-test 的坐标依赖
org.springframework
spring-test
5.0.2.RELEASE
test

编写类和方法,把该类交给 IOC 容器进行管理
package com.qcbyjy.demo5;
public class User {
public void sayHello(){
System.out.println("Hello....");
}
}
编写配置文件 applicationContext_test.xm




编写测试代码
package com.qcbyjy.test;
import com.qcbyjy.demo5.User;
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;
/**
* Spring 整合 Junit 单元测试
*
*/
@RunWith(value = SpringJUnit4ClassRunner.class) // 运行单元测试
@ContextConfiguration(value =
"classpath:applicationContext_test.xml") // 加载类路径下的配置文件
public class Demo5 {
// 测试哪一个对象,把该对象注入进来,在测试环境下,可以使用注解的方式注
入测试的对象
// 按类型自动注入
@Autowired
private User user;
@Test
public void run1(){
// 创建工厂,加载配置文件......
// 调用对象的方法
user.sayHello();
}
}

Spring 整合单元测试(纯注解方式)

编写类和方法
package com.qcbyjy.demo6;
import org.springframework.stereotype.Component;
@Component
public class Customer {
public void save(){
System.out.println("保存客户...");
}
}
编写配置类

 

package com.qcbyjy.demo6;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* Spring 整合 Junit 配置类
*/
// 声明
@Configuration
// 扫描包结构
@ComponentScan(value = "com.qcbyjy.demo6")
public class SpringConfig6 {
}
编写测试方法

package com.qcbyjy.test;
import com.qcbyjy.demo6.Customer;
import com.qcbyjy.demo6.SpringConfig6;
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;
/**
* Spring 整合 Junit 注解的方式测试
*/
@RunWith(SpringJUnit4ClassRunner.class)
// 加载配置类
@ContextConfiguration(classes = SpringConfig6.class)
public class Demo6 {
// 按类型注入
@Autowired
private Customer customer;
/**
* 测试
*/
@Test
public void run1(){
customer.save();
}
}

你可能感兴趣的:(IOC注解以及spring整合JUnit单元测试)