week 5.25-5.31
- | Study-update |
---|---|
-Mon | Spring的面向切面编程,通知 |
-Tue | 环绕通知 |
-Wes | spring in action的mvc学习 |
-Thue | mock的使用, |
-Fri | Springboot |
-Sat | 使用dbutil和Spring进行crud操作 |
-Sun |
5.25 Monday
什么是面向切面编程
切面能帮助我们模块化横切关注点。横切关注点可以被描述为影响应用多处的功能。例如安全。
定义AOP术语
通知(Advice)
切面的工作被称为通知。 通知定义了切面是什么以及何时使用。
Spring可以应用五种类型的通知:
- 前置通知(Before)
- 后置通知(After)
- 返回通知(After-returning)
- 异常通知(After-throwing)
- 环绕通知(Around)
连接点(Join point)
通知的时机,称为连接点。
切点(Poincut)
切点的定义会匹配通知所要织入的一个或多个连接点。
切面(Aspect)
//TODO
引入(Introduction)
//TODO
织入(Introduction)
//TODO
通知小练习
1.创建一个接口
2.创建通知类.@Aspect. 写好通知类的内容
3.在config文件 加@EnableAspectJAutoProxy启动AspectJ自动代理,并配置通知类的bean.
如果是xml配置的话,使用<aop:aspectj-autoproxy/>
编写切点
定义一个Performance
/**
* @author lzr
* @date 2020/5/25 11:26:55
* @description 表演接口
*/
public interface Performance {
public void perform();
}
定义切面
/**
* @author lzr
* @date 2020/5/25 11:27:55
* @description
*/
@Aspect
public class Audience {
//通过@Pointcut注解声明频繁使用的切点表达式
@Pointcut("execution(* com.maaoooo.aop.Performance.perform(..))")
public void performance(){}
//通知方法会在目标方法前调用
@Before("performance()")
public void silenceCellphones(){
System.out.println("Silencing");
}
//通知方法会在目标方法前调用
@Before("performance()")
public void takeSeats(){
System.out.println("takeSeats");
}
//通知方法会在目标方法返回后调用
@AfterReturning("performance()")
public void applause(){
System.out.println("CLAP CLAP! ");
}
//通知方法会在目标方法抛出异常后调用
@AfterThrowing("performance()")
public void demandRefund(){
System.out.println("Demanding a refund");
}
}
config
用@EnableAspectJAutoProxy启动AspectJ自动代理
@Configuration
@EnableAspectJAutoProxy
public class Config {
@Bean
public Audience audience(){
return new Audience();
}
@Bean
public Show show(){
return new Show();
}
}
5.26 Tuesday
/**
* @author lzr
* @date 2020/5/26 10:14:48
* @description
*/
@Aspect
@Component
public class Audience {
@Pointcut("execution(* com.maaoooo.aop.Performance.perform(..))")
public void performance(){}
@Around("performance()")
public void watchPerformance(ProceedingJoinPoint jp){
try {
//前
System.out.println("Silence Cell Phones");
System.out.println("Sit");
jp.proceed();
//无异常
System.out.println("Nice show.");
} catch (Throwable throwable) {
//异常
System.out.println("fuck!");
}
}
}
5.29 Friday
Springboot
快速开发的脚手架。
基于Springboot可以快速开发单个微服务。
约定大于配置。
SpringClound
基于Springboot实现。
5.29 Saturday
POJO
/**
* @author lzr
* @date 2020 05 29 21:29
* @description 账户实体类
*/
public class Account implements Serializable {
private int id;
private String name;
private float money;
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getMoney() {
return money;
}
public void setMoney(float money) {
this.money = money;
}
}
ApplicationContext
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置外部配置-->
<context:property-placeholder location="db.properties"/>
<bean class="com.maaoooo.domain.Account" id="account"/>
<!-- 配置service-->
<bean class="com.maaoooo.service.AccountServiceImpl" id="service">
<property name="accountDao" ref="accountDaoImpl"/>
</bean>
<!-- 配置dao-->
<bean class="com.maaoooo.dao.IAccountDaoImpl" id="accountDaoImpl">
<property name="runner" ref="queryRunner"/>
</bean>
<!-- 配置QueryRunner-->
<bean class="org.apache.commons.dbutils.QueryRunner" id="queryRunner" scope="prototype">
<!-- 注入数据源-->
<constructor-arg name="ds" ref="dataSource"/>
</bean>
<!-- 配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 连接数据库的信息-->
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
</beans>
AccountServiceImpl
/**
* @author lzr
* @date 2020 05 29 21:40
* @description
*/
public class AccountServiceImpl implements IAccountService{
private IAccountDao accountDao;
@Autowired
public void setAccountDao(IAccountDao accountDao) {
this.accountDao = accountDao;
}
public List<Account> findAllAccount() {
return accountDao.findAllAccount();
}
public Account findAccountById(int id) {
return accountDao.findAccountById(id);
}
public void saveAccount(Account account) {
accountDao.saveAccount(account);
}
public void update(Account account) {
accountDao.update(account);
}
@Override
public String toString() {
return "AccountServiceImpl{" +
"accountDao=" + accountDao +
'}';
}
public void deleteAccount(int id) {
accountDao.deleteAccount(id);
}
}
DaoImpl
/**
* @author lzr
* @date 2020 05 29 21:47
* @description
*/
public class IAccountDaoImpl implements IAccountDao {
private QueryRunner runner;
public void setRunner(QueryRunner runner) {
this.runner = runner;
}
@Override
public String toString() {
return "IAccountDaoImpl{" +
"runner=" + runner +
'}';
}
public List<Account> findAllAccount() {
try {
return runner.query("select * from account",new BeanListHandler<Account>(Account.class));
}catch (Exception e){
throw new RuntimeException(e);
}
}
public Account findAccountById(int id) {
try {
System.out.println("check");
return runner.query("select * from account where id=?",new BeanHandler<Account>(Account.class),id);
}catch (Exception e){
throw new RuntimeException(e);
}
}
public void saveAccount(Account account) {
try {
runner.update("insert into account(name,money)values(?,?)",account.getName(),account.getMoney());
}catch (Exception e){
throw new RuntimeException(e);
}
}
public void update(Account account) {
try {
runner.update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());
}catch (Exception e){
throw new RuntimeException(e);
}
}
public void deleteAccount(int id) {
try {
runner.update("delete from account where id=?",id);
}catch (Exception e){
throw new RuntimeException(e);
}
}
}
Test
/**
* @author lzr
* @date 2020 05 29 22:59
* @description
*/
public class Test {
@org.junit.Test
public void testFindAll() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
IAccountService accountService = context.getBean(AccountServiceImpl.class);
List<Account> accounts = accountService.findAllAccount();
for (Account account : accounts) {
System.out.println(account);
}
}
@org.junit.Test
public void testFindAccountById() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
IAccountService accountService = context.getBean("service",AccountServiceImpl.class);
Account account = accountService.findAccountById(1);
System.out.println(account.toString());
}
@org.junit.Test
public void testSave(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
IAccountService accountService = context.getBean("service",AccountServiceImpl.class);
Account account=new Account();
account.setMoney(99999);
account.setName("TestSave");
accountService.saveAccount(account);
}
@org.junit.Test
public void update(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
IAccountService accountService = context.getBean("service",AccountServiceImpl.class);
Account account=new Account();
account.setId(4);
account.setMoney(1);
account.setName("TestUpdate");
accountService.update(account);
}
@org.junit.Test
public void delete(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
IAccountService accountService = context.getBean("service",AccountServiceImpl.class);
accountService.deleteAccount(4);
}
}