日常bug
日常bug
1、新建的 maven 项目没有 .classpath 文件
删除 maven 本地仓库的内容,重启 eclipse 后 Update Project(Alt+F5)
2、The bean 'adminService' could not be injected as a 'com.turtledove.service.impl.AdminService'
Description:
The bean 'adminService' could not be injected as a 'com.turtledove.service.impl.AdminService' because it is a JDK dynamic proxy that implements:
com.turtledove.service.IAdminService
Action:
Consider injecting the bean as one of its interfaces or forcing the use of CGLib-based proxies by setting proxyTargetClass=true on @EnableAsync and/or @EnableCaching.
原因:
自己实现了接口,即在action层中我注入的bean是实现类,而spring boot的事务默认是使用jdk的动态代理,即基于接口的。
解决:
1、将注入 Bean 的方式改成其接口
设置 proxyTargetClass=true 在启动事务管理上@EnableTransactionManagement(proxyTargetClass=true)
2、保留接口方式注入Bean:
在 Service 层对应的实现类上配置 @Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
3、Springboot 2.4.3 跨域问题
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException:
When allowCredentials is true, allowedOrigins cannot contain the special value "*" since that cannot be set on the "Access-Control-Allow-Origin" response header. To allow credentials to a set of origins, list them explicitly or consider using "allowedOriginPatterns" instead.
修改前:
@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
.allowCredentials(true)
.maxAge(3600)
.allowedHeaders("*");
}
}
修改后:
@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
// 设置允许跨域的路由
registry.addMapping("/**")
// 设置允许跨域请求的域名
.allowedOriginPatterns("*")
// 是否允许证书(cookies)
.allowCredentials(true)
// 设置允许的方法
.allowedMethods("*")
// 跨域允许时间
.maxAge(3600);
}
}
浙公网安备 33010602011771号