Spring MVC reads XML file database configuration parameters __ Database

Source: Internet
Author: User
Tags sql server driver

This article mainly describes how to use the property injection and constructor injection implementation of our project to be used in the database parameters to the XML file, to facilitate deployment.

Spring MVC 4.2.6 Project
SQL Server 2008 Database

This article describes the main use of applicationcontext and its implementation class implementation. The main use is classpathxmlapplicationcontext.
Classpathxmlapplicationcontext: Looks for the specified XML configuration file from the Classpath classpath, finds and loads
Complete the instantiation of ApplicationContext. For example:

Mount a single configuration file instantiate the ApplicationContext container
applicationcontext cxt = new Classpathxmlapplicationcontext
(" Applicationcontext.xml ");
Loading multiple configuration files Instantiate ApplicationContext container
string[] configs = {"Bean1.xml", "Bean2.xml", "Bean3.xml"};
ApplicationContext cxt = new Classpathxmlapplicationcontext (configs);

The following are the specific steps:
First, attribute injection
Property injection is the injection of a bean's property value or dependent object through the SetAttribute method. Property injection uses the element, using the Name property to specify the Bean's property name, the Value property, or the child node to specify the property value.

1. Create a Bean class Dbparaproperty

Package com;
    public class Dbparaproperty {//JDBC SQL Server driver class String sqlserverdriverclassname;
    SQL Server connection address String Sqlserverurl;
    SQL Server user name String sqlserverusername;

    SQL Server password String sqlserverpassword;
    Public String Getsqlserverdriverclassname () {return this.sqlserverdriverclassname; } public void Setsqlserverdriverclassname (String sqlserverdriverclassname) {This.sqlserverdriverclassname = s
    Qlserverdriverclassname;
    Public String Getsqlserverurl () {return this.sqlserverurl;
    } public void Setsqlserverurl (String sqlserverurl) {this.sqlserverurl = Sqlserverurl;
    Public String Getsqlserverusername () {return this.sqlserverusername;
    } public void Setsqlserverusername (String sqlserverusername) {this.sqlserverusername = Sqlserverusername;
    Public String Getsqlserverpassword () {return this.sqlserverpassword; } public void Setsqlserverpassword (String sqlserverpassword) {This.sqlserverpassword = Sqlserverpassword;
 }
}

2. Create an XML file

The contents of the document are as follows

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns=
"Http://www.springframework.org/schema/beans"
       xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemalocation= "http:// Www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ">
    < The bean id= "dbparaproperty" class= com. Dbparaproperty ">
        <property name=" Sqlserverdriverclassname "value=" Com.microsoft.sqlserver.jdbc.SQLServerDriver "></property>
        <property name=" Sqlserverurl "value=" Jdbc:sqlserver://127.0.0.1:1433;databasename=test; " ></property>
        <property name= "Sqlserverusername" value= "Sadbparaproperty" ></property>
        <property name= "Sqlserverpassword" value= "admin123" ></property>
    </bean>
</ Beans>

3. Use in Controller

package test; Import com.
Dbparaconstructor; Import com.
Dbparaproperty;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;

Import Org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping ("/test2") public class Test2 {@RequestMapping ("/test") @ResponseBody public objec T Test2 () {//If the XML file is underneath SRC, write the filename directly applicationcontext cpxac = new Classpathxmlapplicationcontext ("Dbpa
        Raproperty.xml ");
        Gets the object based on the identity of the bean node, id dbparaproperty dbparaproperty = (dbparaproperty) cpxac.getbean ("Dbparaproperty");

        System.out.println (Dbparaproperty.getsqlserverusername ());
    return Dbparaproperty.getsqlserverusername (); }
}

Second, the constructor injection
By constructing a method to inject a bean's property value or dependent object, it guarantees that the bean instance can be used after it is instantiated. Constructor injection declares a property in an element.

The steps are as follows:
1. Create Dbparaconstructor class

package com;

public class Dbparaconstructor {
    //JDBC SQL Server Driver class public
    String sqlserverdriverclassname;
    SQL Server connection address public
    String Sqlserverurl;
    SQL Server user name public
    String Sqlserverusername;
    SQL Server password public
    String Sqlserverpassword;

    Public Dbparaconstructor () {} public

    Dbparaconstructor (String sqlserverdriverclassname,string Sqlserverurl, String sqlserverusername,string sqlserverpassword) {
        this.sqlserverdriverclassname = sqlserverdriverclassname;
        This.sqlserverurl = Sqlserverurl;
        This.sqlserverusername = Sqlserverusername;
        This.sqlserverpassword = Sqlserverpassword;
    }

2, under the SRC folder under test to create an XML file.

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns=
"Http://www.springframework.org/schema/beans"
       xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemalocation= "http:// Www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ">
    < The bean id= "dbparaconstructor" class= com. Dbparaconstructor ">
        <constructor-arg name=" Sqlserverdriverclassname "value=" Com.microsoft.sqlserver.jdbc.SQLServerDriver "></constructor-arg>
        <constructor-arg name=" Sqlserverurl "value=" jdbc:sqlserver://127.0.0.1:1433;databasename=test; " ></constructor-arg>
        <constructor-arg name= "Sqlserverusername" value= "Sadbparaconstructor" > </constructor-arg>
        <constructor-arg name= "Sqlserverpassword" value= "admin456" ></ constructor-arg>
    </bean>
</beans>

3. Use in Controller

Package test; Import com.
Dbparaconstructor; Import com.
Dbparaproperty;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;

Import Org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping ("/test2") public class Test2 {@RequestMapping ("/test") @ResponseBody public objec
        T Test2 () {ApplicationContext cpxac = new Classpathxmlapplicationcontext ("Dbparaproperty.xml");
        Dbparaproperty Dbparaproperty = (dbparaproperty) cpxac.getbean ("Dbparaproperty");

        System.out.println (Dbparaproperty.getsqlserverusername ());
        ApplicationContext acc = new Classpathxmlapplicationcontext ("/test/dbparaconstructor.xml");
        Dbparaconstructor dbparaconstructor = (dbparaconstructor) acc.getbean ("Dbparaconstructor"); System.out.println (dbparaconstrUctor.sqlserverusername);
    return Dbparaproperty.getsqlserverusername () + "* * *" +dbparaconstructor.sqlserverusername;
 }
}

The project directory is as follows:

On that path, Java compiles the Java file into a. class file in the classes directory, which is also the root of the project's Java code run. So when you put the XML file under SRC, you can directly write the file name can be found, but if you put it under the other directory, to write the path, such as:/test/xxx.xml.

Self-Study is not easy, I hope this article is helpful to you.

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.