Configurando classpath com Ireport:
-> Ferramentas
-> Opções
-> Classpath
-> addFolder [ coloquei todos os diretórios (pacotes) da aplicação ]
Configurando conexão com Ireport:
-> Connections / Datasources
-> New
-> Spring loaded Hibernate connection
-> Next
->Name: conexao
->Spring configuration: applicationContext.xml,Banco.hbm.xml
->Session Factory Bean ID: sessionFactory
[b]Mensagem de Erro quando clico no botão test:
IOException parsing XML document from class path resource[Banco.hbm.xml];nested exception is java.net.UnknownHostException:hibernate.sourceforge.net[/b]
[code]package aop;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class OperacaoAdvice implements MethodInterceptor {
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("AOP -> Método chamado: " + methodInvocation.getMethod().getName());
Object object = null;
try {
object = methodInvocation.proceed();
System.out.println(object);
return object;
}catch (Exception e) {
// TODO: handle exception
}
return object;
}
}
package aop;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class RecuperacaoListasAdvice implements
AfterReturningAdvice {
public void afterReturning(Object returnValue, Method metodo,
Object[] arg2, Object target) throws Throwable {
Object lista = returnValue;
System.out.println("AOP -> A lista retornou " + lista + " objetos.");
}
}
<?xml version="1.0" encoding="UTF-8"?><class name="Banco" table="TB_BANCO" lazy="false">
<id name="id" type="java.lang.Long" column="ID">
<generator class="native" />
</id>
<property name="nome" column="NOME" type="java.lang.String"
length="50" unique="true" />
</class>
package banco;
import java.io.Serializable;
@SuppressWarnings(“serial”)
public class Banco implements Serializable {
private Long id;
private String nome;
public Banco() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String toString() {
return id + " - " + nome;
}
}
package banco;
public interface BancoDao {
public void salvarBanco(Banco banco);
}
package banco;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class BancoImp extends HibernateDaoSupport implements BancoDao {
@Override
public void salvarBanco(Banco banco) {
getHibernateTemplate().saveOrUpdate(banco);
}
}
<?xml version="1.0" encoding="UTF-8"?><!-- ***************************************************************************************************
Configuração AOP: spring framework
**************************************************************************************************** -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"> <value>org.hsqldb.jdbcDriver</value>
</property> <property name="url"> <value>jdbc:hsqldb:database/tutorial/db_sistema</value>
</property> <property name="username"> <value>sa</value> </property> <property
name="password"> <value></value> </property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>banco/Banco.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.max_fetch_depth">2</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
<prop key="hibernate.c3p0.minPoolSize">5</prop>
<prop key="hibernate.c3p0.maxPoolSize">20</prop>
<prop key="hibernate.c3p0.timeout">600</prop>
<prop key="hibernate.c3p0.max_statement">50</prop>
<prop key="hibernate.c3p0.testConnectionOnCheckout">false</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- ***************************************************************************************************
Configuração AOP: spring framework
**************************************************************************************************** -->
<bean id="operacaoAdvice" class="aop.OperacaoAdvice"></bean>
<bean id="recuperacaoListasAdvice" class="aop.RecuperacaoListasAdvice"></bean>
<bean id="operacaoPointcutAdvisor"
class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="mappedNames">
<list>
<value>save*</value>
<value>remove*</value>
</list>
</property>
<property name="advice">
<ref bean="operacaoAdvice" />
</property>
</bean>
<bean id="recuperacaoListasPointcutAdvisor"
class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="mappedName">
<value>pegar*</value>
</property>
<property name="advice">
<ref bean="recuperacaoListasAdvice" />
</property>
</bean>
<!-- ***************************************************************************************************
Configuração AOP: banco
**************************************************************************************************** -->
<bean id="bancoImpFachadaTarget" class="banco.BancoImp">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="bancoFachadaService" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref bean="bancoImpFachadaTarget" />
</property>
</bean>
package principal;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import banco.Banco;
import banco.BancoDao;
import banco.BancoImp;
public class Sistema {
private BancoDao bancoDao;
private Banco banco;
private static ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"classpath:principal/applicationContext.xml");
public Sistema() {
banco = new Banco();
banco.setNome("Bradesco");
bancoDao = (BancoImp) applicationContext
.getBean("bancoFachadaService");
bancoDao.salvarBanco(banco);
System.out.println(banco);
}
public static void main(String[] args) {
Sistema sistema = new Sistema();
}
}[/code]
Bibliotecas utilizadas:
antlr-2.7.6
cleanimports
concurrent-1.3.2
hibernate3
jaas
jbosscache-core-3.2.1.GA
jgroups-2.2.8
oscache-2.1
spring-aop
spring-hibernate
syndiag2
asm
commons-collections-3.1
connector
hibernate-annotations
jacc-1_0-fr
jboss-common
jstl
proxool-0.8.3
spring-beans
spring-orm
versioncheck
asm-attrs
commons-dbcp
dom4j-1.6.1
hibernate-commons-annotations
javassist-3.9.0.GA
jboss-jmx
jta-1.1
slf4j-api-1.6.1
spring-context
spring-remoting
xerces-2.6.2
c3p0-0.9.0
commons-logging-1.0.4
ehcache-1.1
hsqldb
jaxen-1.1-beta-7
jboss-system
log4j-1.2.16
slf4j-simple-1.6.1
spring-core
standard
xml-apis
cglib-2.1.3
commons-pool
ejb3-persistence
infinispan-core-4.0.0.FINAL
jboss-cache
jdbc2_0-stdext
mysql-connector-java-3.1.10-bin
spring
spring-dao
swarmcache-1.0rc2