0

Spring aop configuration problem in eclipse

-- Same code compile and run in Spring tools suite. But does not compile and run in eclipse. What is the problem I can't found out?

-- bean classes --

package com.obydul.spring.aop;

public class Camera {
    public void snap() {
        System.out.println("SNAP!");
    }
}

-- another bean classes --

package com.obydul.spring.aop;

public class Logger {
    public void aboutToTakePhoto() {
        System.out.println("About to take photo...");
    }
}

-- Spring aop configuration in beans.xml

<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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-3.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
 
    <bean id="camera" class="com.obydul.spring.aop.Camera"></bean>
 
    <bean id="logger" class="com.obydul.spring.aop.Logger"></bean>
 
    <aop:config>
        <aop:pointcut
            expression="execution(void com.obydul.spring.aop.Camera.snap())"
            id="cameraSnap" />
        <aop:aspect ref="logger" id="loggeraspect">
            <aop:before method="aboutToTakePhoto"
                pointcut-ref="cameraSnap" />
        </aop:aspect>
        </aop:config>
    </beans>

main function:

public static void main(String[] args) {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("com/obydul/spring/aop/beans.xml");
            Camera camera = (Camera) context.getBean("camera");
            camera.snap();
 
            context.close();
        }

-- exception in console output --

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
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:529)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:636)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:934)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.obydul.spring.aop.App.main(App.java:8)
 
Caused by: java.lang.ExceptionInInitializerError
    at org.springframework.cglib.core.KeyFactory$Generator.generateClass(KeyFactory.java:166)
    at org.springframework.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
    at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
    at org.springframework.cglib.core.KeyFactory$Generator.create(KeyFactory.java:144)
    at org.springframework.cglib.core.KeyFactory.create(KeyFactory.java:116)
    at org.springframework.cglib.core.KeyFactory.create(KeyFactory.java:108)
    at org.springframework.cglib.core.KeyFactory.create(KeyFactory.java:104)
    at org.springframework.cglib.proxy.Enhancer.<clinit>(Enhancer.java:69)
    at org.springframework.aop.framework.CglibAopProxy.createEnhancer(CglibAopProxy.java:234)
    at org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:176)
    at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:109)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:479)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:364)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:324)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:409)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1520)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
    ... 11 more
 
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
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:357)
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
    at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:199)
    at java.base/java.lang.reflect.Method.setAccessible(Method.java:193)
    at org.springframework.cglib.core.ReflectUtils$2.run(ReflectUtils.java:56)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:312)
    at org.springframework.cglib.core.ReflectUtils.<clinit>(ReflectUtils.java:46)
    ... 28 more

-- I don't know what is the problem in my aop configuration code in beans.xml. Please any one suggest to me how can I solved this problem.

dur
  • 15,689
  • 25
  • 79
  • 125
  • Do go through this [SO Q&A](https://stackoverflow.com/q/41265266) . A quick guess about the difference between STS and Eclipse would be the configured JRE in both . Try to set the same JRE version in Eclipse , that is configured in STS – R.G Oct 16 '21 at 11:00
  • Looks like that code is trying to do something that is not allowed in newer versions of Java - check which Java versions the code supports. – greg-449 Oct 16 '21 at 11:24
  • R.G thanks a lot. setup default JRE in eclipse same as STS. It's work perfectly. – Obydul Islam Oct 16 '21 at 12:26
  • greg-449 thanks for your feedback. It's solved. – Obydul Islam Oct 16 '21 at 12:43

0 Answers0