problem in Spring session scope bean with AOP

前端 未结 1 848
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-06 14:26

I want to inject currentUser instance in HomeController class. so for every request, HomeController will have currentUser object.

My configuration:

&         


        
1条回答
  •  心在旅途
    2021-01-06 14:53

    With scoped-proxies, Spring still needs to know the type of the bean when the context is initialized, and in this case it's failing to do so. You need to try and give it more information.

    I notice that you're only specifying factory-bean in your definition of currentUser, with no factory-method specified. I'm actually rather surprised that that's a valid definition, since the two are normally used together. So try adding the factory-method attribute to currentUser, which specifies the method on userProviderFactoryBean which creates the user bean. That method needs to have a return type of your User class, which Spring will use to infer the type of currentUser.


    Edit: OK, after your comment below, it seems you've misunderstood how to use factory beans in Spring. When you have a bean of type FactoryBean, you don't need to use the factory-bean attribute as well. So instead of this:

    
        
    
    
    
        
    
    

    You just need this:

    
        
        
    
    

    Here, UserProvider is a FactoryBean, and Spring knows how to handle that. The end result will be that the currentUser bean will be whatever UserProvider generates, rather than an instance of UserProvider itself.

    The factory-bean attribute is used when the factory is not a FactoryBean implementation, but just a POJO, and it allows you to tell Spring explicitly how to use the factory. But because you're using FactoryBean, there's no need for this attribute.

    0 讨论(0)
提交回复
热议问题