为什么不能自动装配具有@Transactional方法的类? [英] Why can a class with a @Transactional method not be autowired?

查看:59
本文介绍了为什么不能自动装配具有@Transactional方法的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有WAFFLE过滤器的Spring Security,该过滤器根据ActiveDirectory服务器对用户进行身份验证. 我还创建了一个额外的过滤器,该过滤器也可以根据我的数据库对用户进行身份验证(它只是检查是否 先前已通过身份验证的用户在数据库中).这是通过UserDetailsService的实现来完成的.

I'm using Spring Security with a WAFFLE filter that authenticates the users against an ActiveDirectory server. I created an additional filter that authenticates the user against my database as well (it just checks if the previously authenticated user is in the database). This is done using an implementation of UserDetailsService.

这种组合一直有效,直到我向服务中添加了一个带有@Transactional注释的方法.现在的服务 无法自动连接到过滤器.

This combination was working until I added an @Transactional annotated method to the service. Now the service can not be autowired to the filter.

这是服务类别:

@Service
public class UserService implements UserDetailsService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private LdapUserDao ldapUserDao;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return getUserByUsername(username);
    }

    public User getUserByUsername(final String username) {
        final User databaseUser = userRepository.findByUsername(username);
        final User ldapUser = ldapUserDao.findByUsername(username);

        if (null == databaseUser || null == ldapUser) {
            return null;
        }

        final User user = mergeUsers(databaseUser, ldapUser);

        return user;
    }

    public List<User> getAllUsers() {
        return userRepository.findAll();
    }

    @Transactional
    public void storeUser(final User user) {
        userRepository.save(user);
    }

    private User mergeUsers(final User database, final User ldap) {
        final User mergedUser = new User();

        mergedUser.setId(database.getId());
        mergedUser.setUsername(database.getUsername());
        mergedUser.setEnabled(database.isEnabled());
        mergedUser.setRoles(database.getRoles());

        mergedUser.setEmail(ldap.getEmail());
        mergedUser.setFirstName(ldap.getFirstName());
        mergedUser.setLastName(ldap.getLastName());
        mergedUser.setLocale(ldap.getLocale());

        return mergedUser;
    }

}

这是过滤器:

public class WaffleAuthenticationWrapperFilter extends GenericFilterBean {

    @Autowired
    private UserService userService;

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
            ServletException {
        final SecurityContext securityContext = SecurityContextHolder.getContext();
        final Authentication authentication = securityContext.getAuthentication();

        if (null == authentication || !(authentication instanceof WindowsAuthenticationToken)) {
            chain.doFilter(request, response);
            return;
        }

        final WindowsAuthenticationToken waffleAuthentication = (WindowsAuthenticationToken) authentication;
        final String username = waffleAuthentication.getName().replaceAll("^.*\\\\", "");
        final User user = userService.getUserByUsername(username);

        if (null == user) {
            securityContext.setAuthentication(null);
            return;
        }

        final WaffleAuthenticationWrapper authenticationWrapper = new WaffleAuthenticationWrapper(waffleAuthentication,
                user);

        securityContext.setAuthentication(authenticationWrapper);

        chain.doFilter(request, response);
    }

}

一旦我删除了@Transactional,项目就会再次编译.为什么无法将方法注释为@Transactional?

As soon as I remove the @Transactional the project compiles again. Why is it not possible to annotate the method as @Transactional?

这是堆栈跟踪:

Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:474)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
    at my.project.App.main(App.java:13)
