0

Whenever I m running the code attached below I m facing this error....

g4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'proxy': FactoryBean threw exception on object creation; nested exception is java.lang.ExceptionInInitializerError
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:150)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:102)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1387)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:244)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1048)
    at Sample.Test.main(Test.java:14)
Caused by: java.lang.ExceptionInInitializerError
    at net.sf.cglib.core.KeyFactory$Generator.generateClass(KeyFactory.java:166)
    at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
    at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
    at net.sf.cglib.core.KeyFactory$Generator.create(KeyFactory.java:144)
    at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:116)
    at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:108)
    at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:104)
    at net.sf.cglib.proxy.Enhancer.<clinit>(Enhancer.java:69)
    at org.springframework.aop.framework.Cglib2AopProxy.createEnhancer(Cglib2AopProxy.java:228)
    at org.springframework.aop.framework.Cglib2AopProxy.getProxy(Cglib2AopProxy.java:170)
    at org.springframework.aop.framework.ProxyFactoryBean.getProxy(ProxyFactoryBean.java:362)
    at org.springframework.aop.framework.ProxyFactoryBean.getSingletonInstance(ProxyFactoryBean.java:316)
    at org.springframework.aop.framework.ProxyFactoryBean.getObject(ProxyFactoryBean.java:242)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:143)
    ... 6 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 @3e92efc3
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)
    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 net.sf.cglib.core.ReflectUtils$2.run(ReflectUtils.java:56)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:318)
    at net.sf.cglib.core.ReflectUtils.<clinit>(ReflectUtils.java:46)
    ... 20 more

BeforeAdviceTest.java

package Sample;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class BeforeAdviceTest implements MethodBeforeAdvice {
    @Override  
    public void before(Method method, Object[] args, Object target)throws Throwable {  
        System.out.println("Additional concern " +"before business logic.");  
    }  
}

BuisnessLogic.java

package Sample;

public class BusinessLogic {

    public void implementBusinessLogic(){
        System.out.println("Business logic executed.");
    }
}

Test.java

package Sample;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class Test {
 public static void main(String[] args) {
  //Get ApplicationContext using spring configuration file.
  ApplicationContext context = 
    new ClassPathXmlApplicationContext("applicationContext.xml");
 
  //Get BusinessLogic bean object from ApplicationContext instance. 
  BusinessLogic businessLogic = 
    (BusinessLogic) context.getBean("proxy", BusinessLogic.class);
 
  //Call implementBusinessLogic method of BusinessLogic bean.
  businessLogic.implementBusinessLogic();
 } 
}

applicationContext.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
   <bean id="businessLogic" 
        class="Sample.BusinessLogic"/>
   <bean id="beforeAdviceTest" 
        class="Sample.BeforeAdviceTest"/>
   <bean id="proxy" 
        class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="target" ref="businessLogic"></property>  
    <property name="interceptorNames">  
     <list>  
      <value>beforeAdviceTest</value>  
     </list>  
    </property> 
   </bean> 
</beans>
R.G
  • 6,436
  • 3
  • 19
  • 28
Patel Harsh
  • 231
  • 1
  • 2
  • 7
  • Does this answer your question? [log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment)](https://stackoverflow.com/questions/46608988/log4jwarn-no-appenders-could-be-found-for-logger-org-springframework-core-env) – sorifiend May 08 '22 at 23:58
  • do go through this stackoverflow [Q&A](https://stackoverflow.com/questions/41265266/how-to-solve-inaccessibleobjectexception-unable-to-make-member-accessible-m) . The accepted [answer](https://stackoverflow.com/a/41265267/4214241) to the question probably would solve your issue. – R.G May 09 '22 at 04:22
  • @sorifiend That doesn't seem to be the problem this question is asking about. – Mark Rotteveel May 09 '22 at 09:48
  • The stack trace looks as if you are using an outdated CGLIB version which does not play nice with more recent JDK versions. You want to upgrade Spring or at least CGLIB. The question might be a duplicate of [that one](https://stackoverflow.com/a/71302730/1082681). Please let me know if it helps. – kriegaex May 14 '22 at 09:00

1 Answers1

0

Issue is not with AOP, For logging, your using log4j dependency, We needs to do some configuration for log4j. May be configuration missed, Causing these issues

  • how can I do that ?? – Patel Harsh May 09 '22 at 12:58
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 10 '22 at 07:10