Spring + Hibernate Auto config Annotations

5 respostas
W

Olá galera !

Estou configurando um projeto, e está funcionando corretamente com o
Spring + Hibernate, entretanto estou colocando manualmente as classes
mapeadas pelo hibernate no applicationContext.xml ...

Eu sei que tem uma forma que o proprio spring identifica as classes anotadas
e só se tem que passar o pacote base para a leitura dos beans anotados.

Segue meu atual applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost/hibernate_teste" />
        <property name="username" value="root" />
        <property name="password" value="" />
    </bean>

    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                <value>br.com.meupacote.model.Usuario</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.show_sql">true</prop>
                <!--<prop key="hibernate.hbm2ddl.auto">create</prop>-->
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>


    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
</beans>

Obrigado desde já pelo auxilio ...

5 Respostas

g4j

Meu applicationContext.xml está assim:

<?xml version="1.0" encoding="UTF-8"?>
<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"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:zkspc="http://www.zkoss.org/2008/zkspring/core" 
	xmlns:security="http://www.springframework.org/schema/security"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.0.xsd
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
    default-autowire="byName">

	<context:component-scan base-package="br.com.blabla" scoped-proxy="interfaces"/>	

	<context:annotation-config />


</beans>
W

g4j:
Meu applicationContext.xml está assim:

<?xml version="1.0" encoding="UTF-8"?>
<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"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:zkspc="http://www.zkoss.org/2008/zkspring/core" 
	xmlns:security="http://www.springframework.org/schema/security"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.0.xsd
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
    default-autowire="byName">

	<context:component-scan base-package="br.com.blabla" scoped-proxy="interfaces"/>	

	<context:annotation-config />


</beans>

Saquei cara, essa config implementada em seu appCtx é para o Hibernate mesmo ou é para
detectar as anotações do proprio spring ?

g4j

Ah é. para o spring cara…

Para o hibernate tem um atributo na configuração da sessionfactory chamado packagesToScan:

http://static.springsource.org/spring/docs/2.5.6/api/org/springframework/orm/hibernate3/annotation/AnnotationSessionFactoryBean.html

W

Deu certinho cara, obrigado !

Me diga uma coisa, tu acomselha alguma implementação de DAO Generico
especifico para se trabalhar melhor neste contexto que estou trabalhando ?

Algo que se acople direitinho ao Spring + Hibernate ?
Para depois implementar AOP para transação ?

g4j

wakko:
Deu certinho cara, obrigado !

Me diga uma coisa, tu acomselha alguma implementação de DAO Generico
especifico para se trabalhar melhor neste contexto que estou trabalhando ?

Algo que se acople direitinho ao Spring + Hibernate ?
Para depois implementar AOP para transação ?

Cara, o próprio spring tem alguma coisa para acesso à dados. É o HibernateTemplate.

http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/orm/hibernate3/HibernateTemplate.html

Na internet você deverá encontrar alguns exemplos de utilização.

Criado 7 de abril de 2011
Ultima resposta 8 de abr. de 2011
Respostas 5
Participantes 2