obydul

Spring AOP Program

Oct 14th, 2021 (edited)
419
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     ====================================
  2.  
  3.     package com.obydul.spring.aop;
  4.  
  5.     public class Camera {
  6.         public void snap() {
  7.             System.out.println("SNAP!");
  8.         }
  9.     }
  10.  
  11.     ==================================
  12.  
  13.     package com.obydul.spring.aop;
  14.  
  15.     public class Logger {
  16.         public void aboutToTakePhoto() {
  17.             System.out.println("About to take photo...");
  18.         }
  19.     }
  20.  
  21.     ===================================
  22.  
  23.     package com.obydul.spring.aop;
  24.  
  25.     import org.springframework.context.support.ClassPathXmlApplicationContext;
  26.  
  27.     public class App {
  28.  
  29.         public static void main(String[] args) {
  30.             ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("com/obydul/spring/aop/beans.xml");
  31.             Camera camera = (Camera) context.getBean("camera");
  32.             camera.snap();
  33.  
  34.             context.close();
  35.         }
  36.     }
  37.  
  38.     ================ beans.xml ===============================
  39.  
  40.     <?xml version="1.0" encoding="UTF-8"?>
  41.     <beans xmlns="http://www.springframework.org/schema/beans"
  42.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  43.     xmlns:aop="http://www.springframework.org/schema/aop"
  44.     xmlns:context="http://www.springframework.org/schema/context"
  45.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  46.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
  47.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
  48.  
  49.     <bean id="camera" class="com.obydul.spring.aop.Camera"></bean>
  50.  
  51.     <bean id="logger" class="com.obydul.spring.aop.Logger"></bean>
  52.  
  53.     <aop:config>
  54.         <aop:pointcut
  55.             expression="execution(void com.obydul.spring.aop.Camera.snap())"
  56.             id="cameraSnap" />
  57.         <aop:aspect ref="logger" id="loggeraspect">
  58.             <aop:before method="aboutToTakePhoto"
  59.                 pointcut-ref="cameraSnap" />
  60.         </aop:aspect>
  61.         </aop:config>
  62.     </beans>
  63.  
  64.     ================= pom.xml ================
  65.  
  66.     <project xmlns="http://maven.apache.org/POM/4.0.0"
  67.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  68.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  69.     <modelVersion>4.0.0</modelVersion>
  70.     <groupId>com.obydul.spring.aop</groupId>
  71.     <artifactId>aop</artifactId>
  72.     <version>0.0.1-SNAPSHOT</version>
  73.     <build>
  74.         <sourceDirectory>src</sourceDirectory>
  75.         <plugins>
  76.             <plugin>
  77.                 <artifactId>maven-compiler-plugin</artifactId>
  78.                 <version>3.8.1</version>
  79.                 <configuration>
  80.                     <source>1.7</source>
  81.                     <target>1.7</target>
  82.                 </configuration>
  83.             </plugin>
  84.         </plugins>
  85.     </build>
  86.     <dependencies>
  87.         <dependency>
  88.             <groupId>org.springframework</groupId>
  89.             <artifactId>spring-context</artifactId>
  90.             <version>3.2.12.RELEASE</version>
  91.         </dependency>
  92.         <dependency>
  93.             <groupId>org.springframework</groupId>
  94.             <artifactId>spring-core</artifactId>
  95.             <version>3.2.12.RELEASE</version>
  96.         </dependency>
  97.         <dependency>
  98.             <groupId>org.springframework</groupId>
  99.             <artifactId>spring-beans</artifactId>
  100.             <version>3.2.12.RELEASE</version>
  101.         </dependency>
  102.         <dependency>
  103.             <groupId>org.springframework</groupId>
  104.             <artifactId>spring-aspects</artifactId>
  105.             <version>3.2.12.RELEASE</version>
  106.         </dependency>
  107.         <dependency>
  108.             <groupId>org.springframework</groupId>
  109.             <artifactId>spring-aop</artifactId>
  110.             <version>3.2.12.RELEASE</version>
  111.         </dependency>
  112.     </dependencies>
  113.     </project>
  114.  
  115.     ======= console output =======
  116.  
  117.     org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'camera' defined in class path resource [com/obydul/spring/aop/beans.xml]: Initialization of bean failed; nested exception is java.lang.ExceptionInInitializerError
  118.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:529)
  119.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
  120.     at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
  121.     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
  122.     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
  123.     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191)
  124.     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:636)
  125.     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:934)
  126.     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
  127.     at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
  128.     at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
  129.     at com.obydul.spring.aop.App.main(App.java:8)
  130.  
  131.     Caused by: java.lang.ExceptionInInitializerError
  132.     at org.springframework.cglib.core.KeyFactory$Generator.generateClass(KeyFactory.java:166)
  133.     at org.springframework.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
  134.     at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
  135.     at org.springframework.cglib.core.KeyFactory$Generator.create(KeyFactory.java:144)
  136.     at org.springframework.cglib.core.KeyFactory.create(KeyFactory.java:116)
  137.     at org.springframework.cglib.core.KeyFactory.create(KeyFactory.java:108)
  138.     at org.springframework.cglib.core.KeyFactory.create(KeyFactory.java:104)
  139.     at org.springframework.cglib.proxy.Enhancer.<clinit>(Enhancer.java:69)
  140.     at org.springframework.aop.framework.CglibAopProxy.createEnhancer(CglibAopProxy.java:234)
  141.     at org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:176)
  142.     at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:109)
  143.     at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:479)
  144.     at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:364)
  145.     at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:324)
  146.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:409)
  147.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1520)
  148.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
  149.     ... 11 more
  150.  
  151.     Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @179ece50
  152.     at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:357)
  153.     at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
  154.     at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:199)
  155.     at java.base/java.lang.reflect.Method.setAccessible(Method.java:193)
  156.     at org.springframework.cglib.core.ReflectUtils$2.run(ReflectUtils.java:56)
  157.     at java.base/java.security.AccessController.doPrivileged(AccessController.java:312)
  158.     at org.springframework.cglib.core.ReflectUtils.<clinit>(ReflectUtils.java:46)
  159.     ... 28 more
RAW Paste Data