24.12.31 SpringBootDay02

复制代码
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface SaveLog {
}

@Component
@Aspect
public class LogAspect {

    @Resource
    HttpSession session;
    @Resource
    SystemLogService logService;

    @Around("@annotation(com.javasm.bootdemo.common.interfaces.SaveLog)")
    public Object aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable {
        Object proceed = joinPoint.proceed();
        //获取用户信息
        Object user = session.getAttribute("user");
        Integer uid = -1;
        if (user != null){
            WebUser webUser = (WebUser) user;
            uid = webUser.getUid();
        }
        //获取类名
        String className = joinPoint.getTarget().getClass().getName();
        //方法名
        String methodName = joinPoint.getSignature().getName();
        //参数
        Object[] args = joinPoint.getArgs();
        String argsString = Arrays.toString(args);
        //组装参数
        SystemLog systemLog = new SystemLog(uid,className,methodName,argsString);
        logService.insert(systemLog);
        return proceed;
    }
}

事务

开启事务

复制代码
@EnableTransactionManagement//开启事务

需要开启事务的方法上
@Transactional

事务是否生效,完全看是否抛出异常
    看方法是否向spring抛出异常
  • 在调用的一方,添加@Transactional
    • 事务生效了
  • 在被调用的一方,
    • 事务没有生效
  • 使用try...catch捕获异常,并打印,导致事务失效

Spring的事务机制,是看调用的方法,是否抛出异常

不要在每一个方法上都加@Transactional

只有多表调用的时候,才建议使用事务

跨域

复制代码
@Configuration
public class CorsConfig {

    @Bean
    public CorsFilter corsFilter(){
        //预先的配置
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        //所有的请求,都要过滤,都添加跨域
        source.registerCorsConfiguration("/**",buildConfig());
        return new CorsFilter(source);
    }
    private CorsConfiguration buildConfig(){
        CorsConfiguration config = new CorsConfiguration();
        //配置 允许所有的作用域
        config.addAllowedOrigin("*");
        //头信息
        config.addAllowedHeader("*");
        //方法
        config.addAllowedMethod("*");
        //cookie,session会失效
        config.setAllowCredentials(true);
        //有效期
        config.setMaxAge(3600L);

        return config;
    }
}
相关推荐
西岸行者5 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
starlaky5 天前
Django入门笔记
笔记·django
勇气要爆发5 天前
吴恩达《LangChain LLM 应用开发精读笔记》1-Introduction_介绍
笔记·langchain·吴恩达
悠哉悠哉愿意5 天前
【单片机学习笔记】串口、超声波、NE555的同时使用
笔记·单片机·学习
勇气要爆发5 天前
吴恩达《LangChain LLM 应用开发精读笔记》2-Models, Prompts and Parsers 模型、提示和解析器
android·笔记·langchain
qianshanxue115 天前
计算机操作的一些笔记标题
笔记
土拨鼠烧电路5 天前
笔记11:数据中台:不是数据仓库,是业务能力复用的引擎
数据仓库·笔记
土拨鼠烧电路5 天前
笔记14:集成与架构:连接孤岛,构建敏捷响应能力
笔记·架构
烟花落o5 天前
栈和队列的知识点及代码
开发语言·数据结构·笔记·栈和队列·编程学习