Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to accelerate project development in Java application development 4

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to speed up project development in Java Application Development 4. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Template, a technology that we use every day, such as:

The Java class is the template for creating instances of Java objects.

Generic, parameterized type templates, such as List; common templates such as generic DAO, generic Service, etc.

Arrays.sort () A sorted template

DAO in Spring supports design; refer to "my summary of SpringDAO layer support"

HttpServlet design, such as the method that service forwards to the beginning of each do according to http method (doGet, doPost, etc.)

And like Struts2's ActionSupport design, we can automatically get support such as internationalization and verification after we inherit.

Template page technology such as JSP, Velocity, Freemarker, etc.

Wait a minute.

The benefits of templates are obvious: constraint + reuse. Through the template, we can separate the change from the constant, reuse the same, change can be handed over to the subclass / through the callback mechanism, but also has a constraint to prevent code from being scribbled.

Then we should make good use of this technology to speed up the development of the project. Next let's take a look at how to use template technology to speed up our development.

Next, I'll show you how to use template technology to speed up development, but I won't accept how to use template technology to develop reusable code; this article uses IntelliJ IDEA as a prototype:

Code generation

Live Template

File and code Templates

Automatic code generation

1. Code generation

If we require I + j, we can first:

Int k = add (I, j)

Then press Alt+enter to automatically generate the relevant code; instead of typing it yourself; it will automatically generate the corresponding variables and method bodies, as shown in the figure

1. Press Alt+Enter to pop up prompts such as create method / create local variable

2. You can modify the template parameters according to the generated template method.

3. There are also typical examples, such as generating constructor, getter/setter, etc. Press Alt+Insert to get the following figure:

For example, when you generate a constructor, you can select the relevant fields and automatically generate the corresponding assignment:

4. The overlay method of Ctrl+O/Alt+Insert generation; the implementation method of ctrl+I/Alt+Insert generation

5. After the code is selected, Ctrl+Alt+T pops up Surround with to generate the code that surrounds the code, such as if/else:

6. In the page with JSP tags, press Ctrl+Alt+J to generate the surrounding tags:

Wait a minute. For details, please refer to jetbrains. The "Generating Code" section of the website.

2 、 Live Template

If you have used idea, you must have used it, such as typing psvm and then pressing the Tab key, which will automatically generate the public static void main method, which will save us a lot of characters. This feature is called Live Template; in idea. Next let's take a look at what Live Template is available and how to customize Live Template.

The most common features:

Psvm- > public static void main (String [] args) {}

Psfi-> public static final int

3. Ifn will automatically generate the following figure

You can find all the templates at File--- > Settings---- > Live Templates, such as:

For example, lazy generates delayed initialization code, and inn generates if (* *! = null)

For example, fori-- > for (int j = 0; j

< ; j++) {} List list; itli ---->

For (int I = 0; I

< list.size(); i++) { Object o = list.get(i);} 等等,这个可以去Settings里查看。 接下来我们看看如何自定义自己的Live Template: 输入"缩写前缀",即在代码中输入的前缀; 模板文本; Change:选择在哪使用; 接下来在Java文件中,输入test会生成 hello world; 此处看到 $END$ 这种变量,接下来解释下: 格式是$变量名$ $END$ : 表示展开模板后光标停留的位置,比如我们的test,生成模板后,光标停留在hello world 前边; $SELECTION$ : 表示对选中的代码应用模板;如我们定义test为: 如 此处选中"int i = 1;",然后按Ctrl+Alt+T 弹出"Surround With" 然后输入test前缀,自动生成:--->

Hello int i = 1; world

There are also prefixes such as entering if for and pressing Ctrl+Shift+Enter will automatically generate the form with () and {}, which is very convenient.

If you want to define your own variables, you can directly click on the "Edit variables" to the right of the template text when writing the template: such as getting the returned variable, method name, class name, and so on.

You can refer to "Live Templates" on the official website.

3 、 File and code Templates

So far we have used code block-level templates, and what we often need in our work is:

Generate headers such as copyrighthead

Generate a class header Javadoc, such as user, time, and version

