[Transaction][Annotation] Utilisation des annotations Spring
Bonjour tout le monde,
J'ai une question concernant les annotations Spring.
Mon application utilise aujourd'hui spring-config.xml pour déclarer les DAO et les services que je fais injecter dans ma classe Main via cette syntaxe :
Classe Main :
Code:
1 2 3 4 5 6 7 8 9 10
|
public static Service service;
public static void main(String[] args) throws Exception {
// configuration de l'application
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"spring-config.xml");
// couche service
service = (Service) ctx.getBean("service");
service.getAllPersonnes(); |
Mon problème est que je commence à avoir un bon nombre de DAO et de services. Je voulais passer aux annotations pour éviter d'augmenter la taille du fichier spring-config.xml en y ajoutant chaque fois les nouveaux DAO et les nouveaux services.
Je me suis documenter aux sujet des annotations grace au post de Tommy. Mais je bloque au niveau de l'injection des services dans ma classe Main.
Je ne sais pas quel annotation utiliser et comment faire pour récuperer le service :cry:
Je ne sais pas aussi qu'est-ce qu'il faut changer dans mon fichier de configuration pour que ca marche !
voici mon code :
/*************** DAO **********************/
Code:
1 2 3 4 5 6 7 8 9
|
@Repository
@Transactional
public class DaoImpl implements Dao {
@PersistenceContext
private EntityManager em;
...
} |
/*************** SERVICE **********************/
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
@Service
public class ServiceImpl implements IService {
@Autowired
//Injection du DAO
private Dao dao;
public void setDao(Dao dao) {
this.dao = dao;
}
...
} |
/*************** MAIN**********************/
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
public static Service service;
public static void main(String[] args) throws Exception {
// Normalement il ne faut plus faire ceci mais une annotation ???
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"spring-config.xml");
// Injection du service
service = (Service) ctx.getBean("service");
service.getAllPersonnes(); |
/*************** Spring-config.xml **********************/
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
<?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:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<tx:annotation-driven />
<context:annotation-config/>
<context:component-scan base-package="dao" />
<context:component-scan base-package="service" />
<aop:aspectj-autoproxy />
<!-- couches applicatives
<bean id="dao" class="dao.DaoImpl" />
<bean id="service" class="service.ServiceImpl">
<property name="dao" ref="dao" />
</bean>
-->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="generateDdl" value="true" />
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop><!-- validate | update | create | create-drop-->
</props>
</property>
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
</bean>
<!-- la source de donnéees DBCP -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://10.122.204.52/jpa" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!-- le gestionnaire de transactions-->
<tx:annotation-driven transaction-manager="txManager" />
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- traduction des exceptions -->
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<!-- persistence -->
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
</beans> |
Merci beaucoup d'avance pour votre aide.
Injection d'un bean Service dans un main
Salut,
Merci de m'avoir répondu.
Je t'explique mon problème :
Je développe une application avec plusieurs DAO et plusieurs Services. Donc je passais par le fichier spring-config.xml pour les déclarer tous afin de me les faire injecter dans ma classe main.
Afin d'éviter de me taper toutes les déclarations dans mon spring-config.xml, j'ai pensé passer par les annotation de spring. Donc, j'ai rajouté les bonnes annotations dans mon DAO puis dans ma classe service, comme je l'ai indiqué dans mon ancien post, mais je ne sais pas quelles annotations utiliser pour injecter mes services dans ma classe main.
Le code ci-dessous correspond à mon ancienne classe main qui se basait sur le fichier de conf pour injecter un service.
Code:
1 2 3 4 5 6 7
|
// configuration de l'application
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"spring-config.xml");
// couche service
service = (Service) ctx.getBean("service");
service.getAllPersonnes(); |
Mais logiquement, avec les annotation je n'aurais plus à faire ça ?
De toutes les manières, ce bout de code ne marche plus !
Je voulais savoir par quoi le remplacer pour le faire injecter mon service ServiceImpl ?
Merci beaucoup pour ta réponse.
Merci beaucoup pour ta réponse
Merci beaucoup pour ta réponse.
Ca marche parfaitement.
Je vais fermer ce post et je vais ouvrir un autre, j'ai un problème relatif à la session hibernate dans Spring.