{"id":11,"date":"2013-03-20T19:16:52","date_gmt":"2013-03-20T19:16:52","guid":{"rendered":"http:\/\/www.yeetrack.com\/post\/2013-03-20\/Spring%E9%85%8D%E7%BD%AEPropertyOverrideConfigurer"},"modified":"2013-09-27T22:36:46","modified_gmt":"2013-09-27T14:36:46","slug":"spring%e9%85%8d%e7%bd%aepropertyoverrideconfigurer","status":"publish","type":"post","link":"https:\/\/www.yeetrack.com\/?p=11","title":{"rendered":"Spring\u914d\u7f6ePropertyOverrideConfigurer"},"content":{"rendered":"<p>Spring\u7684\u914d\u7f6e\u6587\u4ef6\u4e3aapplicationContext.xml\uff0c\u4f46\u662f\u6709\u4e9b\u5e38\u91cf\u53c2\u6570\u4e00\u822c\u4e0d\u914d\u7f6e\u5728applicationContext.xml\u4e2d\uff0c\u800c\u662f\u653e\u5728\u5355\u72ec\u7684properties\u6587\u4ef6\u4e2d\uff0c\u5982\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f\u3002<br \/> applicationContext.xml\u914d\u7f6e\u5916\u90e8\u7684properties\u6587\u4ef6\u65f6\uff0c\u9700\u8981PropertyOverrideConfigurer\u5bf9\u8c61\uff0c\u6307\u5b9aproperties\u6587\u4ef6\u7684\u4f4d\u7f6e\uff0c\u7136\u540e\u6307\u5b9a\u5c5e\u6027\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u4e0b\u9762\u662fmysql\u7684\u914d\u7f6e\u6587\u4ef6\uff0cjdbc.properties:<\/p>\n<pre config=\"brush:html;toolbar:false;\">\n#\u914d\u7f6e\u6570\u636e\u9a71\u52a8  \n    jdbc.driverClassName=com.mysql.jdbc.Driver\n    jdbc.url=jdbc:mysql:\/\/127.0.0.1:3306\/test?characterEncoding=UTF-8\n    jdbc.username=root\n    jdbc.password=toor  \n<\/pre>\n<p>\u4e0b\u9762\u662f\u5728applicationContext.xml\u4e2d\u914d\u7f6e\u8be5properties\u6587\u4ef6\uff1a<\/p>\n<pre config=\"brush:html;toolbar:false;\">\n    &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\n    &lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\n    xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xmlns:p=&quot;http:\/\/www.springframework.org\/schema\/p&quot;\n    xmlns:context=&quot;http:\/\/www.springframework.org\/schema\/context&quot;\n    xmlns:aop=&quot;http:\/\/www.springframework.org\/schema\/aop&quot;\n    xsi:schemaLocation=&quot;     \n          http:\/\/www.springframework.org\/schema\/beans     \n          http:\/\/www.springframework.org\/schema\/beans\/spring-beans-3.0.xsd     \n          http:\/\/www.springframework.org\/schema\/context     \n          http:\/\/www.springframework.org\/schema\/context\/spring-context-3.0.xsd \n          http:\/\/www.springframework.org\/schema\/aop     \n          http:\/\/www.springframework.org\/schema\/aop\/spring-aop-3.0.xsd&quot;\n    default-autowire=&quot;byName&quot;&gt;\n\n    &lt;!-- \u914d\u7f6e\u6570\u636e\u5e93\u4fe1\u606f\u6e90 --&gt;\n    &lt;bean id=&quot;dataSource&quot; class=&quot;org.apache.commons.dbcp.BasicDataSource&quot; destroy-method=&quot;close&quot;&gt;\n        &lt;property name=&quot;driverClassName&quot; value=&quot;${jdbc.driverClassName}&quot;&gt;&lt;\/property&gt;\n        &lt;property name=&quot;url&quot; value=&quot;${jdbc.url}&quot;&gt;&lt;\/property&gt;\n        &lt;property name=&quot;username&quot; value=&quot;${jdbc.username}&quot;&gt;&lt;\/property&gt;\n        &lt;property name=&quot;password&quot; value=&quot;${jdbc.password}&quot;&gt;&lt;\/property&gt;\n    &lt;\/bean&gt;\n    &lt;bean id=&quot;propertyConfigurer&quot; class=&quot;org.springframework.beans.factory.config.PropertyPlaceholderConfigurer&quot;&gt;\n        &lt;property name=&quot;location&quot; value=&quot;classpath:jdbc.properties&quot;&gt;&lt;\/property&gt;\n    &lt;\/bean&gt;\n    &lt;\/beans&gt;  \n<\/pre>\n<p>\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\uff0cPropertyConfigurer\u5bf9\u8c61\u4f1a\u5bfb\u627ejdbc.properties\u6587\u4ef6\u4e2d\uff0c\u5bfb\u627e\u76f8\u5e94\u7684\u53d8\u91cf\u3002<br \/> \u4e0b\u9762\u662f\u6d4b\u8bd5\u4ee3\u7801\uff0cSpringTest.java<\/p>\n<pre config=\"brush:java;toolbar:false;\">\npackage com.yeetrack.mavenSpring;\n\n    import java.sql.Connection;\n    import java.sql.ResultSet;\n    import java.sql.SQLException;\n    import java.sql.Statement;\n\n    import javax.sql.DataSource;\n\n    import org.springframework.context.ApplicationContext;\n    import org.springframework.context.support.ClassPathXmlApplicationContext;\n\n    public class SpringTest\n    {\n\n        public static void main(String[] args)\n        {\n            \/\/\u83b7\u53d6ApplicationContext\u5bb9\u5668\n            ApplicationContext context = new ClassPathXmlApplicationContext(&quot;applicationContext.xml&quot;);\n            \/\/\u901a\u8fc7Spring\u4ee3\u7406\u7c7bProxyFactoryBean\u83b7\u53d6ServiceImpl\u5b9e\u4f8b\n            IService hello = (IService) context.getBean(&quot;service&quot;);\n            hello.sayHello(&quot;Good Day!&quot;);\n            DataSource data = (DataSource) context.getBean(&quot;dataSource&quot;);\n            Connection conn = null;\n            ResultSet result = null;\n            try\n            {\n                conn = data.getConnection();\n                Statement stat = conn.createStatement();\n                String sql = &quot;select * from user&quot;;\n                result = stat.executeQuery(sql);\n                while(result.next())\n                {\n                    System.out.print(result.getString(1)+&quot;  &quot;);\n                    System.out.print(result.getString(2)+&quot;  &quot;);\n                    System.out.println(result.getString(3));\n\n                }\n                result.close();\n                conn.close();\n            } catch (SQLException e)\n            {\n                \/\/ TODO Auto-generated catch block\n                e.printStackTrace();\n            }\n        }\n\n    }\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Spring\u7684\u914d\u7f6e\u6587\u4ef6\u4e3aapplicationContext.xml\uff0c\u4f46\u662f\u6709\u4e9b\u5e38\u91cf\u53c2\u6570\u4e00\u822c\u4e0d\u914d\u7f6e\u5728applicationContext.xml\u4e2d\uff0c\u800c\u662f\u653e\u5728\u5355\u72ec\u7684properties\u6587\u4ef6\u4e2d\uff0c\u5982\u6570\u636e\u5e93\u8fde\u63a5&#46;&#46;&#46;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[33],"tags":[8,7,5],"class_list":["post-11","post","type-post","status-publish","format-standard","hentry","category-coding","tag-java","tag-7","tag-5"],"views":3325,"_links":{"self":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/11","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11"}],"version-history":[{"count":1,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/11\/revisions"}],"predecessor-version":[{"id":187,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/11\/revisions\/187"}],"wp:attachment":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}