Spring Configuration Detailed

Source: Internet
Author: User
Tags print format log4j

1. Preface

The background of the company's old project is based on the spring framework, which also uses the Log4j.jar and other open source rack packages. In the new project, the spring and hibernate frameworks are used, using the Hibernate framework for persistence, simplifying SQL operations, and more. Hibernate profiles can be in two formats, one is hibernate.properties and the other is hibernate.cfg.xml. The latter is slightly more convenient, and when you add HBM mapping files, you can add them directly to the hibernate.cfg.xml, without having to include them in the initialization code like hibernate.properties. The hibernate.cfg.xml format is used in our new project. However, in this article will not be described in detail, the subsequent opportunity to fill up.

In the company project, the middleware mainly has tomcat,webshpere,weblogic. The following will be a preliminary introduction to the basic configuration of spring in the project, the configuration of log4j, and the configuration of the relevant parameters of the middleware.

2.spring Configuration -- with old GIS Project for example

The configuration in GISV13 involves the configuration of SPRINGMVC,IOC,AOP, quartz. The implementation of the configuration is implemented cooperatively through annotation configuration and XML configuration. Here, I will follow the spring configuration process, the other several aspects of the configuration of the fusion, for a comprehensive analysis.

2.1SpringMVC the configuration

2.1.1.web.xml the configuration

In a Web program, when the middleware starts, the middleware reads the configuration from Web. XML first. In Web. XML, you can configure listeners, filters, servlet mappings, and so on. In the spring framework, we mainly need to configure the path of the spring container configuration file that is read when the container is initialized and the dispatcher Dispatcherservlet in Springmvc.

In GISV13 's web. XML, we defined the following:

Initgisconfigservlet defines when a container starts, it first runs this method. This part of Servletname is then the MVC that defines the springmvc of the dispatcher and the path to the load configuration file for that servlet. Multiple such servlets can be configured in Web. Xml. The configuration of this XML in this section refers to the dispatcher that touches springmvc when the suffix name is. htm. The configuration for this servlet is under the/web-inf/classes/applicationcontext-servlet.xml address. The servlet's configuration file has its default address, which is shown here.

2.1.2.servlet the configuration file

First, you need to define the reference space for the SPRINGMVC and the configuration format in the configuration file.

2.1.2.1 Configuration Controller

To support the annotation format, the following configuration is added:

This configuration adds the annotated code to the configuration file when it is loaded by the container and completes the MVC configuration. In GISV13, @controller is used, @Service to define the control layer and the implementation layer in DAO, and the association between URL and Controller is carried out by @requestmapping.

2.1.2.2 Configuration Model and the View

The data returned by the controller in SPRINGMVC is wrapped in the Modelandview class. This class contains the specific data that is returned and the URL that the returned data points to.

The configuration associated with this in GISV13 is:

In this configuration, a prefix is added to the URL in Modelandview by default, and a file format is added to the file name pointed to by the URL, and the returned URL is converted to the/home/url/file.jsp style.

Also in this configuration is the view and the visual plot translator: The view is Org.springframework.web.servlet.view.JstlView, and the visual illustration translator is: Org.springframework.web.servlet.view.InternalR Esourceviewresolver.

In fact, internalresourceviewresolver default view is Jstlview. It is not configured here or can be.

Depending on the view and purpose, there are many different types of visual illustrations. Support for views such as Pdf,xml and upload and download files is supported in SPRINGMVC.

2.2.IOC the configuration

In GISV13, the IOC-related configuration is also done together with annotations and XML. The configuration address associated with the container is already in Web. XML and will be installed when the container is started. Here we explain the use of the IOC ORM.

2.2.1. defining jdbc.properties files

There are two main items here:

That is, the database driver and the database address.

2.2.2 Configure database Connections

For different databases, the spring metabase references different classes, and the GISV13 supports Oracle, so the class referenced here is Basicdatasource. The specific configuration is as follows:

This configuration instantiates the Basicdatasource class when the container starts, and each object in the property is configured for the object when it is initialized, which contains the drive type, the connection address, the maximum number of connections, and so on. The class capacity in the previously configured Jdbc.properties, which can be used directly here.

2.2.3 . Working with Databases

In system code, the instantiated object is injected into the object corresponding to the annotation when the container is initialized, through @autowired.

You can inject a JDBC object that is automatically instantiated with the database configuration in spring into the annotation object. We generally use the JdbcTemplate class, this class will be the database operation of the shallow packaging, and so on, and so on, and stored procedure call, avoid the code repeated use of fault-tolerant mechanism and other duplicate code.

2.3.AOP the configuration

AOP is one of the highlights of spring, and it involves a lot of concepts, such as pointcuts, enhancements, cross sections, and so on. This configuration is often used in features such as use validation. This feature was also used in GISV13, primarily for database transaction management. In spring, the implementation of AOP can be implemented by general XML configuration, ASPECTJ configuration, and annotation configuration. Combined with the system, I will aspectj the configuration to roughly explain.

2.3.1. Configuring the Things Management class

