In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "what is the method of lookup-method injection in Sping". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is the method of lookup-method injection in Sping"?
Lookup-method injection
Lookup-method injection is when the container overrides the method on the container management (container-managed) bean and returns the lookup result of another named bean in the container. Lookup usually requires a prototype bean, as described in the previous section. The Spring framework uses the CGLIB library (CGLIB is a powerful, high-performance code generation library. It is widely used in the AOP framework (Spring, dynaop) to provide method interception operations) bytecode generation dynamic generation of subclasses covering the method to achieve this method injection.
You need to pay attention to a few points:
For this dynamic subclass to work, the Spring bean container subclass cannot be a final class, and the method to be overridden cannot be a final class.
Unit testing classes with abstract methods need to subclass the class themselves and implement other abstract methods.
Concrete classes provide the specific methods required by the component lookup.
Another key limitation is that lookup-method cannot go with factory methods, especially the @ Bean method in the configuration class, because in this case the container is not responsible for creating instances, so you cannot dynamically create run-time generated subclasses.
For the CommandManager class in the previous code, the Spring container dynamically overrides the implementation of the createCommand () method. The CommandManager class does not have any Spring dependencies, as shown in the rewritten example:
Package fiona.apple
Public abstract class CommandManager {
Public Object process (Object commandState) {/ / get the new instance of the command interface Command command = createCommand (); / / set the status command.setState (commandState) on the (hopefully brand new) command instance; return command.execute ();}
/ / Yes. But where is the implementation of this method? Protected abstract Command createCommand ();}
In the client class of the method to be injected (in this case, CommandManager), the method to be injected requires a signature in the following form:
[abstract] theMethodName (no-arguments)
If the method is abstract, the dynamically generated subclass implements the method. Otherwise, the dynamically generated subclass overrides the method defined in the original class. Use the following example:
The bean of commandManager calls its own createCommand () method when it needs a new instance of myCommand bean. If you really need myCommand bean, you must deploy it as prototype. If it is singleton, the same myCommand bean instance is returned each time.
Alternatively, you can often rely on target bean to solve the problem based on the return type of lookup method:
Public abstract class CommandManager {
Public Object process (Object commandState) {MyCommand command = createCommand (); command.setState (commandState); return command.execute ();}
@ Lookup protected abstract MyCommand createCommand ();}
Note that you should usually declare annotated lookup method with a concrete subclass implementation so that they are compatible with Spring's component scanning rules, where abstract classes are ignored by default. This restriction does not apply to bean classes that specify registration or import.
Another way to access a different Scoped target bean is the ObjectFactory/Provider injection point. Think of Scoped bean as a dependency.
You may also find ServiceLocatorFactoryBean (in the org.springframework.beans.factory.config package) very useful.
Any method to replace
One form of method injection that is not as useful as lookup method injection is the ability to replace any method in a managed bean with another method.
With XML-based configuration metadata, you can replace the existing method implementation of the deployed bean with alternative method elements. Consider the following class, which has a method called computeValue that overrides it:
Public class MyValueCalculator {
Public String computeValue (String input) {}
/ / other methods...}
The class that implements the org.springframework.beans.factory.support.MethodReplacer interface provides a new method definition, as shown in the following example:
/ * used to rewrite the existing CtudioValueString (String) implementation in MyValueCalCulter * / public class ReplacementComputeValue implements MethodReplacer {
Public Object reimplement (Object o, Method m, Object [] args) throws Throwable {/ / gets the input value, processes it, and returns the result String input = (String) args [0];. Return;}}
Deploy the initial class and specify the bean that the method overrides, similar to the following example:
String
You can use one or more in to indicate the method parameter type of the method to be overridden. The type of the parameter is required only if the method is overloaded and multiple types exist in the class. For convenience, the type string of a parameter can be a substring of a fully qualified type name. For example, all of the following match java.lang.String:
Java.lang.StringStringStr
Because the number of parameters can distinguish each possible situation, the simplified approach saves a lot of input and only allows you to type the shortest string that matches the parameter type.
At this point, I believe you have a deeper understanding of "what is the method of lookup-method injection in Sping". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.