Home > Enterprise >  CannotLoadBeanClassException: Error loading class
CannotLoadBeanClassException: Error loading class

Time:10-20

Recently I'm learning Spring framework. So, I am trying to check how Dependency Injection works in spring framework. As a result I have been created a new java project After running my project I'm getting this error

Main.java

package com.exe;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.to.someBO;

public class Main {
    public static void main(String[] args) {
        ApplicationContext ap = new ClassPathXmlApplicationContext("beans.xml");
        someBO bo = ap.getBean("proxy", someBO.class);
        bo.Validate();
        try {
            bo.Validate(17);
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }
}

beans.xml

<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"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/beans/spring-aop.xsd">

    <bean id="bo" ></bean>

    <bean id="ba" ></bean>
    <bean id="aa" ></bean>
    <bean id="bh" ></bean>
    <bean id="ea" ></bean>
    <bean id="proxty"
        >
        <property name="target" ref="bo"></property>
        <property name="interceptorName">
            <list>
                <value>aa</value>
            </list>
        </property>
    </bean>
</beans>

ExceptionAdvisor


import org.springframework.aop.ThrowsAdvice;

public class ExceptionAdvisor implements ThrowsAdvice {

    public void afterThrowing(Exception ex) {
        System.out.println("Additional concern if exception occurs");
    }

}

BeforeAdvisor


import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class BeforeAdvisor implements MethodBeforeAdvice{
    @Override
    public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable{
        System.out.println("Logging before call of method " arg0.getName());
    }

}

AfterAdvisor


import java.lang.reflect.Method;

import org.aspectj.weaver.patterns.ThrowsPattern;
import org.springframework.aop.AfterReturningAdvice;

public class AfterAdvisor implements AfterReturningAdvice{
    
    @Override
    public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable{
        System.out.println("Some stuff port method call" arg1.getName());
    }

}

BothAdvisor

package com.apps;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class BothAdvisor implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation arg0) throws Throwable {
        System.out.println("Before Method");
        arg0.proceed();
        System.out.println("After method");
        return null;
    }
}

someBO

package com.to;

public class someBO {
    public void Validate() {
        System.out.println("Validation stuff from BO");
    }

    public void Validate(int age) throws Exception {
        if (age < 18) {
            throw new ArithmeticException("Not Valid Age");

        } else {
            System.out.println("Vote Confirmed");
        }
    }
}

enter image description here

  • Related