Configuration of Bean for 6.Spring series 3

Source: Internet
Author: User

One, two ways to configure beans using the XML configuration Bean

1. Introduction of external Properties file in IOC container

In an IOC container, when a bean is configured, it is sometimes necessary to introduce information about the system deployment in the bean's configuration (e.g., file path, data source configuration information, etc.), and these deployment details actually need to be separated from the bean configuration, and Spring provides a The Beanfactory Propertyplaceholderconfigurer processor, which allows partial configuration of the bean to be transferred to the properties file, can be used in the IOC container as a variable of ${var}. Propertyplaceholderconfigurer dependent files are loaded with attributes, and using these attributes to replace variables, Spring also allows the use of ${propname} in the property file to implement mutual references between attributes.

Note: With spring iterations, the release of the 2.5 version introduces an external properties file that simplifies the introduction of external properties files through the <context:property-placeholder> element:

First, we introduce the C3P0 and MySQL driver jar packages:

C3p0-0.9.1.2.jar, Mysql-connector-java-5.1.7-bin.jar

Next, create the Db.properties configuration file, which is configured with MySQL connection information:

jdbc.user=xxxjdbc.password=123456jdbc.driverclass=com.mysql.jdbc.Driverjdbc.jdbcUrl =jdbc:mysql://xxx:3306/spring

Next, introduce the external properties file in the IOC container and configure the database connection information:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd ">        <!--introducing external resource files -    <Context:property-placeholder Location= "Classpath:db.properties"/>        <!--Configure database connection information using the C3P0 connection pool -    <BeanID= "DataSource"class= "Com.mchange.v2.c3p0.ComboPooledDataSource">        < Propertyname= "User"value= "${jdbc.user}"></ Property>        < Propertyname= "Password"value= "${jdbc.password}"></ Property>        < Propertyname= "Jdbcurl"value= "${jdbc.jdbcurl}"></ Property>        < Propertyname= "Driverclass"value= "${jdbc.driverclass}"></ Property>    </Bean></Beans>

Finally, start the test program:

 Public classMain {@SuppressWarnings ("Resource")     Public Static voidMain (string[] args)throwsSQLException {applicationcontext ctx=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); //test whether the database can be connectedDataSource DataSource = (DataSource) ctx.getbean ("DataSource");        System.out.println (Datasource.getconnection ()); //post-run output: [email protected]//represents a successful connection    }}

The life cycle of a bean in a 2..IOC container

Configuration of Bean for 6.Spring series 3

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.