Home > Java > Java Tutorial > body text

JAVAEE project structure and concurrency thoughts

高洛峰
Release: 2017-01-17 11:11:19
Original
1387 people have browsed it

The scaffolding that has long dominated the javaee field is led by spring struts2 mybatis/hibernate;

Spring:

Spring is not just for Java services. As an implementation of the cgi standard, spring is not just a framework in the Java field, the C# platform can still benefit; spring provides various convenient annotation configuration methods such as abstraction or bootde integration solutions, which greatly simplifies Javaee projects Basics;

In the process of using spring, there are two sides: one part is lightweight annotation, and the other part is inclined to full annotation.

First of all, the premise of annotation is that it must go through proxy, dynamic, static, cglib proxy. For lightweight annotations, the perspective is static or one-time annotations, such as controller annotations. These one-time annotations or compile-time annotations are initialized in the project context as an implicit one-time scan. Related are service and other similar annotations, which provide singleton lightweight object instances. Considered the first choice. This reduces the cost of proxies and reflections during the runtime, and saves better resources for the stack during the runtime.

Another category such as responsebody is a dynamic annotation or a runtime annotation. The reflection of this annotation will be executed every time a request is made. Annotations during runtime naturally occupy resources.

Generally speaking, if annotations are not necessary, they can be omitted. Based on the servlet-based request and response methods, there are no problems that MVC cannot solve, such as parameter retrieval, parameter passing, return, etc., and there is no need for runtime annotations at all. , runtime annotations seem to reduce the amount of code. In order to make up for various shortcomings in this process, a ring of dynamic annotations will be run to execute an annotation you use inside the method. For the param annotation written in the method, what code is missing compared to using request get? It just adds a layer of code interception to the original one-step process.

How to know whether an annotation is a run-time or compile-time annotation? It is very simple. Ctrl + mouse click, you will see:

@Target({ElementType.TYPE, ElementType.METHOD})

@Retention(RetentionPolicy.RUNTIME)

@Documented

Retention This enumeration type completely and clearly explains the validity period of the annotations you use.

Use spring. Everyone likes to use singletons. This is an excellent way. Singletons have nothing to do with concurrency itself, except that you have to let it cause resource identification competition.

If There must be many prototype objects in your project, which means you have abused or used the wrong objects. MVC basically takes parameters and returns. Each request is a thread, and a separate request response has their incoming and outgoing things. Completely isolated. Speaking of mybatis, many people use various resmappers in one configuration of mybatis. Each time, various beans go back and forth. When a request is made, the beans as parameters and results must be available once. Mybatis has clear sql maintenance on the surface. At the expense of greatly reducing the efficiency of jdbc, not many people care about it. Beans are nothing to them. They don’t understand and don’t care about gc. When problems arise, they add memory and add memory to the top. The only solution is time. Problem, trading space for time. The same parameter and result mapping, then you can use jdk's map to create a new bean. Which one is better? Naturally, it is jdk's own map.

I have always stubbornly believed that new an object of jdk itself consumes far less than Define a bean yourself. Why, Sorry, I do not know either. So I always map to come and map to go. When using mybatis, you must closely check whether your things are proxied to your class. The method is very simple. In the project log, open debug and see if your log is creating a new sqlsession every time. If so, Please note that your mybatis session is not pooled and is not proxied by things. If there is a competing SQL statement in a method, sorry, there will be no error. It is just that the database did not execute your SQL statement. Soon, you will find that the connection pool is used quickly and new connections are created frequently. Of course, if you can do without mybatis, don't hesitate, it is right not to use it.

Why must we use static annotations as much as possible? It is very simple. Spring classes are basically singletons. If the project is large enough, there will be enough bean instances. What do these singletons occupy? The object instance is in the heap space and the reference is in the stack. So when will gc recycle these singleton objects? what you think? Therefore, when based on annotations, try to reduce the use of dynamic proxies. Leave more resources for where they are needed.

In the past, we would say that there is no stack without heap, but now it has changed that the string constant pool of jdk1.7 has been placed in the heap.

Which is better, compiled or interpreted? Of course, it is analytical, and compiled is similar to the intermediary model. Therefore, building an excellent compiled language is much more difficult than an interpreted language.

