@Configuration
- 作用:指定当前类是一个配置类
- 细节:如果该类的字节码文件当成AnnotationConfigApplicationContext类的参数时,不需要加这个注解,如果不是就必须加上注解,或者同样当成参数传给该类也可以实现。
package config;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SpringConfiguration {
}
@ComponentScan
- 作用:用于通过注解指定Spring在创建容器时要扫描的包
- 属性:value它和basePackages的作用是一样的,都是用于指定创建容器时要扫描的包,我们使用了此注解,就相当于在xml中配置了<context:component-scan base-package=“com.spring”></context:component-scan>,basePackages写不写都可以,如果是数组类型需要写在{}中。
@Configuration
//@ComponentScan(basePackages = "com.spring")
@ComponentScan({"com.spring"})
public class SpringConfiguration {
}
@Bean
- 作用:用于把当前方法的返回值作为bean对象存入spring的ioc容器中
- 属性:name用于指定bean的id,当不写时,默认值是当前方法的名称
- 细节:当我们使用注解配置方法时,如果方法有参数,spring框架会去容器中查找有没有可用的bean对象。查找的方法和Autowired注解的作用是一样的
package config;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
//@ComponentScan(basePackages = "com.spring")
@ComponentScan("com.spring")
public class SpringConfiguration {
@Bean(name = "runner")
@Scope("prototype")
public QueryRunner createQueryRunner(DataSource dataSource){
return new QueryRunner(dataSource);
}
@Bean(name = "dataSource")
public DataSource ceateDataSource(){
try{
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setDriverClass("com.mysql.jdbc.Driver");
ds.setJdbcUrl("jdbc:mysql://localhost:3306");
ds.setUser("root");
ds.setPassword("111111");
return ds;
}catch (Exception e){
throw new RuntimeException();
}
}
}
这两个是一样的
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 告知spring在创建容器的时候要扫描的包,配置所需要的标签不是在beans的约束中,而是一个名称为context名称空间和约束中。-->
<context:component-scan base-package="com.spring"></context:component-scan>
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
<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"></property>
<property name="user" value="root"></property>
<property name="password" value="111111"></property>
</bean>
</beans>
AnnotationConfigApplicationContext类
这个类和读取bean.xml文件是一样的,一个是读取文件,另一个是读取配置类。其余的方法还是一样的。
package com.spring.ul;
import com.spring.config.SpringConfiguration;
import com.spring.service.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 表现层
*/
public class Client {
public static void main(String[] args) {
//获取核心容器对象
//ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
IAccountService accountService = ac. getBean("accountService",IAccountService.class);
IAccountService accountService1 = ac. getBean("accountService",IAccountService.class);
System.out.println(accountService == accountService1);
}
}
@Import
- 作用:用于导入其他的配置类
- 属性:value:用于指定其他配置类的字节码。当我们使用Import的注解之后,有Import注解的父类,
package config;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
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;
@Configuration
//@ComponentScan(basePackages = "com.spring")
@ComponentScan("com.spring")
@Import(jdbConfig.class)
public class SpringConfiguration {
@Bean(name = "runner")
public QueryRunner createQueryRunner(DataSource dataSource){
return new QueryRunner(dataSource);
}
@Bean(name="dataSource")
public DataSource ceateDataSource(){
try{
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setDriverClass("com.mysql.jdbc.Driver");
ds.setJdbcUrl("jdbc:mysql://localhost:3306");
ds.setUser("root");
ds.setPassword("111111");
return ds;
}catch (Exception e){
throw new RuntimeException();
}
}
}
@PropertySource
- 作用:用于指定properties文件的位置
- 属性:value指定文件的名称和路径
- 关键字:classpath表示类路径下
package config;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.context.annotation.*;
import javax.sql.DataSource;
@Configuration
//@ComponentScan(basePackages = "com.spring")
@ComponentScan("com.spring")
@Import(jdbConfig.class)
@PropertySource("classpath:jdbcConfig.properties")
public class SpringConfiguration {
@Bean(name = "runner")
public QueryRunner createQueryRunner(DataSource dataSource){
return new QueryRunner(dataSource);
}
@Bean(name="dataSource")
public DataSource ceateDataSource(){
try{
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setDriverClass("com.mysql.jdbc.Driver");
ds.setJdbcUrl("jdbc:mysql://localhost:3306");
ds.setUser("root");
ds.setPassword("111111");
return ds;
}catch (Exception e){
throw new RuntimeException();
}
}
}
spring整合junit
- 应用程序入口
main方法 - junit单元测试中,没有方法也能执行
junit集成了一个main方法
该方法就会判断当前的测试类中哪些方法有@Test注解
junit就会让有Test注解的方法执行 - junit不会管我们是否采用spring框架
在测试方法的时候,junit根本不知道我们是不是使用了spring框架
所以也就不会为我们读取配置文件/配置类创建spring核心容器 - 由以上三点可知
当测试类方法执行的时候,没有ioc容器,就算写了Autowired注解,也无法实现注入
解决办法
- 导入spring整合junit的jar(坐标)
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.7.RELEASE</version>
</dependency>
- 使用junit提供的一个注解把原有的main方法替换了,替换成spring提供的 @RunWith
@RunWith(SpringJUnit4ClassRunner.class)
- 告知spring的运行器,spring和ioc创建是基于xml还是注解,并且说明位置
@ContextConfiguration
locations:指定xml文件的位置,加上classpath关键字,表示在类路径下
classes:指定在注解类所在位置
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
- 细节:当我们使用spring 5.x版本的时候,要求junit的jar必须是4.12以及以上的版本