Pessoal,
Tenho essas classes:
/*
* GerenciadorDeArquivos.java
*
* Created on 4 de Maio de 2007, 15:49
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package ExemploGerenciadorDeArquivos;
/**
*
* @author vlima
*/
public class GerenciadorDeArquivos {
/** Creates a new instance of GerenciadorDeArquivos */
public GerenciadorDeArquivos() {
}
public void salvarArquivo(String diretorioASalvar, String arquivoSalvo){
System.out.println("GerenciadorDeArquivos: SALVO["+arquivoSalvo+"] em ["+diretorioASalvar+"]");
}
}
/*
* PublicadorNoticias.java
*
* Created on 4 de Maio de 2007, 15:56
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package ExemploGerenciadorDeArquivos;
/**
*
* @author vlima
*/
public class PublicadorNoticias {
/** Creates a new instance of PublicadorNoticias */
public PublicadorNoticias() {
}
private GerenciadorDeArquivos gerenciadorDeArquivos = null;
public void setGerenciadorDeArquivos(GerenciadorDeArquivos
gerenciadorDeArquivos) {
this.gerenciadorDeArquivos = gerenciadorDeArquivos;
}
public void publicar(String titulo){
String arquivo = "file.txt";
String diretorio = "/usr/local/reportagens";
gerenciadorDeArquivos.salvarArquivo(diretorio, arquivo);
}
}
/*
* LogAdvice.java
*
* Created on 7 de Maio de 2007, 08:27
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package ExemploGerenciadorDeArquivos;
import java.lang.reflect.Method;
import java.util.Date;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.SpringProxy;
/**
*
* @author vlima
*/
public class LogAdvice implements MethodBeforeAdvice {
/** Creates a new instance of LogAdvice */
public LogAdvice() {
}
public void before(Method metodo, Object[] argumentos, Object alvo) throws Throwable {
// lista de parâmetros
StringBuffer lista = new StringBuffer();
for (int i = 0; i < argumentos.length; i++) {
lista.append("\n" + argumentos[i]);
}
System.out.println("LOG --- " + new Date() + " --- Executado método'" + metodo.getName() + "' utilizando como parâmetros " + lista);
}
}
/*
* IniciarUsandoSpring.java
*
* Created on 4 de Maio de 2007, 16:15
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package ExemploGerenciadorDeArquivos;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
*
* @author vlima
*/
public class IniciarUsandoSpring {
/** Creates a new instance of IniciarUsandoSpring */
public IniciarUsandoSpring() {
}
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:ExemploGerenciadorDeArquivos/applicationContext-ExemploGerenciadorDeArquivos.xml");
PublicadorNoticias publicadorNoticias = (PublicadorNoticias)applicationContext.getBean("publicadorNoticias");
publicadorNoticias.publicar("teste");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!--<bean id="gerenciadorDeArquivo"
class="ExemploGerenciadorDeArquivos.GerenciadorDeArquivos"
/>
<bean id="publicadorNoticias"
class="ExemploGerenciadorDeArquivos.PublicadorNoticias">
<property name="gerenciadorDeArquivos" ref="gerenciadorDeArquivo"/>
</bean>-->
<bean id="gerenciadorDeArquivoTarget" class="ExemploGerenciadorDeArquivos.GerenciadorDeArquivos"/>
<bean id="gerenciadorDeArquivo" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="interceptorNames"><value>logAdvice</value></property>
<property name="target" ref="gerenciadorDeArquivoTarget" />
</bean>
<bean id="logAdvice" class="ExemploGerenciadorDeArquivos.LogAdvice" />
<bean id="publicadorNoticias" class="ExemploGerenciadorDeArquivos.PublicadorNoticias">
<property name="gerenciadorDeArquivos" ref="gerenciadorDeArquivo"/>
</bean>
</beans>
Quando executo o IniciarUsandoSpring dá o seguinte erro:
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@750159: display name [org.springframework.context.support.ClassPathXmlApplicationContext@750159]; startup date [Mon May 07 09:04:27 BRT 2007]; root of context hierarchy
07/05/2007 09:04:27 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [ExemploGerenciadorDeArquivos/applicationContext-ExemploGerenciadorDeArquivos.xml]
07/05/2007 09:04:28 org.springframework.context.support.AbstractApplicationContext refresh
INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@750159]: org.springframework.beans.factory.support.DefaultListableBeanFactory@23e5d1
07/05/2007 09:04:28 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@23e5d1: defining beans [gerenciadorDeArquivoTarget,gerenciadorDeArquivo,logAdvice,publicadorNoticias]; root of factory hierarchy
07/05/2007 09:04:28 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@23e5d1: defining beans [gerenciadorDeArquivoTarget,gerenciadorDeArquivo,logAdvice,publicadorNoticias]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'publicadorNoticias' defined in class path resource [ExemploGerenciadorDeArquivos/applicationContext-ExemploGerenciadorDeArquivos.xml]: Cannot resolve reference to bean 'gerenciadorDeArquivo' while setting bean property 'gerenciadorDeArquivos'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'gerenciadorDeArquivo': FactoryBean threw exception on object creation; nested exception is org.springframework.aop.framework.AopConfigException: Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces.
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'gerenciadorDeArquivo': FactoryBean threw exception on object creation; nested exception is org.springframework.aop.framework.AopConfigException: Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces.
Caused by: org.springframework.aop.framework.AopConfigException: Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces.
at org.springframework.aop.framework.DefaultAopProxyFactory.createAopProxy(DefaultAopProxyFactory.java:65)
at org.springframework.aop.framework.ProxyCreatorSupport.createAopProxy(ProxyCreatorSupport.java:106)
at org.springframework.aop.framework.ProxyFactoryBean.getSingletonInstance(ProxyFactoryBean.java:297)
at org.springframework.aop.framework.ProxyFactoryBean.getObject(ProxyFactoryBean.java:227)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectFromFactoryBean(AbstractBeanFactory.java:1191)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1162)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:261)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:109)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1073)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:835)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:423)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:144)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:279)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:360)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:91)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:75)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:65)
at ExemploGerenciadorDeArquivos.IniciarUsandoSpring.main(IniciarUsandoSpring.java:26)
Java Result: 1
EXECUTADO COM SUCESSO (tempo total: 1 segundo)
Alguém pode me ajudar.