The structure of the web is very clear. First, it is still the context, and then there is a series of components in order. Our most important thing is the servlet component, which is the standard of Javaee. The other web components are protocol standards, and everyone must have them. Then you will see that the mapping of the servlet of many projects is /. This is a bad way, because it is very simple. There is never a need for js or css to be processed through the servlet. Therefore, the mapping mainly takes into account the interaction with the server-side components of the web container. Generally, two kind of identification, such as .do and .action .do requires permission authentication, action is directly released. js etc. do not need to enter the servlet, the web context returns directly according to the URL, and then there is no MVC interception and release in the MVC, which is unnecessary, creates problems, and is not a good way to solve the problem. In this way, regardless of the intervention of nginx, your static resources are static to the web container and have no relationship with the servlet. The servlet is only concerned with what you need it to handle.

Where is the best place to write js?

Many people are used to writing js in jsp or html, which is bad.

When we build a project, we must hope that our js and css can be cached by the browser.

Then the js written in the script tag of the page is just a tag. It is no different from a div or input and will not be cached. I checked a lot of information and the cache I saw clearly stated the unit of cache. is a file. instead of labels. So write your css js in the file and import the file, so that the file will be cached. I am not completely sure about this, because there is no direct definite answer, it is just my guess.

jsp is actually a servlet, so it is a dynamic page. Every time you need to load the class for dynamic translation, then the write method in the class writes the page to http to the browser, and the browser renders it. If it is html , then it is static. There is no doubt that it is dynamic and flexible. Since it is a servlet, it is a Java object, and various Java tags and methods are possible. Static needs to be handled by yourself. Static pages use a similar macro language. It is better to use jsp directly.

How much data should be loaded at one time on the page?

If your page displays things according to categories and lists, the amount of data is very small, hundreds of items, and the type is displayed in the current takeout ordering app, then all categories and data are given at once, so The processing is handled on the client side. Category switching and previewing in the entire process are all handled on the page, including search, our client's js, and the js executed in A's mobile phone or computer will not compete with B's mobile phone or computer, right? , if you flash an ajax every time you switch a type, it will be the same web container group, so there will be competition. The more frequent the operations, the greater the competition. This point is closely related to the actual scene.

The amount of data returned by a query has little to do with performance. Tens of thousands of pieces of data are only a few tens of KB.

The number of queries, that is, the number of interactions with the server, is the direct cause of the overall performance.

The amount of data in a query is proportional to the size of the table being queried. Returning 10 items at a time will not speed up the query or return 1,000 items at a time, slowing down the query. Database operations are essentially collection applications, and Nothing is created.

The premise of tuning is to give the most appropriate amount. It is not that the more you give, the more appropriate it is. There is an upper limit on the memory that jdk or tomcat can consume on OS with different digits and different digits.

Use nginx;

Use cache when necessary;

Select message middleware or other middleware according to whether you need it;

Separate or main database Wait, it must be that the current database cannot support the business volume.

Single cases are a good way.

Multi-threading is a sharp knife, regardless of the specific language.

Maven management is a good way, but the main body of your project should be webmvc. Create a web project and embed maven as a component instead of creating a maven project and then converting it to a web project, unless it is idle of.

Using spring is currently the best scaffolding.

Use jdbc as much as possible, if possible.

If you can do things on the client, don't interact with the server. The client's resources are vast, but the server's resources are limited.

Try to send as few requests as possible. Code that sends fewer requests is good code, unless it is a real-time application.

Every tool in the code is a tool, and the API is what you need to understand most. There is no accurate answer to which one is better and which one is not.

Everything is an object, which is pure for Java. Proxy is an object, reflection is an object, object is an object, and basic data types are not objects.

Except for basic types, everything is done through objects. No matter how complex the process is, it is done through the methods of the corresponding objects combined with the parameters of the methods. How to serialize and deserialize a class. To put it bluntly, it is the IO and transmission of files, and then loading them into jvm and constructing them into objects.

rpcThe reason why rpc is not calling something in a thread, but what is being called, is proxied. The proxy converts your needs into parameters and sends them out as a data stream. The server sends your request The stream is then converted into an object, and then streamed and sent back. You then construct the object and process it through the object.

NIO is a good way, and netty is a good choice. Is there any multi-threaded socket that can surpass netty?

zookeeper is an excellent tool for a series of solutions such as good distributed registration.

These things are principles plus objects, so you have to look hard to use them.

The above is my personal understanding. Corrections are welcome.

For more articles related to JAVAEE project structure and concurrency thoughts, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!