Java – how to programmatically scope beans

I tried to find the scope of the bean by its name

What I have found so far is:

beanfactory#isPrototype(String name)
           #isSingleton(String name)

In my case, I want to know if the bean is within the scope of the request I can use some internal methods in the spring framework, but I want to know if there is a "right" method

Solution

Good question

There is no isrequst () method in beanfactory because the request scope is only related to the web

I just tried to find a way to do this and failed So if you use annotations, I can suggest a feasible solution When you get a bean instance, say bean getClass(). getAnnotation(Scope.class). If this returns the scope call value ()

It's not a "scientific" method, but I hope it's good enough for you

edit

Other methods are as follows The request scope bean is stored in the request properties I don't remember its name now, but you can easily find it yourself, just check your request in the debugger Then check whether the reference to the bean exists This approach may be better, but it requires some effort to study the request properties and data structures used by the spring framework

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>