Caused by: org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.initialize(TomcatEmbeddedServletContainer.java:98)
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.<init>(TomcatEmbeddedServletContainer.java:75)
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getTomcatEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:378)
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:155)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:157)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130)
    ... 7 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration.setFilterChainProxySecurityConfigurer(org.springframework.security.config.annotation.ObjectPostProcessor,java.util.List) throws java.lang.Exception; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webSecurityConfig': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private my.project.auth.WaffleAuthenticationWrapperFilter my.project.config.WebSecurityConfig.waffleAuthenticationWrapperFilter; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'waffleAuthenticationWrapperFilter': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private my.project.services.UserService my.project.auth.WaffleAuthenticationWrapperFilter.userService; nested exception is java.lang.IllegalArgumentException: Can not set my.project.services.UserService field my.project.auth.WaffleAuthenticationWrapperFilter.userService to com.sun.proxy.$Proxy84
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:368)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1119)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1014)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
    at org.springframework.boot.context.embedded.ServletContextInitializerBeans.getOrderedBeansOfType(ServletContextInitializerBeans.java:209)
    at org.springframework.boot.context.embedded.ServletContextInitializerBeans.addAsRegistrationBean(ServletContextInitializerBeans.java:165)
    at org.springframework.boot.context.embedded.ServletContextInitializerBeans.addAsRegistrationBean(ServletContextInitializerBeans.java:160)
    at org.springframework.boot.context.embedded.ServletContextInitializerBeans.addAdaptableBeans(ServletContextInitializerBeans.java:143)
    at org.springframework.boot.context.embedded.ServletContextInitializerBeans.<init>(ServletContextInitializerBeans.java:74)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getServletContextInitializerBeans(EmbeddedWebApplicationContext.java:234)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.selfInitialize(EmbeddedWebApplicationContext.java:221)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.access$000(EmbeddedWebApplicationContext.java:84)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext$1.onStartup(EmbeddedWebApplicationContext.java:206)
    at org.springframework.boot.context.embedded.tomcat.TomcatStarter.onStartup(TomcatStarter.java:54)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5151)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration.setFilterChainProxySecurityConfigurer(org.springframework.security.config.annotation.ObjectPostProcessor,java.util.List) throws java.lang.Exception; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webSecurityConfig': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private my.project.auth.WaffleAuthenticationWrapperFilter my.project.config.WebSecurityConfig.waffleAuthenticationWrapperFilter; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'waffleAuthenticationWrapperFilter': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private my.project.services.UserService my.project.auth.WaffleAuthenticationWrapperFilter.userService; nested exception is java.lang.IllegalArgumentException: Can not set my.project.services.UserService field my.project.auth.WaffleAuthenticationWrapperFilter.userService to com.sun.proxy.$Proxy84
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:649)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 35 more
Caused by: org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webSecurityConfig': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private my.project.auth.WaffleAuthenticationWrapperFilter my.project.config.WebSecurityConfig.waffleAuthenticationWrapperFilter; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'waffleAuthenticationWrapperFilter': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private my.project.services.UserService my.project.auth.WaffleAuthenticationWrapperFilter.userService; nested exception is java.lang.IllegalArgumentException: Can not set my.project.services.UserService field my.project.auth.WaffleAuthenticationWrapperFilter.userService to com.sun.proxy.$Proxy84
    at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:164)
    at org.springframework.beans.factory.support.AbstractBeanFactory.evaluateBeanDefinitionString(AbstractBeanFactory.java:1365)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:957)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:606)
    ... 37 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webSecurityConfig': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private my.project.auth.WaffleAuthenticationWrapperFilter my.project.config.WebSecurityConfig.waffleAuthenticationWrapperFilter; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'waffleAuthenticationWrapperFilter': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private my.project.services.UserService my.project.auth.WaffleAuthenticationWrapperFilter.userService; nested exception is java.lang.IllegalArgumentException: Can not set my.project.services.UserService field my.project.auth.WaffleAuthenticationWrapperFilter.userService to com.sun.proxy.$Proxy84
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:523)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:512)
    at org.springframework.security.config.annotation.web.configuration.AutowiredWebSecurityConfigurersIgnoreParents.getWebSecurityConfigurers(AutowiredWebSecurityConfigurersIgnoreParents.java:52)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.expression.spel.support.ReflectiveMethodExecutor.execute(ReflectiveMethodExecutor.java:112)
    at org.springframework.expression.spel.ast.MethodReference.getValueInternal(MethodReference.java:129)
    at org.springframework.expression.spel.ast.MethodReference.access$000(MethodReference.java:49)
    at org.springframework.expression.spel.ast.MethodReference$MethodValueRef.getValue(MethodReference.java:342)
    at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:88)
    at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:120)
    at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:242)
    at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:161)
    ... 41 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private my.project.auth.WaffleAuthenticationWrapperFilter my.project.config.WebSecurityConfig.waffleAuthenticationWrapperFilter; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'waffleAuthenticationWrapperFilter': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private my.project.services.UserService my.project.auth.WaffleAuthenticationWrapperFilter.userService; nested exception is java.lang.IllegalArgumentException: Can not set my.project.services.UserService field my.project.auth.WaffleAuthenticationWrapperFilter.userService to com.sun.proxy.$Proxy84
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 63 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'waffleAuthenticationWrapperFilter': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private my.project.services.UserService my.project.auth.WaffleAuthenticationWrapperFilter.userService; nested exception is java.lang.IllegalArgumentException: Can not set my.project.services.UserService field my.project.auth.WaffleAuthenticationWrapperFilter.userService to com.sun.proxy.$Proxy84
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1127)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1044)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
    ... 65 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private my.project.services.UserService my.project.auth.WaffleAuthenticationWrapperFilter.userService; nested exception is java.lang.IllegalArgumentException: Can not set my.project.services.UserService field my.project.auth.WaffleAuthenticationWrapperFilter.userService to com.sun.proxy.$Proxy84
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 76 more
Caused by: java.lang.IllegalArgumentException: Can not set my.project.services.UserService field my.project.auth.WaffleAuthenticationWrapperFilter.userService to com.sun.proxy.$Proxy84
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.set(Unknown Source)
    at java.lang.reflect.Field.set(Unknown Source)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:557)
    ... 78 more

