注解驱动
使用注解的方式开发
- 注解启动时使用注解的形式替代xml配置,将繁杂的spring配置文件从工程中彻底消除掉,简化书写。
- 注解的弊端:
- 为了达成注解驱动的目的,可能会将原先很简单的书写,变的更加复杂
- XML中配置第三方开发的资源是很方便的,但使用注解驱动无法在第三方开发的资源中进行编辑,因此会增大开发工作量
常见的注解
注解使用前提
-
在配置文件中开启注解解析,在加载类中配置的注解项
// 注解扫包 <context:component-scan base-package="packageName"/>
- 在启动的时候会对包进行扫描。
- 扫描文件是以递归的形式进行
- 扫描文件仅扫描合法的java文件
- 扫描时仅扫描spring可识别的注解
- 扫描结束以后把有效的注解转发为对应的资源加载到IOC容器中
- 就加载效率而言注解方式快于xml
常见注解使用
-
bean标签的注解方式
-
注解名:@Component @Controller @Service @Repository
-
类型:类注解
-
说明:
◆ @Controller、@Service 、@Repository是@Component的衍生注解,功能同@Component
-
属性:
- value(默认) bean的唯一标识id
-
-
bean的生命周期
- 注解名:@PostConstruct、@PreDestroy
- 类型:方法注解
- 作用:定义bean的生命周期PostConstruct初始化方法注解PreDestroy终结方法注解
-
bean的单例还是多例
- 注解名:@Scope
- 类型:类注解
- 作用:定义获得的类是单例还是多例 prototype多例 singleton单例
第三方资源的注入
加载第三方资源
-
注解名:@Bean
-
类型:方法注解
-
作用:加载第三方的bean资源
-
使用说明:
- 因为注解必须作用在源代码上,但是第三方的源代码无法修改,所以使用@Bean注解方法解决这个问题
- 这个是方法注解,如果要使用必须把这个类注入到IOC容器中,所以类上必须使用注解
注解实现依赖注入
-
注解名:@value(非引用)
-
类型:属性注解,方法注解
-
作用:给属性赋值,如果添加在属性上面,类可以省略set方法
-
注解名:@Autowired、@Qualifier
-
类型:属性注解,方法注解
-
作用:给引用类型的属性或者方法赋值
-
说明:@Autowired默认按类型装配,指定@Qualifier后可以指定自动装配的bean的id
-
相关属性:required:定义该属性是否允许为null
-
注解名:@Primary
-
类型:类注解
-
作用:设置类对应的bean按类型装配时优先装配
-
说明:@Autowired默认按类型装配,当出现相同类型的bean,使用@Primary提高按类型自动装配的优先级,多个@Primary会导致优先级设置无效
注解方式加载Properties配置文件
- 注解名:@PropertySource
- 类型:类注解
- 作用:加载配置文件
- 说明:不支持*的通配符形式,一旦加载所欲的spring控制的bean都可以使用
- 属性:value 加载配置文件的文件名 ignoreResourceNotFound:如果资源未找到,是否忽略,默认为false
纯注解格式
- 创建一个配置类
- 在配置类中使用注解
- 名称:@Configuration、@ComponentScan
- 类型:类注解
- 作用:设置当前类为spring核心配置加载类 @ComponentScan扫包
- 使用:使用AnnotationConfigApplicationContext类创建容器
bean加载控制
-
@DependsOn
-
类型:类注解、方法注解
-
控制bean的加载顺序,使其在指定bean加载完毕后再加载
-
说明:
◆ 配置在方法上,使@DependsOn指定的bean优先于@Bean配置的bean进行加载
◆ 配置在类上,使@DependsOn指定的bean优先于当前类中所有@Bean配置的bean进行加载
◆ 配置在类上,使@DependsOn指定的bean优先于@Component等配置的bean进行加载
⚫ 名称:@Order
⚫ 类型:配置类注解
⚫ 位置:配置类定义的位置(类上)
⚫ 作用:控制配置类的加载顺序⚫ 名称:@Lazy
⚫ 类型:类注解、方法注解
⚫ 位置:bean定义的位置(类上或方法上)
⚫ 作用:控制bean的加载时机,使其延迟加载Spring 整合Mybatis(XML方式)
-
mybatis中使用的连接池技术可以是c3p0也可以是druid连接池
-
引入依赖
-
引入mybatis支持spring整合的依赖
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.2</version> </dependency>
-
引入spring整合jdbc的依赖
在使用连接池的时候需要spring的支持org/springframework/dao/support/DaoSupport
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.9.RELEASE</version> </dependency>
-
引入mybatis框架的依赖
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.0</version> </dependency>
-
-
整合过程:
-
整合连接池,注入连接池资源(druid)
<bean id="druid" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean>
-
整合mybatis,注入核心配置资源
<!-- spring管理sqlsession的bean--> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="druid"></property> <!-- 给包起别名扫包--> <property name="typeAliasesPackage" value="com.wx.entity"> <!-- 加载外部核心配置文件--> <property name="configLocation" value="classpath:MyBatisConfig.xml"</property> </property> </bean>
-
注入映射配置文件的资源
<!--加载mybatis的映射配置文件--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 映射配置文件扫包--> <property name="basePackage" value="com.wx.dao"></property> </bean>
-
service层中的dao层的代理对象的属性注入,注入service层的资源
<!-- service层的实现类交给ioc容器--> <bean class="com.wx.services.imp.QuestionServiceImp" id="questionServiceImp"> <!-- dao层属性set注入--> <property name="questionDao" ref="questionDao"></property> </bean>
-
Spring 整合Mybatis(注解方式)
- maven依赖引入与xml的方式相同
- 整合过程
-
业务类使用注解的方式声明bean,属性采用注解的方式注入
-
加载外部properties文件
@Configuration @ComponentScan("com.wx")// 扫包解析注解 @PropertySource({"classpath:jdbc.properties","classpath:log4j.properties"}) // 加载properties配置文件
-
注入数据库连接池
/** * 数据库连接池的配置类 */ @Component @Order(1) public class DataSourceConfiguration { @Value("${jdbc.driver}") private String driver; @Value("${jdbc.url}") private String url; @Value("${jdbc.username}") private String username; @Value("${jdbc.password}") private String password; @Bean public DataSource getDataSource(){ DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setDriverClassName(driver); druidDataSource.setUrl(url); druidDataSource.setUsername(username); druidDataSource.setPassword(password); return druidDataSource; }
-
注入Mybatis资源
@Bean // @DependsOn("dataSourceConfiguration") public SqlSessionFactoryBean getSqlSessionFactoryBean(@Autowired DataSource dataSource ){ SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); // 赋值 factoryBean.setDataSource(dataSource); // 注入连接池 factoryBean.setTypeAliasesPackage("com.wx.entity"); // 给包起别名 ClassPathResource resource = new ClassPathResource("SqlMapConfig.xml"); // 加载Mybatis核心xml配置文件 factoryBean.setConfigLocation(resource); return factoryBean; } @Bean public MapperScannerConfigurer getMapperScannerConfigurer(){ MapperScannerConfigurer configurer = new MapperScannerConfigurer(); configurer.setBasePackage("com.wx.dao"); // 扫包加载映射配置文件 return configurer; }
-
-
Spring整合junite
-
junite是使用类加载器进行加载
-
整合过程:
-
导入spring对于test的支持
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.1.9.RELEASE</version> </dependency>
-
导入junit的单元测试
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency>
-
在maven工程的test包下面的测试类中注入依赖
// 设置spring加载junit的专用类加载器 @RunWith(SpringJUnit4ClassRunner.class) // 指定Spring入口的位置 @ContextConfiguration(classes = SpringConfiguration.class) public class DeptServiceTest
-
Spring接管junite的运行权,使用Spring专用的junite类加载器
-
将junite测试用例注册到spring的IOC容器中
-
-
使用注意事项
- 在spring5.0以后要求junite的版本必须是4.12及以上的
- Junite仅用于单元测试,不能将Junite的测试类配置成为spring的bean ,否者会将改配置打包进入工程里