{"id":436,"date":"2019-01-06T19:51:45","date_gmt":"2019-01-06T11:51:45","guid":{"rendered":"https:\/\/kaispace.cn\/?p=436"},"modified":"2019-01-06T19:51:45","modified_gmt":"2019-01-06T11:51:45","slug":"spring%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0%e4%ba%8c%e4%b9%8b%e9%ab%98%e7%ba%a7%e8%a3%85%e9%85%8d","status":"publish","type":"post","link":"https:\/\/blog.kaispace.cn\/?p=436","title":{"rendered":"Spring\u5b66\u4e60\u7b14\u8bb0(\u4e8c)\u4e4b\u9ad8\u7ea7\u88c5\u914d"},"content":{"rendered":"<h1>\u9ad8\u7ea7\u88c5\u914d<\/h1>\n<ul>\n<li>Spring profile<\/li>\n<\/ul>\n<p>\u524d\u63d0\uff1a<strong>profile\u88ab\u6fc0\u6d3b(active)<\/strong><\/p>\n<p>Spring3.1\u53ea\u80fd\u5728\u7c7b\u4e0a\u7528\uff0c3.2\u53ef\u4ee5\u5728Config\u7c7b\u4e2d\u5efa\u4e0d\u540c\u73af\u5883\u4e0b\u7684bean<\/p>\n<h2>\u4e00\u3001\u8bbe\u7f6eprofile<\/h2>\n<h3>Java\u65b9\u6cd5<\/h3>\n<pre><code class=\"language-java line-numbers\">@Configuration\n@Profile(\"dev\")\npublic class DevelopmentProfileConfig{\n    return new EmbeddedDataBaseBuilder()\n    .setType(EmbeddedDataBase.H2)\n    .addScript(\"classpath:schema.sql\")\n    .addScript(\"classpath:test-data.sql\")\n    .build();\n}\n<\/code><\/pre>\n<h3>XML\u65b9\u6cd5<\/h3>\n<pre data-language=XML><code class=\"language-markup line-numbers\">&lt;beans xnlns=\"http:\/\/ww.springframework.org\/schema\/beans\"\n      xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n      xsi:schemaLocation=\"http:\/\/springframework.org\/schema\/beans\n      http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd\n      http:\/\/www.springframework.org\/schema\/context\"\n      profile=\"dev\"&gt;\n\n      &lt;beans profile=\"dev\"&gt;\n        &lt;jdbc:embedded-database id=\"dataSource\"&gt;\n          &lt;jdbc:script location=\"classpath:schema.sql\" \/&gt;\n          &lt;jdbc:script location=\"classpath:test-data.sql\" \/&gt;\n          &lt;\/jdbc:embedded-database&gt;\n      &lt;\/beans&gt;\n\n      &lt;beans profile=\"qa\"&gt;\n      &lt;!--QA\u7684DataSource--&gt;\n      &lt;\/beans&gt;\n\n      &lt;beans profile=\"prod\"&gt;\n      &lt;!--\u751f\u4ea7\u73af\u5883\u7684DataSource--&gt;\n      &lt;\/beans&gt;\n\n&lt;\/beans&gt;\n<\/code><\/pre>\n<h2>\u4e8c\u3001\u6fc0\u6d3bprofile<\/h2>\n<p>\u4f8b\u5982\u5728web.xml\u4e2d<\/p>\n<pre data-language=XML><code class=\"language-markup line-numbers\">&lt;context-param&gt;\n    &lt;param-name&gt;spring.profiles.default&lt;\/param-name&gt;\n    &lt;param-value&gt;dev&lt;\/param-value&gt;\n&lt;\/context-param&gt;\n<\/code><\/pre>\n<pre><code class=\"language-Java line-numbers\">@ActiveProfiles(\"dev\")\n<\/code><\/pre>\n<h2>\u4e09\u3001\u6761\u4ef6\u521b\u5efaBean(Spring4\u65b0\u7279\u6027)<\/h2>\n<pre><code class=\"language-java line-numbers\">@Conditional(xxxCondition.class)\n\/\/\u8fd9\u4e2a\u7c7b\u9700\u8981\u5b9e\u73b0Condition\u63a5\u53e3\uff0c\u8fd4\u56detrue\u5219\u521b\u5efa\uff0c\u5426\u5219\u4e0d\u521b\u5efa\n<\/code><\/pre>\n<h4>\u6761\u4ef6\u9650\u5b9a<\/h4>\n<ul>\n<li>@Primary<\/li>\n<li>@Qualifer(xxx)<\/li>\n<li>\u81ea\u5b9a\u4e49\u6ce8\u89e3<\/li>\n<\/ul>\n<h2>\u56db\u3001Bean\u7684\u4f5c\u7528\u57df<\/h2>\n<ol>\n<li>\u5355\u4f8b(singleton)<\/li>\n<li>\u539f\u578b(prototy)<\/li>\n<li>\u4f1a\u8bdd(session)<\/li>\n<li>\u8bf7\u6c42(request)<\/li>\n<\/ol>\n<pre><code class=\"language-java line-numbers\">JAVA\u8bbe\u7f6e\u65b9\u6cd5@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)\u6216\u8005@Scope(\"prototype\")\n<\/code><\/pre>\n<pre><code class=\"language-XML line-numbers\">XML\u662fscope\u5c5e\u6027,\u4ee3\u7406\u662f&lt;aop:scoped-proxy&gt;,\u82e5\u4ee3\u7406\u63a5\u53e3\u8fd8\u8981\u8bbe\u7f6eproxy-target-class=\"false\"\n\u4ee5\u53ca\u4e5f\u8981\u6dfb\u52a0AOP\u7684\u547d\u540d\u7a7a\u95f4\n<\/code><\/pre>\n<h4>\u4f1a\u8bdd\u5c42(Session)\u4f5c\u7528\u57df<\/h4>\n<pre><code class=\"language-java line-numbers\">@Scope(\n    value=WebApplicationContext.SCOPE_SESSION,\n    proxyMode=ScopeProxyMode.INTERFACES)\n<\/code><\/pre>\n<p>\u82e5\u5bf9\u7c7b\u4ee3\u7406\u5219\u9700\u8981\u6539\u4e3aproxyMode=ScopeProxyMode.TARGET_CLASS,\u4f46\u6700\u597d\u8fd8\u662f\u63a5\u53e3<br \/>\n\u8fd9\u4e2a\u505a\u6cd5\u662f\u4e0d\u4f20\u76f4\u63a5\u7684\u5f15\u7528\u7ed9\u5176\u4ed6Bean\uff0c\u56e0\u4e3a\u4f5c\u7528\u57df\u53ef\u80fd\u4e0d\u540c\uff0c\u800c\u662f\u4f20\u4e00\u4e2a\u4ee3\u7406\uff0c\u5f53\u90a3\u4e2aBean\u8c03\u7528\u4f9d\u8d56\u7684Bean\u7684\u65b9\u6cd5\u65f6\uff0c\u4f1a\u7531\u4ee3\u7406\u8f6c\u5230\u5b9e\u9645\u53bb\u6267\u884c<\/p>\n<h2>\u4e94\u3001\u8fd0\u884c\u65f6\u6ce8\u5165<\/h2>\n<p>@PropertySource\u5c5e\u6027\u6e90<br \/>\n\u540e\u518denv.getProperty()<\/p>\n<h4>\u5360\u4f4d\u7b26<\/h4>\n<pre><code class=\"language-java line-numbers\">${...}\u4e3a\u5360\u4f4d\u7b26,\u6709\u70b9\u50cfhtml\n@Value(${disc.title}) String title;\nthis.t = title;\n<\/code><\/pre>\n<h4>SpEL \u5f88\u5f3a,\u540e\u9762\u8981\u518d\u770b<\/h4>\n<pre><code class=\"language-Java line-numbers\">\u683c\u5f0f#{...}\n\u5982#{T(System).currentTimeMillis()}\nT()\u8bf4\u660e\u5c06\u5176\u89c6\u4e3ajava.lang.System\u4e2d\u7684\u5bf9\u5e94\u7c7b\u578b\nxxx?.\u7684?\u662f\u4e3a\u4e86\u8fdb\u884c\u65b9\u6cd5\u5904\u7406\u65f6\u907f\u514d\u7a7a\u6307\u9488\u5f02\u5e38\n.?['param']\u67e5\u8be2\u8fd0\u7b97\u7b26,\u8fd4\u56de\u7b26\u5408\u67e5\u8be2\u6761\u4ef6\u7684\u96c6\u5408\n.^[]\u67e5\u8be2\u8fd0\u7b97\u7b26\uff0c\u8fd4\u56de\u7b2c\u4e00\u4e2a\u6ee1\u8db3\u6761\u4ef6\n.$[]\u67e5\u8be2\u8fd0\u7b97\u7b26\uff0c\u8fd4\u56de\u6700\u540e\u4e00\u4e2a\u6ee1\u8db3\u6761\u4ef6\n.![]\u6295\u5f71\u8fd0\u7b97\u7b26\uff0c\u53bb\u9664\u96c6\u5408\u4e2d\u6bcf\u4e2a\u6210\u5458\u7684\u5c5e\u6027\u6295\u5f71\u5230\u4e00\u4e2a\u65b0\u7684\u96c6\u5408\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9ad8\u7ea7\u88c5\u914d Spring profile \u524d\u63d0\uff1aprofile\u88ab\u6fc0\u6d3b(active) Spring3.1\u53ea\u80fd\u5728\u7c7b&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,6],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=\/wp\/v2\/posts\/436"}],"collection":[{"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=436"}],"version-history":[{"count":0,"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=\/wp\/v2\/posts\/436\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=436"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=436"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=436"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}