Take the 2-minute tour ×

Let's consider that I have some Class A which has property of class B.

public class ClassA{

private ClassB classB; 

public ClassA(ClassB classB){
 this.classB = classB;

 //some methods ommitted.

No I have CGLIB proxy:

public class CGLibProxy  implements MethodInterceptor{

            MethodProxy methodProxy) throws Throwable {

    if (method.getName().startsWith("print")){
        System.out.println("We will not run any method started with print"); 
        return null;
        return methodProxy.invokeSuper(object, args);

Now, When I use CGLib for ClassA , proxy creates ClassA instance.

My question is how can I pass classB parameter to this proxy, because as far As I understand CGLib will run empty constructor for ClassA?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

I do not see any code examples on how you are wrapping ClassA in with the CGLibProxy class but if you are dealing with cglib directly then you should have a instance of net.sf.cglib.proxy.Enhancer in that case you can supply the constructor args as follows.

import net.sf.cglib.proxy.Enhancer;

public class CGLibProxyMain {

    public static void main(String[] args) {
        Enhancer enhancer = new Enhancer();
        enhancer.setCallback(new CGLibProxy());
        ClassA a = (ClassA) enhancer.create(new Class[] {ClassB.class}, new Object[] {new ClassB()});
share|improve this answer
add comment

Your Answer


