{"id":2788,"date":"2018-05-13T16:41:00","date_gmt":"2018-05-13T07:41:00","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=2788"},"modified":"2023-05-27T23:11:33","modified_gmt":"2023-05-27T14:11:33","slug":"%e3%80%90spring-framework%e3%80%91aop%e3%82%92%e5%ae%9f%e8%a3%85%e3%81%97%e3%81%a6%e3%81%bf%e3%82%8b%e3%80%82","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=2788","title":{"rendered":"\u3010Spring Framework\u3011AOP(\u30a2\u30b9\u30da\u30af\u30c8\u6307\u5411\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0)\u3092\u5b9f\u88c5\u3057\u3066\u307f\u308b\u3002\uff08\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u304b\u3089\u5b9f\u88c5\uff09"},"content":{"rendered":"<h2>AOP(\u30a2\u30b9\u30da\u30af\u30c8\u6307\u5411\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0)\u3068\u306f\uff1f<\/h2>\n<p>\u30af\u30e9\u30b9\u9593\u306b\u5171\u901a\u3059\u308b\u51e6\u7406\u3092\u3001\u5916\u3060\u3057\u3059\u308b\u5b9f\u88c5\u306e\u3053\u3068\u3067\u3059\u3002<\/p>\n<h2>\u5b9f\u969b\u306b\u5b9f\u88c5\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/h2>\n<p>\u5b9f\u88c5\u30a4\u30e1\u30fc\u30b8\u3092\u30af\u30e9\u30b9\u56f3\u306b\u3059\u308b\u3068\u4e0b\u8a18\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-2817\" src=\"http:\/\/www.code-magagine.com\/wp-content\/uploads\/2018\/05\/AOP\u30b5\u30f3\u30d7\u30eb.png\" alt=\"\" width=\"821\" height=\"336\" srcset=\"http:\/\/www.code-magagine.com\/wp-content\/uploads\/2018\/05\/AOP\u30b5\u30f3\u30d7\u30eb.png 821w, http:\/\/www.code-magagine.com\/wp-content\/uploads\/2018\/05\/AOP\u30b5\u30f3\u30d7\u30eb-300x123.png 300w, http:\/\/www.code-magagine.com\/wp-content\/uploads\/2018\/05\/AOP\u30b5\u30f3\u30d7\u30eb-768x314.png 768w\" sizes=\"(max-width: 821px) 100vw, 821px\" \/><\/p>\n<h3>Main.java(\u5b9f\u884c\u3059\u308b\u30e1\u30a4\u30f3\u30d7\u30ed\u30b0\u30e9\u30e0\uff09<\/h3>\n<pre class=\"lang:default decode:true\">package com.springapp;\r\n\r\nimport org.springframework.context.ApplicationContext;\r\nimport org.springframework.context.support.ClassPathXmlApplicationContext;\r\n\r\npublic class Main {\r\n\r\n    public static void main(String[] args) {\r\n\r\n    \tApplicationContext app = new ClassPathXmlApplicationContext(\"bean.xml\");\r\n        \r\n        \/\/AOP\u9069\u7528\u524d\r\n        TestBean bean1 = (TestBean) app.getBean(\"TestBean\");\r\n        bean1.HelloWorld();\r\n\r\n        System.out.println(\"--------------------\");\r\n\r\n        \/\/AOP\u9069\u7528\u5f8c\r\n        TestBean bean2 = (TestBean) app.getBean(\"proxyBean\");\r\n        bean2.HelloWorld();\r\n    }\r\n\r\n}\r\n<\/pre>\n<h3>TestBean.java(AOP\u306b\u3088\u308b\u51e6\u7406\u3092\u6ce8\u5165\u3059\u308bBean\uff09<\/h3>\n<pre class=\"lang:default decode:true \">package com.springapp;\r\n\r\npublic class TestBean {\r\n    private String helloWorld;\r\n\r\n    public TestBean() {\r\n        super();\r\n        this.helloWorld = \"Hello World!\";\r\n    }\r\n\tpublic void HelloWorld() {\r\n        System.out.println(this.helloWorld);\r\n    }\r\n}\r\n<\/pre>\n<h3>AOPClass.java(\u6ce8\u5165\u3059\u308b\u51e6\u7406\u3092\u5b9a\u7fa9\u3057\u305f\u30af\u30e9\u30b9\u3002\u3053\u306e\u30af\u30e9\u30b9\u304c\u4eca\u56de\u306e\u809d\u3067\u3059\u3002\uff09<\/h3>\n<pre class=\"lang:default decode:true\">package com.springapp;\r\n\r\nimport java.lang.reflect.Method;\r\n\r\nimport org.springframework.aop.AfterReturningAdvice;\r\nimport org.springframework.aop.MethodBeforeAdvice;\r\n\r\npublic class AOPClass implements MethodBeforeAdvice, AfterReturningAdvice {\r\n\r\n\t@Override\r\n\tpublic void before(Method method, Object[] args,Object target) throws Throwable {\r\n\t\tSystem.out.println(\"\u4e8b\u524d\u51e6\u7406\");\r\n\t}\r\n\t\r\n\t@Override\r\n\tpublic void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {\r\n\t\tSystem.out.println(\"\u4e8b\u5f8c\u51e6\u7406\");\r\n\t}\r\n\r\n}<\/pre>\n<p>\u4e0b\u8a18\u4e8c\u3064\u306e\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u3092\u5b9f\u88c5\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<table>\n<tbody>\n<tr>\n<th>\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u540d<\/th>\n<th>\u8aac\u660e<\/th>\n<\/tr>\n<tr>\n<td>MethodBeforeAdvice<\/td>\n<td>AOP\u3067\u3001\u30e1\u30bd\u30c3\u30c9\u5b9f\u884c\u524d\u306b\u51e6\u7406\u3092\u6ce8\u5165\u3059\u308b\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u3067\u3059\u3002<span style=\"color: #ff0000;\"><strong>\u300cbefore\u30e1\u30bd\u30c3\u30c9\u300d<\/strong><\/span>\u3092\u5b9f\u88c5\u3057\u307e\u3059\u3002<\/td>\n<\/tr>\n<tr>\n<td>AfterReturningAdvice<\/td>\n<td>AOP\u3067\u3001\u30e1\u30bd\u30c3\u30c9\u5b9f\u884c\u5f8c\u306b\u51e6\u7406\u3092\u6ce8\u5165\u3059\u308b\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u3067\u3059\u3002<span style=\"color: #ff0000;\"><strong>\u300cafterReturning\u30e1\u30bd\u30c3\u30c9\u300d<\/strong><\/span>\u3092\u5b9f\u88c5\u3057\u307e\u3059\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>bean.xml(AOP\u306e\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u3067\u3059\u3002\uff09<\/h3>\n<pre class=\"lang:default decode:true\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\r\n&lt;beans xmlns=\"http:\/\/www.springframework.org\/schema\/beans\"\r\n\txmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\r\n\txsi:schemaLocation=\"http:\/\/www.springframework.org\/schema\/beans http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd\"&gt;\r\n\r\n    &lt;!-- AOP\u3092\u4f7f\u308f\u306a\u3044\u5834\u5408 --&gt;\r\n    &lt;bean id=\"TestBean\" class=\"com.springapp.TestBean\" \/&gt;\r\n\r\n    &lt;!-- AOP(\u51e6\u7406\u306e\u6ce8\u5165\uff09\u3059\u308b\u5834\u5408 --&gt;\r\n    &lt;!-- AOP\u306e\u30af\u30e9\u30b9\uff08\u4e8b\u524d\u51e6\u7406\u3001\u4e8b\u5f8c\u51e6\u7406\u3092\u5b9a\u7fa9\uff09 --&gt;\r\n    &lt;bean id=\"AOPClass\" class=\"com.springapp.AOPClass\" \/&gt;\r\n    &lt;bean id=\"proxyBean\" class=\"org.springframework.aop.framework.ProxyFactoryBean\"&gt;\r\n    \t&lt;!-- \u51e6\u7406\u3092\u6ce8\u5165\u3059\u308b\u5bfe\u8c61\u3068\u306a\u308b\u30af\u30e9\u30b9 --&gt;\r\n        &lt;property name=\"target\" ref=\"TestBean\"&gt;&lt;\/property&gt;\r\n        &lt;property name=\"interceptorNames\"&gt;\r\n            &lt;list&gt;\r\n                &lt;value&gt;AOPClass&lt;\/value&gt;\r\n            &lt;\/list&gt;\r\n        &lt;\/property&gt;\r\n    &lt;\/bean&gt;\r\n\r\n&lt;\/beans&gt;\r\n<\/pre>\n<h4>ProxyFactoryBean<\/h4>\n<p>\u8a2d\u5b9a\u60c5\u5831\u306b\u3001\u300cProxyFactoryBean\u300d\u3068\u3044\u3046\u30af\u30e9\u30b9\u3092\u8a2d\u5b9a\u3057\u3066\u3044\u307e\u3059\u3002Spring\u306eAOP\u5c02\u7528\u30af\u30e9\u30b9\u3067\u3001AOP\u3092\u4f7f\u3046\u305f\u3081\u306b\u306f\u5b9a\u7fa9\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<h5><strong>target<\/strong><\/h5>\n<p>AOP\u306e\u5bfe\u8c61\u3068\u306a\u308b\u30af\u30e9\u30b9\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002\uff08\u3053\u3053\u3067\u306f\u3001\u4f5c\u6210\u3057\u305f\u300cTestBean\u300d\u3092\u6307\u5b9a\u3057\u3066\u3044\u307e\u3059\u3002\uff09<\/p>\n<h5><strong>interceptorNames<\/strong><\/h5>\n<p>AOP\u3067\u51e6\u7406\u3092\u6ce8\u5165\u3059\u308b\u30af\u30e9\u30b9\u3092\u6307\u5b9a\u3067\u304d\u307e\u3059\u3002list\u3068\u3057\u3066\u8907\u6570\u6307\u5b9a\u3059\u308b\u3053\u3068\u3082\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<h3>\u5b9f\u884c\u7d50\u679c<\/h3>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-2815\" src=\"http:\/\/www.code-magagine.com\/wp-content\/uploads\/2018\/05\/AOP_\u4e8b\u524d\u4e8b\u5f8c.png\" alt=\"\" width=\"189\" height=\"184\" \/><\/p>\n<p>AOP\u9069\u7528\u5f8c\u3067\u306f\u3001\u51e6\u7406\u304c\u6ce8\u5165\u3055\u308c\u3066\u3044\u306a\u3044\u3067\u3059\u304c\u3001AOP\u9069\u7528\u5f8c\u306f\u3001\u300c\u4e8b\u524d\u300d\u3068\u300c\u4e8b\u5f8c\u300d\u306b\u51e6\u7406\u304c\u633f\u5165\u3055\u308c\u3066\u3044\u308b\u306e\u304c\u308f\u304b\u308a\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"AOP(\u30a2\u30b9\u30da\u30af\u30c8\u6307\u5411\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0)\u3068\u306f\uff1f \u30af\u30e9\u30b9\u9593\u306b\u5171\u901a\u3059\u308b\u51e6\u7406\u3092\u3001\u5916\u3060\u3057\u3059\u308b\u5b9f\u88c5\u306e\u3053\u3068\u3067\u3059\u3002 \u5b9f\u969b\u306b\u5b9f\u88c5\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002 \u5b9f\u88c5\u30a4\u30e1\u30fc\u30b8\u3092\u30af\u30e9\u30b9\u56f3\u306b\u3059\u308b\u3068\u4e0b\u8a18\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002 Main.java(\u5b9f\u884c\u3059\u308b\u30e1\u30a4\u30f3\u30d7\u30ed [&hellip;]","protected":false},"author":1,"featured_media":2824,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[31],"tags":[],"_links":{"self":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/2788"}],"collection":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2788"}],"version-history":[{"count":12,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/2788\/revisions"}],"predecessor-version":[{"id":20573,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/2788\/revisions\/20573"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/media\/2824"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2788"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2788"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2788"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}