The database that the transaction management is associated with is primarily defined in this class. The configuration of the DataSource is described in the previous chapter.

2.3.2 configure enhanced class capacity

In this class, an enhanced class tolerance is defined, which is the content that is woven into the destination object (target) code. The attributes defines the action that the enhancement corresponds to. For example, when the method name starts with find, the type of transaction management is read-only. The properties of transaction management can also set the propagation characteristics, as well as the isolation of things. If not set, the retransmission feature here is::P ropgation_required. This configuration item means that when I invoke the service layer's method to open a transaction (specifically call that layer of the method to start creating a transaction, depending on the configuration of the AOP), then when invoking the other methods inside the service layer, Creates a new transaction if the current method produces a transaction that is the result of the current method. This work is made by spring to help us do it.

2.3.3. defining weaving points and facets

First define the weaving point, which means all methods in the class that are under the cn.com.* package to end with manager in the DAO package.

It then defines the tangent plane, which first associates the weaving point, and then the enhanced class. That is, the enhanced class capacity is added to the method that satisfies the weaving condition, which is described in 4.2.

2.4.SpringQuartz the configuration

SPRING3.0 's own task, which can be seen as a lightweight quartz, is much simpler to use than quartz, and is able to perform timed triggering of tasks.

2.4.1 Job class Configuration

Defines a job class. Value is the reference address of the class that needs to be triggered.

2.4.2 Trigger Configuration

The triggering conditions are divided into two types, simple conditions and complex conditions.

2.4.2.1 Simple Trigger Configuration

You can configure trigger delay time and trigger interval time. Where ref is the configuration (bean) name of the class that needs to be triggered.

2.4.2.2 Complex Trigger Configuration

A complex configuration can be configured with a specific trigger time and so on.

3.log4j the configuration

The common open source project of support log is more, mainly have: Log4j,jul,jcl,slf4j,logback and so on. However, Log4j is dominated by its long history, extensive application, support for multiple languages and platforms, and powerful functions.

3.1 Dependent Packages

Generally need to Log4j-1.2.13.jar and Commons-logging-1.1.1.jar two rack package. However, Log4j-1.2.13.jar is a must, Commons-logging-1.1.1.jar is not necessary.

The purpose of Commons-loggin is to provide a unified interface for "All Java log implementations", its own log function is usually weak (only a simple simplelog), so it is generally not used alone. However, almost all projects that use log4j also use Commons-loggin at the same time. This is done primarily to avoid being too tightly bound to your project with log4j. In addition, "use commons-logging and log4j simultaneously", can provide a unified log interface, simple operation, while avoiding the project and a log implementation system tightly coupled, so you can automatically select the appropriate log implementation system.

3.2 Web in the configuration

In Web. XML, you need to configure the configuration file address of the log4j and listen for startup items.

3.3 log4j.properties Configuration

LOG4J can configure the output level of the log, output format, log path log name, time format and so on.

3.3.1 Log Level

Log level:

Error: The system's continued operation is not affected, although a fault event occurs.

WARN: A scenario in which a potential error occurs.

INFO: The message highlights the application's running process at a coarse-grained level.

Debug: Fine-grained information events are very helpful for debugging applications.

3.3.2 Appender Configuration

which
Org.apache.log4j.ConsoleAppender (console).
Org.apache.log4j.FileAppender (file).
Org.apache.log4j.DailyRollingFileAppender (generates a log file every day).
Org.apache.log4j.RollingFileAppender (creates a new file when the file size reaches the specified size).
Org.apache.log4j.WriterAppender (sends the log information in stream format to any specified place).

3.3.3 Threshold Configuration

Layout:
Org.apache.log4j.HTMLLayout (Layout in HTML table Form).
Org.apache.log4j.PatternLayout (flexibility to specify layout mode).
Org.apache.log4j.SimpleLayout (contains the level of log information and the information string).
Org.apache.log4j.TTCCLayout (contains information about the time, thread, category, and so on) that the log produces.

3.3.4 Print Format

LOG4J uses a print format that resembles the printf function in C language to format the log information

%M the message specified in the output code

%p output priority, i.e. Debug,info,warn,error,fatal

%r output the number of milliseconds that the log information is consumed from the application boot to output

%c output belongs to the class, which is usually the full name of the class

%t output The name of the thread that generated the log event

%n output a carriage return line break, Windows platform is "\ r \ n", Unix platform is "\ n", that is, one-hop messages occupy a row

%d date or time of the output log time, followed by a pair of curly braces for custom formatting

%c output belongs to the class, which is usually the full name of the class

%l Accurate to Line

%x Output Alignment

3.3.5 path address of the output

In Web. XML, the project name is defined:

The input log path is defined in Log4j.properties:

3.4 used in code

First quote:

Then instantiate:

Last use:

4. Middleware Configuration -- take Tomcat for example

Our general configuration in Tomcat is: port, number of connections, number of threads, encoding format, file compression, memory size, etc.

Configuration of the 4.1 server.xml

4.2 Memory Settings

You can increase the number of Tomcat memory in the Catalina.bat file to avoid space heap problems.

Spring Configuration Detailed

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.