Create a configuration file such as spring, which may be copied from somewhere else each time

For example, when we do an enterprise project, we first write a CRUD DAO, Service, Controller, etc., but almost every module is similar, but we may have to repeat it every time.

Because IDEA uses velocity templates, the syntax is relatively flexible, while Eclipse is self-created, so it is not as flexible as IDEA when creating it.

File---- > Settings--- > File and Code Templates opens the template settings page

Generate copyrighted headers for Class

1. Create an Include file

First select the Includes tab, and then click create to create a

Then enter the name at Name

Extension partial input extension

Document body

This file contains my copyright

2. Add to the class template

First check Class

Use @ parse ("Coyright.java") at the top of the page to include the file you just wrote

Then create a new Java Class, and the copyright will appear at the top of the page

Custom Service template

Take KeyValueService.java as an example.

First, File--- > Settings---- > File and Code Templates go to the template settings page

Create a template, as shown

Click the Templates tab and click the add button

Enter a name at Name

Enter the extension at Extension

Enter the template body, where we can use ${NAME} and ${PACKAGE_NAME} to get the entered file name and package name, respectively

Syntax is velocity syntax, so it is very powerful, and supposedly complex requirements can also be implemented.

3. Add Class

3.1. Alt+Enter pops up the new list at the package, and select Java Class.

Then enter the prefix of Service in the pop-up interface and select the Service type:

4. Then the class is created:

/ * Copyright (c) 2005-2012 https://github.com/zhangkaitao * * Licensed under the Apache License, Version 2.0 (the "License"); * / package com.sishuok.es.maintain.keyvalue.service; import com.sishuok.es.common.inject.annotation.BaseComponent; import com.sishuok.es.common.service.BaseService; import com.sishuok.es.maintain.keyvalue.entity.HelloValue; import com.sishuok.es.maintain.keyvalue.repository.HelloRepository; import org.springframework.beans.factory.annotation.Autowired Import org.springframework.stereotype.Service; / *

User: Zhang Kaitao *

Date: 13-6-20 4:31 *

Version: 1.0 * / @ Service public class HelloService extends BaseService {@ Autowired @ BaseComponent private HelloRepository helloRepository;}

It's very convenient.

In addition, we can also define jsp templates, html templates, profile templates, and so on, to eliminate repetitive work in development.

But the main disadvantage of this approach is that there can be only one file at a time. For example, when we generate Service, DAO, Contoller and other * * are also generated automatically. Then you need automatic code generation.

4. Automatic code generation

It is estimated that many friends have used / developed code generators; in fact, it does not take long to develop a simple code generator, the core guiding idea is: the separation of change and immutability:

What remains unchanged is the structure.

The package name, class name, and entity data are changed.

So according to this idea, it is easy to write a code generator, which can be done by following these steps:

Automatically generate DAO, Service, Controller of a module

Automatically generate a module's Entity, DAO, Service, Controller according to the database

Automatically generate module code for one-to-one and one-to-many relationships

Module code that automatically generates code commonly used in a company, such as a tree

What we have seen so far are mainly these types of automatic code generation.

Code-generated template files can use plain text (that is, pure string substitution), and more advanced ones can use template languages such as velocity, which is more powerful.

If you have a friend who can't write a code generator, you can first build a sample code of Example, and then copy, paste, modify the entity name, and so on. It is estimated that a basic module code can be produced in 30 seconds. But if you give it to the code generator, it's faster.

For example, the author has just developed a new project, there is no time to develop a code generator, write some examples, so that if you write a new module can directly copy a change, especially the tree to save a lot of time, click showcase to view.

Code generator is not *, if you have done an Internet project, there are not as many additions, deletions, changes and queries as enterprise applications, so at this time, it doesn't make any sense to simply generate a code generator for CRUD.

At this point, we have introduced the template, the use of the template can improve the speed of development, but the template is not *, only found that some code is composed of: change and immutability, then we can turn the immutable into a template. the changed part is filled in by placeholders (that is, variables).

Java application development 4 on how to speed up the development of the project to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report