Today we have in the virtual studio the most famous Spring speaker in the world - Josh Long.
It is his reports that open Java-conferences around the world. It is he who answers community questions, does Spring Tips on YouTube, it is his “This week in Spring” that we read every week and much more.
By the way, Josh allowed to use all the materials in our own “This week in Java” , but he makes them in such a volume and depth that this data has never been compressed to the format of “digest for 15 minutes.”
Sometimes it seems that he is in all cities at the same time, reads reports and writes articles at one moment. Today we will understand how he does it. We learn about the "code jurgenization", the reasons for Spring's amazing vitality and how he managed to live for so many years without global rewriting from scratch and other interesting features.
Josh Long, Spring Developer Advocate at Pivotal
Yevgeny Trifonov, Oleg Chirukhin - edited by JUG.ru Group
Everyone knows that you travel a lot, but it would be interesting if you could give some statistics on how many conferences you attended last year, how many flights you made and the like.
Good question. I have a table in which all the meetings that I attend in a year are recorded. My assistant, Tasha, helps me to organize the schedule and this table. So, let's open this tablet on the phone ... Two hundred and forty. In 2018, at the moment, I attended 240 different events. Some of these meetings take place online (like ours with you), but I have to fly to most. During this year I flew more than half a million miles. Let me check what the distance from the Earth to the Moon ... It is 239 thousand miles, so that I could fly to the Moon and back.
"To the Moon and back with Spring." A good title for Tolkien’s book.
Do airlines offer you special deals because you fly so much?
Well, I'm not complaining about the service. I often fly with United Airlines, and they have a Global Services program. You can get there only by receiving an invitation - you yourself cannot ask it for yourself, and it’s not known what its criteria are. Every year they invite people who fly the most. Last year, someone said that they choose 1% of the most frequent customers around the world. Or maybe they filter by volume of costs. Somehow, they treat me very well. It's nice. But even in this case, I think, for the majority of people, hops overlap. It definitely concerns me. I fly not for the flights themselves, but to meet people who use Spring. I would teleport if I had the chance. Life would be much simpler and more interesting.
Good reason to wait for Hyperloop.
Yes, but this will have to wait long.
Let's talk about a topic closer to Spring. You communicate about it with people all over the world - are there any regional differences in its prevalence?
I would not say. Spring has always been 100% open source, and as long as people write software that will work on the Internet, Spring will continue to enjoy success around the world. True, there are other open-source projects where regional specificity is noticeable. I’ll not be able to provide statistics now, but it seems to me that GridGain is very common in Russia.
Yes it's true.
And in China, MyBatis is very popular. In the United States and Europe, I have not seen him for 10 years, and in China it is distributed as well as Spring. And, in essence, there is nothing bad in MyBatis, it is fast and powerful - so why not use it?
We did a project in which MyBatis works together with GridGain, it is inside there.
(laughs) So you are using both those and others - great! I like both of these technologies. There was no normal support for MyBatis, and I constantly asked various huge companies in China that did amazing things - why do they need MyBatis along with Spring? There are many other options, Hibernate works very well, and MyBatis is practically not used in the West. In the end, I decided that, once the program is used, it is necessary to ensure its normal operation. If you now look at the Spring Boot Starter sources for MyBatis, you will see my name there. I wanted those who need this program in the east to work with it normally. In general, much of my work derives from such discoveries that I make in different countries during my travels.
You are the Developer Advocate in Pivotal for Spring projects. What does this mean in practice? What exactly are you doing?
Tough question. The Developer Advocate concept has become widespread thanks to Apple and, in particular, Microsoft. It began about 30 years ago, when Microsoft realized that it was developers who had real power. If you want your platform to be a success and be useful, you need the developers to create new valuable things on this platform. Accordingly, you must convince them of the advantages of your platform. Microsoft began to try to motivate developers in a number of different ways. They made Visual Studio very cheap, i.e. they provided the available tools. They have created many very interesting APIs with which you can work under Windows. In general, Microsoft realized that it was necessary to cooperate with the community. People trust not software, but other people. People have no emotional attachment to the software. One may imagine that you will become famous by simply writing a program in privacy in your hotel room, and who needs it, they will find the documentation themselves. But in reality this usually does not happen. Much of my responsibility is communicating with people from the community. I listen to what people are saying, and bring it to the developers, and then pass on to the community what the developers are saying. In other words, I am a kind of conductor.
The Spring team is weird and beautiful. She is more than fifteen years old. At first, she was very small, and the developers themselves were engaged in consulting. They constantly worked with other teams, helping them to create their own software, that is, they solved real problems, and did not sit in the ivory tower. They did not pretend to know in advance what software was needed, but wrote what others really needed, and the use was always the main criterion for them. They shared with their clients their suffering in solving problems. We do not create unnecessary things to anyone. And this, as you know, is often a problem when creating software. In fact, the first Spring team already consisted of Developer Advocates. They were not like what programmers usually represent. Yes, they were talented developers, but at the same time they willingly discussed their ideas with other people, attended presentations, met with people, made friends, held dialogues. Thanks to this, Spring quickly gained great popularity.
The problem is that this approach doesn’t scale well. It’s impossible for all developers to spend half their time on traveling, chatting and meeting. At the time when I joined the team, I already wrote code for Spring, since it is an open source project. In addition, I already had published books, I wrote articles and made reports. So, I already tried my best to present Spring to the community and give the opportunity to the maximum number of people to get to know it. Therefore, the Spring team offered me to do everything the same, but on an ongoing basis and for money. I think I’m 80% of the Developer Advocate and 20% of the programmer, while the rest of the Developer Advocate team is 20% and 80% of the programmers.
In general, my job is to keep in touch with the community, and there are many different ways to do this. I am writing my sixth book now, it is called The Reactive Spring Book; My previous one was published by O'Reilly under the name "Cloud Native Java." I made a lot of video tutorials that can be viewed on Safari Books Online , each of them for 4-5 hours. In addition, every Wednesday I post a Spring Tips video on YouTube , each of which is dedicated to a specific narrow aspect of the ecosystem. They usually last from 45 minutes to an hour, every year I do at least two seasons, sometimes three. Thus, every year, every two weeks I prepare enough material for a full report at the conference. Starting from January 2011, every Tuesday, without exception, I make a new entry in the blog “This week in Spring” , in which I review all the most interesting things in the ecosystem. I also have other blogs, the last two evenings I have been doing just that. In addition, I also write code and speak at conferences. So my work involves the maximum of the most diverse activities - but I could very well be limited to something one. There are people who only blog or just shoot a video, and they do it very well. Some do not even travel, but conduct seminars online. My approach is different from others, but in the end, all this activity has the same goal.
By the way, my responsibilities also include doing tutorials, blogging, and so on. For me it is very difficult and can take an arbitrary amount of time. How hard is it given to you? Say, how much time does it take to prepare one Spring Tips video?
I forgot to say - I had a new podcast, it still didn’t work in it, but seven interviews have already been prepared :-) As for the time for preparation, here it happens differently. If I am already familiar with the topic that I decided to cover, then it is enough for me to sit down and write everything down two or three times - I always make many mistakes. It takes about four hours once a week, that is, a little. But in other cases this is not enough. Sometimes I study a certain problem and make notes on it for many months, and then I decide that, since this work has been done, then why not make a video of these records. I constantly learn, as probably you. But such situations, when I need to learn something specifically for the video, rarely arise. The hardest thing here is to decide that you need to sit down and delve into the topic, and not the learning process itself.
Sometimes programmers from our team release something that no one has ever created before. In this situation, people, of course, will have questions, and these questions will fall on programmers by default. And I can make a video where everything will be explained, and put it online in advance so that people have time to meet. Obviously, in this situation, I also have to learn - if we are talking about something completely new.
How many projects are there in Spring?
Good question. I think a few dozen. There are very specialized modules, some of which are developed by the community, some by the Spring team at Pivotal, or other large companies. All support for Google GCP was made at Google, support for Microsoft Azure is at Microsoft. But a lot - like, for example, MyBatis - is being developed by the community. In addition, we have separate modules, for example, Spring Data Neo4j, a module for the Neo4j graph database. It is part of the Spring Data project, but it was made in collaboration with Neo4j, they did most of the work on this project, he just lived in our git-repository. There are many such examples.
As for Spring Boot, we have a wonderful mechanism with it called Auto-configuration. It provides people with a convenient way to group what they will work on. The person simply downloads the JAR file to his classpath, and it is automatically added to the running Spring Boot App. There are a lot of such Auto-configurations in the ecosystem, I just don’t know about a significant part. They work like plugins.
And how to understand users in all this variety of projects? Is there some general structure or idea?
Most likely, you do not need all the projects. Mark your goal, and select the desired project based on it. I do not like it when people try to "learn Spring" - this is a meaningless exercise. The question should be put like this: I need to write, say, a REST API. My first step is to go to spring.io/guides , where you can find a simple and accessible guide that is 10-15 minutes long. It will have everything you need to know: what code to write, in which folder, how to do it in IntelliJ or in Eclipse or in something else. We try to explain everything in great detail and do not omit anything, because we want these guidelines to be accessible to everyone. Whatever you do - JMS, Neo4j, security, circuit breakers, Kafka - we have a separate manual for each topic. Decide on your task and select the appropriate guide. You need to think not about Spring, but about what you will integrate with your system - Spring is just a tool that allows this integration. Therefore, there is no sense in “learning Spring” - it should be used if it can simplify your specific task.
What projects in Spring are, from your point of view, the most promising? Or the most underrated?
The most popular library is Spring Retry. It was originally developed in Spring Batch. I don’t know if you ever used Spring Batch, it allows you to process large amounts of sequentially transmitted data, for example, documents from the file system, XML, CSP files and so on. In one of the options for using this tool, you read the record, and then write it down - for example, from a web service to a message queue. Processing of this data can go on for hours, and it would be highly undesirable if the system, due to one error, rolled back all the work done during the night at the very end. You can't do that. Spring Batch works with data packets, it processes records not one at a time, but ten or one thousand each. Even if the processing of thousands of entries disappears, all the rest will be saved. In addition, when writing package systems, you need to keep in mind that you need to access other services that may fail. For this there is a Spring Retry. This library allows you to make repeated calls to services. In addition, it can use exponential exposure. In addition to Spring Batch, Spring Retry is also used in Spring Integration, Spring Cloud Stream, Spring Cloud Data Flow. In the last two, we support Spring Retry because of its connections with some other things. Thus, this library is used in many Spring projects, and I’m not sure that everyone knows about it. Spring Retry is a very often used library that is sometimes simply overlooked. In general, we have a lot of different jet things. They are usually the most interesting.
Why exactly reactive?
We have reactivity everywhere. The advantage of Spring is that you can start and complete a project with it. This week we announced that we will support the Facebook RSocket project. It is a fully reactive gRPC counterpart, but significantly more flexible. It can be used for pub / sub, streaming, client request / response - in general, with it you can implement many different messaging patterns. And it is used in Facebook. There are two bundles, one in C ++, the other in Java. The one that is in Java is written using Reactor, our reactive library. It is used by Salesforce. Of course, there are other options. Have you heard of gRPC from google? It is very high quality and interesting, but it is not reactive, by default it does not work well with reactive types. This shortage is not at Salesforce gRPC. He has a compiler that creates services based on Spring Reactor. So both Facebook and Salesforce were able to scale Reactor for their needs.
Reactor itself is one of our most interesting projects. RxJava came out earlier, but Reactor was the first to provide support for reactive streams. This largely happened because the great programmer David Carnok, the RxJava project manager, worked with us on Reactor. So much of the new and interesting things that happened in our ecosystem, one way or another touched Reactor. Due to this, the project has become extremely attractive for companies that create large systems. Reactor also underlies the Spring WebFlux framework. Also, on the basis of Reactor, we made support for reactive messaging, reactive web sockets, Spring Cloud Stream, reactive circuit breakers and so on - all of these components can be integrated into reactive applications. You can, of course, just use Spring, but I prefer to use the entire ecosystem using these reactive types.
In addition, we recently announced R2DBC, our API for SQL-based data access for reactive drivers. Reactive JDBC does not yet exist. The problem is that if you use a reactive code, then you cannot use the lock. If blocking is used, then this interaction needs to be scaled, increasing the number of threads. And this is contrary to the original goal, since we just want to avoid scaling up by increasing the number of threads - this is too expensive. Therefore, R2DBC provides an abstraction that provides reactive access to data. There are database drivers that are initially reactive — for example, Postgres. So you can use R2DBC, among other things, with Postgres and work with reactive SQL without using locking, since you don't have threads. This whole topic seems to me extremely interesting.
Perhaps you could answer some frequently asked technical questions? One of the problems is that when using multiple Spring projects, multiple annotations occur at the same time. It becomes difficult to understand what each of them is doing - you can’t just click on it, hold down the Ctrl key and go to the definition. How to behave in this situation?
It should be run with the `--debug` option. Most of the Spring ecosystem now exists in the form of Auto-configuration. Some part is like an imported configuration, if in this case you click on the annotation, you will see the inscription `@ Import`, which will then contain its class. If you then click on it, you will see the objects that were created for you. But sometimes Spring acts through the Auto-configuration mechanism — in this situation no annotations are created. There is only a library and classpath. Then it's really hard to figure out what is happening and why. We want to make it as easy as possible for people to figure it out. For this, there is the `--debug` parameter, which must be entered when the application is started. Then you will see a report that describes all the steps performed by the application at startup. The application checks whether some fields or classes exist, and, depending on this, it decides whether to create some objects for it or not. The report shows you which conditions were met, which did not, and which configurations did not depend on any checks. So, if you can’t understand, for example, why your connection to Kafka doesn’t work, you can look at the corresponding condition - you may not have the necessary class in the classpath.
Now there will be a little trolling question. Often, opponents of Spring declare that it is the cause of low-quality architecture, because in it you can simply use
@Autowired
anywhere and everywhere. Do you agree with this opinion? Ten years ago, the architecture was created in three layers, the values were transferred from the constructor to the constructor, the DTOs were created. With Spring you can get rid of all this. Is this good or bad?
Spring is a tool. It allows more people to create software that comes to production and works correctly, so I do not think it is harmful. In recent years, millions of people have created applications with the help of it, and everything works fine for them. To consider that Spring as a whole is harmful is absurd, such an opinion otherwise than ignorant cannot be called.
In Spring, you can create quite explicit wiring. If you are concerned that it will be unclear where the object was attached to, you can link it yourself. You can create a java-configuration in which one method will call another, and the output value of the method being invoked will be a bean - Spring will take care of it. Even with repeated or thousand-time calling of the same bean, you will never have more than one link if this object is singleton. You want everything to be explicit - this can be done. If you wish, you can turn to an even more boring option - XML. The bottom line is that if you do not want to use `@ Autowired`, then this can be avoided.
If you use Spring Boot in the most usual way, then you will have only one bin. You know that you need a `ConnectionFactory` bin - in this case there is no problem in making an injection by type. You still have only one object, two `ConnectionFactory` for one` Connection` you will not have. You will not have two Hibernate sessions, so nothing prevents you from giving an injection of `HibernateSession` - it still exists in a single copy. If you want everything to be explicit, you can use the `@ Qualifier` annotation. With the help of it, you mark that, the injection of which you make by type. This will make it possible to distinguish different implementations of bins from each other. Suppose you have a service with which applications in one place can communicate with iTunes, in the other - with Amazon, in the third - with the Android Play Store. These will be three different beans of a different type, but each will have an annotation `@ Qualifier`. Further, this annotation can be superimposed on another annotation, thus ensuring type safety. You will bind this type, and you will have a summary on top of it, and when the bean is created, it will have a summary of `@ Qualifier`, including on the consumer site. Thus, these annotations will provide you with links, and even if you have three implementations while the program is running, you can still find the one you need.
That is, in general, in your opinion, flexibility is a positive feature. Do you and the Spring team adhere to some basic principles, like Python Zen?
Yes, stick. By the way, I have been programming in Python since the end of the 90s, and I am a big supporter of Zen Python, he has a very correct general idea. As for your question, here you need to tell about Jurgen Höller, co-founder of Spring and the second developer who worked on the project. This is the sweetest soul of a man, very quiet, one of the best with whom I had to communicate. It is very easy to talk to him if, for example, you met him at a conference. At the same time, he has a powerful intellect, and his contribution to Spring can not be overestimated. The JAX conference is very important, although, of course, it does not reach the Joker :) - it gave him a special award for the work that no one else but him had ever received. Usually awards are given in a special category, but this one is without a category and belongs only to Jürgen. Spring is the only project in the Java ecosystem that has never been rewritten in 15 years - because there is no need for it. Jurgen has a very clear idea of how to write modules, how to express types, in general, how to create a base of high-quality code that can live for decades and change easily if necessary. There are no other such projects in the Java ecosystem. Much of the projects no longer exist, and even among those that have ever been rewritten from scratch, very few have lived for 15 years.
Without being rewritten, only Spring existed for 15 years, because from the very beginning it was written very cleanly. Jurgen taught us some basic principles that we follow: how to make modules, how to make packages-implementations (as opposed to the public part), how to describe the surface API level and design types for it, which patterns should be used. Using Spring, you inevitably get acquainted with design patterns, and it has always been like this. Immediately comes to mind an example of a pattern Template. The Spring Integration framework introduces you to the MVC enterprise integration patterns, Spring MVC. All these patterns are part of the framework, and we use them based on proven practices. Our Zen are the principles that Jurgen taught us. In this respect, we are all his disciples. Jürgen is known for his slightly odd approach to how to write code - because of his manner, there was even a special term, “jurgenize”. There are many truly intelligent people with a lot of experience in our team; some already have degrees and bald spots. And yet, Jurgen makes his changes to their code. Sometimes these little things are like adding spaces, cleansing, and so on, but sometimes he rewrites the code from scratch a thousand times better, nor does he change the authorship of the code, that is, commit under the name of the original author. In these cases, it is said that the code was “jurinized”, that is, it corrected it. Thanks to Jürgen, our code is very clean. For code analysis tools, it is considered to be a sign of high quality to find at least some malfunction in Spring, because there is a very clean code. In general, I can’t tell you our principles in detail right now, but they exist, they were laid by Jurgen, and all Spring projects follow them. As for Spring Boot, we have very precise rules for creating modules, which we call starters.
OK, thanks for answer. My last question will be about performance and Java 11. As far as I know, Spring always generates a context in runtime. This may take from minutes to several hours in some old banking programs.
Come on? I do not believe.
Honestly, I saw it with my own eyes.
But it can not be Spring. Most likely, something terrible is done there with Hibernate. Beans can be generated in millions, empty beans can be compiled and run very quickly. It takes time to load information from the database, validate, and so on, this may cause Spring to delay. But Spring itself is very fast.
Yes, I think there really was some horror with Hibernate. And yet, Spring starts 30 seconds, sometimes a minute or two - it's still quite a long time, don't you think? If, let's say, we run it in Docker, then we need the container to start right away. Is it possible to reduce the launch time of Spring?
Spring launch takes less than two seconds from me. And with the help of the Spring Cloud Function this time can be reduced to half a second or less. We have projects that run without problems in half a second - for example, Spring Boot in the Spring Cloud Function. So, again, the problem is not in Spring, but what you do for them, what happens on the background - whether you get access to another data source on the Internet, whether you download files from the Internet, or if you upload data to the grid. . If everything you do is small microservices, then your system should work very quickly. If your application runs for a very long time, something is wrong with it.
Clear. By the way, have you heard of GraalVM and AOT?
Yes. I will tell you more: we have the Spring Fu project. He is experimental, I made a video about him from the Spring Tips series . We wrote it on Kotlin, now there is a Java API for it. This project was created for environments like GraalVM. Spring Fu does not use dynamically generated proxies or `@ Configuration`. The native image generator in GraalVM needs to know which classes will be loaded. But if you have dynamic loading with cglib or Byte Buddy, this makes it difficult to use GraalVM. Hibernate, Spring, and all other libraries that use dynamically generated proxies are poorly suited for creating a native image. Therefore, there is nothing like this in Spring Fu, this is clearly stated. It still uses Spring, but the approach to building applications is different. And one of the advantages of Spring Fu is that it works well with GraalVM. And now the application starts instantly. In a sense, generally instantly: once and it is entirely in the memory! It should be remembered that the native image generator in GraalVM is still at a very early stage of development. Because of this, sometimes there are difficulties, because it seems to people that the application launched by GraalVM immediately starts to load very quickly, and at runtime the speed will be the same as before. But this is not the case, because in Runtime with GraalVM you no longer use the JVM, the behavior of the system will be different. So test this project for health, but remember that it is not a free launch acceleration. And yet, I am delighted with GraalVM, their team put a lot of effort into working better with frameworks.
My last question will be, of course, about Java 11.
Yes, she is beautiful.
What does Spring have to do with it?
For Spring developers there are two main criteria by which we choose this or that technology: 1. whether it is suitable for business needs, 2. whether it suits us as programmers. As for the first criterion, whether this technology provides higher speed, stability, whether it has long-term support. All this is present in Java 11. Support for Java 8 will end soon, so it makes sense to switch to the next release with long-term support. By the way, my friends, if you move on to the next version, grab the OpenJDK build - it is excellent in all respects. The correct version is very easy to get, I already wrote about this on Twitter. This release works very well with Spring - it is stable. You can even now go to https://start.spring.io , generate a new project, choose Java 11 and it will work. Java 11 support is available in both IntelliJ and Eclipse.
As for the second criterion, there are no significant changes for Spring developers in Java 11. It's nice that `var` and the HTTP Client appeared, but Spring already had a reactive WebClient, so I'm not sure that there would be any good from the HTTP Client. Some handy things have been added to Java 10 and Java 9. In addition, it has become possible to run a Java program as a script — this is not bad. I'm not sure that it makes sense to do with Spring - although on the other hand, why not. How it will look, I do not know.
Was it difficult for Spring to switch to Java 11?
Not. At the heart of Spring are very high quality libraries and ecosystems. Note that the Spring framework itself supports both the classpath and the modulepath, but I personally do not recommend using the modulepath. To use it properly, everything else must support the modulepath. But this is not yet possible, now there are too few people providing support for the modulepath in libraries. The classpath mode works fine. We had quite standard problems with CGLib, AspectJ, and the JAXB XML libraries that everyone had during the transition to Java 9. All these problems were solved in about the last year, so for us the transition to Java 11 was simple.
By the way, we plan a lot of interesting things for the next Java versions, for example, the Loom project is fibers for Java.
Will it be in java 12?
Most likely not, it will do for quite some time.
Kotlin also uses coroutines, so this project is very interesting to us. Whenever he comes out, he will benefit greatly. The ability to express reactive pipelines without creating a reactive code is very important. Personally, I look forward to the appearance of multi-line String. Maybe this is not the best evidence of me - I have a lot of code that requires a large number of lines, for example, SQL queries and the like.
So this is because you have a significant part of the code optimized for display on slides. On the slides during the presentations it will be convenient to display the entire string on the screen.
Yes, otherwise it becomes difficult to read. In IDE they will look much better. Again, Python, Kotlin, Scala, Groovy - any language of the last 20 years provides support for multi-line String. In my opinion, this is quite natural.
Perhaps you would like to leave some advice to our readers? It may concern Spring or Pivotal, or it may be relative to Java as a whole.
As with any technology, the best part of Spring is its community. Technically, Spring is a very interesting set of projects, but the secret of its longevity is a wonderful community. No one needs your beautiful software if you need to communicate with unbearable people for the sake of it. Therefore, we try to be as friendly as possible. Our developers, including project managers, answer questions on Stack Overflow and monitor various tags there. We have chatrooms on Gitter, and each chatroom in it corresponds to a repository on GitHub. For example, https://gitter.im/spring-projects/spring-boot . There you can find Spring developers and ask questions that interest you.
People often ask me how to help a project, how to get started writing code for Spring. We have a lot of problems with tags like “ideal for contribution” on GitHub. We welcome everyone, but not all projects are suitable for everyone, some bugs are more difficult than others. So if you want to work with us, look for these tags and help with problems. That is how the replenishment of our community. We appreciate the work of everyone who helps to improve our ecosystem.
Minute advertising. Josh Long will come to the Joker 2018 conference with the report "Reactive Spring" . You can buy tickets on the official conference website .
Source: https://habr.com/ru/post/425961/
All Articles