Spring笔记七 - 纯注解编程(完结)
1. 纯注解版AOP编程
1.1 搭建环境
1. 应用配置Bean
2. 注解扫描
1.2 开发步骤
1. 原始对象
@Service(@Component)
public class UserServiceImpl implements UserService {
}
2. 创建切面类 (额外功能 切入点 组装切面)
@Aspect
@Component
public class MyAspect {
@Arround("execution(* login(..))")
public Object arround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("--- aspect log ---");
Object ret = joinPoint.proceed();
return ret;
}
}
3. Spring的配置文件中
<aop:aspectj-autoproxy/>
@EnableAspectjAutoProxy: 在配置Bean添加注解
1.3 注解AOP细节分析
1. 代理创建方式的切换 JDK Cglib
<aop:aspectj-autoproxy proxy-target-class=true|false/>
ture: Cglib (default)false: JDK
@EnableAspectjAutoProxy(ProxyTargetClass = true|false)
2. springboot AOP的开发方式
@EnableAspectjAutoProxy (已设置好)
1. 原始对象
@Service(@Component)
public class UserServiceImpl implements UserService {}
2. 创建切面类 (额外功能 切入点 组装切面)
@Aspect
@Component
public class MyAspect {
@Arround("execution(* login(..))")
public Object arround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("--- aspect log ---");
Object ret = joinPoint.proceed();
return ret;
}
}
*注意:
Spring AOP 代理默认实现 JDK
SpringBoot AOP 代理默认实现 Cglib
2. 纯注解版 Spring + MyBatis整合
2.1 基础配置 (配置Bean)
三部分:
1. 连接池
<!--连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/xxx?useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="xxx"/>
</bean>
@Bean
@public DruidDataSource dataSource() {
DruidDataSource dataSource = new DruidDataSouce();
dataSource.setDriverClassName("");
dataSource.serUrl();
...
return dataSource;
}
2. SqlSessionFactoryBean
<!--创建SqlSessionFactory SqlSessionFactoryBean-->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--entity包存放实体类-->
<property name="typeAliasesPackage" value="包的路径名"/>
<property name="mapperLocations">
<list>
<value>classpath:xxx.xxx.mapper/*Mapper.xml</value>
</list&g