推荐答案

要应用AOP spring使用代理,这些代理有两种:JDK动态代理(基于接口)和基于CgLIB的代理(基于类).默认情况下,Spring对实现1个或多个接口的类使用JDK动态代理.

To apply AOP spring uses proxies, these come in 2 flavors JDK Dynamic Proxies (interface based) and CgLIB based proxies (class based). By default Spring uses JDK Dynamic Proxies for classes that implement 1 or more interfaces.

在运行时,您将获得一个动态创建的对象(代理),该对象实现包装的类(UserService)的所有接口(UserDetailsService).因此,动态创建的对象是UserDetailsService,而不是UserService.

At runtime you get a dynamically created object (the proxy) which implements all the interfaces (UserDetailsService) of the class it is wrapping (UserService). So that dynamically created object is a UserDetailsService but not a UserService.

因此出现无法将代理转换为UserService的错误.

Hence the error that the proxy cannot be cast to the UserService.

您(至少)有2种方法来解决此问题

You have (at least) 2 ways of fixing this

  1. 接口程序
  2. 启用基于类的代理.

程序到接口

在调用getUserByUsername时,您需要创建一个新接口并实现该接口以及UserDetailsService.

Program to interfaces

As you are calling the getUserByUsername you would need to create a new interface and implement this as well as the UserDetailsService.

public interface UserService {
    User getUserByUsername(String username);
    void storeUser(User user);
    List<User> findAllUsers();
}

@Service("userService")
public class UserServiceImpl implements UserDetailsService, UserService { ... }

如果您这样操作,则可以保持过滤器不变,因为UserService现在只是一个界面.如果给接口命名,则还必须将过滤器中的UserService字段更改为该类型.

If you do it like this you can leave your filter untouched, as the UserService is now simply an interface. If you give the interface another name you would also have to change the UserService field in the filter to be of that type.

使用Spring Boot启用基于类的代理就像在application.properties文件中添加属性一样容易.

As you are using spring boot enabling class based proxies is as easy as adding a property to your application.properties file.

spring.aop.proxy-target-class=true

这将创建一个基于类的代理,这样您就可以保持过滤器的代码不变.

This will create a class based proxy and that way you can leave your code for the filter unchanged.

这篇关于为什么不能自动装配具有@Transactional方法的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