Announcement Announcement Module
Collapse
No announcement yet.
Error creating bean... No visible constructors... Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Error creating bean... No visible constructors...

    Hi,
    I am trying to create a MethodInterceptor to intercept a Facade when it's called. The facade is a singleton with a private constructor and a getInstance() method to get access.
    I need create an interceptor to figure out the method that is being called.
    If someone knows where is the problem in the code, please tell me what about.

    The configuration
    Code:
    <bean name="exampleFacade" 
    	class="com.example.ExampleFacade" 
    	factory-method="getInstance">
    </bean>
    
    <bean name="exampleInterceptor"
    	class="com.example.ExampleInterceptor" />
     <bean id="exampleFacadeProxy"
    	class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
    	<property name="beanNames">
    		<list>
    			<value>exampleFacade</value>
    		</list>
    	</property>
    	<property name="interceptorNames">
    		<list>
    			<value>exampleInterceptor</value>
    		</list>
    	</property>
    </bean>
    The classes
    Code:
    public class ExampleFacade {
    	
    	/**
    	 * Singleton instance.
    	 */
    	private static ExampleFacade facade;
    	
    	/**
    	 * Private Constructor.
    	 */
    	private ExampleFacade() {
    	}
    	
    	/**
    	 * To get the unique instance.
    	 */
    	public static ExampleFacade getInstance() {
    		if (facade == null) {
    			synchronized (logger) {
    				if (facade == null) {
    					facade = new ExampleFacade();
    				}
    			}
    		}
    		return facade;
    	}
    	...
    }
    Code:
    public class ExampleInterceptor implements MethodInterceptor {
    
    	protected static final Log logger = LogFactory.getLog(ExampleInterceptor.class);
    
    
    	public Object invoke(MethodInvocation methodInvocation) throws Throwable {
    
    		long start = System.currentTimeMillis();
    		try {
    
    			logger.info("-----------Before proceed----------------------");
    			Object result = methodInvocation.proceed();
    			logger.info("-----------After proceed----------------------");
    			return result;
    		}
    		finally {
    			long end = System.currentTimeMillis();
    			long timeMs = end - start;
    			System.out.println("Method: " + methodInvocation.getMethod().getName() + " took: " + timeMs + "ms.");
    		}
    	}
    
    }
    The Exception
    Code:
     ERROR [2008-12-01 14:50:50,049] [main] [web.context.ContextLoader] [] - Context initialization failed
     org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'exampleFacade' defined in ServletContext resource [/WEB-INF/spring/bean-context.xml]: Initialization of bean failed; nested exception is null
    java.lang.IllegalArgumentException: No visible constructors in class com.example.ExampleFacade
    	at net.sf.cglib.proxy.Enhancer.filterConstructors(Enhancer.java:531)
    	at net.sf.cglib.proxy.Enhancer.generateClass(Enhancer.java:448)
    ...
    ...
    ...

  • #2
    Spring uses jdk- or cglib-based proxies for aop implementation. Cglib proxy can't be applied to the class without visible no-args constructor. That's the reason of an error.

    Solution: switch to jdk-based proxies or use aspectj weaving.

    Comment

    Working...
    X