Method of reading XML file database configuration parameters by spring MVC

This paper mainly introduces how to put the database parameters used in our project into XML files through attribute injection and constructor injection to facilitate deployment.

spring mvc 4.2. 6 project

SQL Server 2008 database

This article mainly uses ApplicationContext and its implementation class. Classpathxmlapplicationcontext is mainly used.

Classpathxmlapplicationcontext: find and load the specified XML configuration file from the classpath classpath

Complete the instantiation of ApplicationContext. For example:

Here are the specific steps:

1、 Attribute injection

Attribute injection is to inject the attribute value or dependent object of a bean through the setAttribute method. Attribute injection uses the element, the name attribute specifies the attribute name of the bean, and the value attribute or child node specifies the attribute value.

1. Create a bean class dbparaproperty

2. Create an XML file

The contents of the document are as follows

3. Use in controller

2、 Constructor injection

Inject the attribute value or dependent object of the bean through the construction method, which ensures that the bean instance can be used after instantiation. Constructor injection declares attributes in elements.

The steps are as follows:

1. Create dbparaconstructor class

2. Create an XML file in the test folder under Src.

3. Use in controller

The project directory is as follows:

About that path, Java will compile the java file into The class file is placed in the classes directory, which is also the root directory where the Java code of the project runs. So when you put the XML file under SRC, you can write the file name directly to find it, but if you put it under other directories, write the path, for example: / test / xxx xml。

The above spring MVC method to read XML file database configuration parameters is all the content shared by Xiaobian. I hope it can give you a reference and support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>