一、常用注解

Spring框架重要知识点总结(二)_配置文件

代码详解

// 默认当前类名就是ID名称,首字母小写
@Component(value = "c")
// @Controller
// @Service(value = "c")
// @Repository(value = "c")
// @Scope(value = "singleton") // 默认值,单例的
// @Scope(value = "prototype") // 多例的
public class Car {
// 注解注入值,属性set方法是可以省略不写的。
// 只有一个属性,属性的名称是value,value是可以省略不写的
@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("操作...");
}

二、纯注解

纯注解的方式是微服务架构开发的主要方式,所以也是非常的重要。纯注解的目的是替换掉所有的配置文件。但是需要编写配置类。

(一)编写实体类

@Component
public class Order {
@Value("北京")
private String address;
@Override
public String toString() {
return "Order{" +
"address='" + address + '\'' +
'}';
}}

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

// 声明当前类是配置类
@Configuration
// 扫描指定的包结构
@ComponentScan(value = "cn.tx.demo4")
public class SpringConfig {

}

(三)测试方法

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的配置文件可以分成多个配置的,编写多个配置类。用于导入其他配置类​

​@Bean注解 只能写在方法上,表明使用此方法创建一个对象,对象创建完成保存到IOC容器中​

// 声明当前类是配置类
@Configuration
// 扫描指定的包结构
@ComponentScan(value = "cn.tx.demo4")
// @ComponentScan(value = {"cn.tx.demo4","cn.tx.demo3"})
// 引入新的配置类
@Import(value = {SpringConfig2.class})
public class SpringConfig {
/ * 创建连接池对象,返回对象,把该方法创建后的对象存入到连接池中,使用@Bean注解解决
<!--配置连接池对象-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql:///spring_db" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
*
* @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单元测试,则需要jar包,在导入spring-test的坐标依赖。

(二)配置文件+注解方式

​①编写类和方法,把该类交给IOC容器进行管理​

public class User {
public void sayHello(){
System.out.println("Hello....");
}}

​②编写配置文件applicationContext.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">

<!--整合单元测试-->
<bean id="user" class="cn.tx.demo5.User"/>
</beans>

​③编写测试代码​

@RunWith(value = SpringJUnit4ClassRunner.class)     // 运行单元测试
@ContextConfiguration(value = "classpath:applicationContext.xml") // 加载类路径下的配置文件
public class Demo5 {
// 测试哪一个对象,把该对象注入进来,在测试环境下,可以使用注解的方式注入测试的对象
// 按类型自动注入
@Autowired
private User user;

@Test
public void run1(){
// 创建工厂,加载配置文件......
// 调用对象的方法
user.sayHello();
}
}

(三)纯注解方式

①在(二)的基础上,将配置文件换成配置类

②在测试类中,加载类路径下的配置文件,改为记载配置类@ContextConfiguration(classes=配置类名.class)

欢迎各位大佬,评论指出不足。