小编Bab*_*abi的帖子

Spring 错误“名为‘x’的 Bean 预计为‘y’类型,但实际上为 [com.sun.proxy.$Proxy] 类型”

我正在尝试使用 Spring Security 在应用程序中实现基于 DAO 的身份验证。

当我尝试使用用户登录应用程序时,出现以下错误:

failed to lazily initialize a collection of role: com.example.app.dao.User.groups, could not initialize proxy - no Session
Run Code Online (Sandbox Code Playgroud)

看看@jcmwright80 对这个问题的回答,我明白我最好将UserDetailsServiceImplclass 注释为@Transactional. 执行此操作后,我在登录时收到错误:

Bean named 'userDetailsService' is expected to be of type 'com.example.app.service.UserDetailsServiceImpl' but was actually of type 'com.sun.proxy.$Proxy238'"}}
Run Code Online (Sandbox Code Playgroud)

这似乎是与 UserDetailsS​​erviceImpl 上创建的代理对象相关的问题 - 我怎样才能优雅地解决这个问题?

代码

安全配置的相关部分:

@Configuration
@ComponentScan("com.example.app.service")
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter  {

    @Bean
    public UserDetailsService userDetailsService() {
        return new UserDetailsServiceImpl();
    }
    
    @Bean
    public BCryptPasswordEncoder …
Run Code Online (Sandbox Code Playgroud)

java aop spring

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

aop ×1

java ×1

spring ×1