A detailed description of class objects in Java, usage scenarios in spring

Source: Internet
Author: User

With the recommendation of the new Architecture project of the mic-goo portal, in this process. I have a better understanding of the various APIs in spring, and recently received a request from the leadership, because in the process of each interface call, write a lot of duplicated code, so this time write a public method to optimize the code.

First, lambda expressions and functional interfaces are used here as the underlying method. By defining the default method in a functional interface as a common logic, invoking a specific interface through a lambda expression, to simplify typography, I'm going to list a test demo for the moment,

public class Testlamda
{

public static void Main (string[] args)
{
System.out.println (GetResult ());;

}

private static int GetResult () {
converter<string, Integer> convent = (String from), {
Return Integer.parseint (from);
};
Return Convent.convert ("56");
}

}

@FunctionalInterface
public interface Converter<f, T>
{
default T Convert (F from)
{
System.out.println ("transform from String to int");
return transform (from);

}

public T transform (F string);
}

Second, because the interface call needs to reference the class object in the Spring container, the Applicationcontext.getbean (class<t> Paramclass) method is used when the object reference is made. Because the objects in my original call container are called through the IDs in the configuration, very few objects of class classes are used. A new understanding of class classes has been made through this use. We all know that all Java classes inherit the class object, and there is a method in the object class: GetClass (). This method is used to obtain a reference to the class of an object that has been instantiated by this class, which refers to the object of class. We cannot generate a class object ourselves (the constructor is private), and the class object is created by the Java virtual machine to automatically create the class object when the classes are called, or through the DefineClass method in the class loader. All of the objects we build have a field that records the location of the object to which the object belongs in the class class.

We know that the class loader works by the parent delegation model, so we can guarantee the uniqueness of each class object, we know that there are multiple loaders in Java, each loader can load multiple classes, so as long as you get the class object, you can use its getclassloader () method to obtain a reference to the ClassLoader. Now that you understand the reference to the class object, go back to spring in the IOC container (beanfactory) Getbean () method, and you can see that the object loaded in spring by the class object and the ID in the configuration can be referenced to our object

There is a pit point in the second article, that is, if you inject two times the same class into the spring container, getting the object through the Getbean (Class<t> Class) method will give an error because there are two identical objects in the JVM, which is the container will not know which class to load

A detailed description of class objects in Java, usage scenarios in spring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.