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 :
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.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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();
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![]()
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 : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 @Repository @Transactional public class DaoImpl implements Dao { @PersistenceContext private EntityManager em; ... }
/*************** SERVICE **********************/
/*************** MAIN**********************/
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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; } ... }
/*************** Spring-config.xml **********************/
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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();
Merci beaucoup d'avance pour votre aide.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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>
Partager