Bonjour à tous,

Parmi les nouvelles fonctionnalités de spring 3.1 figure le support des servlet 3.0 permettant de se passer du web.xml.

A des fins de veille, j'essaye de transformer un de mes projets de manière à pouvoir me passer de certaines configuration XML (ex: configuration base de données pour améliorer les tests unitaires).

Je bloque aujourd'hui sur une configuration spécifique de mon application

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
 
<context:component-scan base-package="com.gh.auditv2"/>
	<!-- <context:spring-configured/>-->
 
	<context:load-time-weaver
		weaver-class="org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver"
		aspectj-weaving="on" />
 
 
	<!-- fichier de properties -->
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<value>classpath:databaseConfiguration.properties</value>
		</property>
	</bean>
	<jee:jndi-lookup id="auditv2DataSource" jndi-name="java:comp/env/jdbc/oracle" />
 
	<bean id="sessionFactory"
		class="com.gh.common.utils.hibernate.AnnotationAndHticSessionFactoryBean">
		<qualifier type="fr.ht.dao.MainSessionFactory"/>
		<property name="dataSource" ref="auditv2DataSource" />
		<property name="hibernateProperties" ref="hibernateProperties" />
		<property name="packagesToScan">
			<list>
				<value>com.gh.auditv2.domain</value>
			</list>
		</property>
		<property name="hibMappingProperties" value="hib-mapping.properties" />
		<property name="excludesHbmsList">
			<list>
			</list>
		</property>
	</bean>
 
	<util:properties id="hibernateProperties">
		<prop key="hibernate.dialect">${hibernate.dialect}</prop>
		<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
		<prop key="hibernate.max_fetch_depth">${hibernate.max_fetch_depth}</prop>
	</util:properties>
 
	<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<tx:annotation-driven transaction-manager="transactionManager" />
Sur cette configuration, on remarque en particulier le bean sessionFactory qui repose sur une classe custom AnnotationAndHticSessionFactoryBean (extension de AnnotationSessionFactoryBean) ainsi que la ligne

Code : Sélectionner tout - Visualiser dans une fenêtre à part
<qualifier type="fr.ht.dao.MainSessionFactory"/>
C'est précisément cette ligne qui semble me poser problème. J'ai en effet transformé ce fichier XML dans sa totalité en code java à l'exception de cette ligne. En "mode" XML, le serveur se lance; en "mode" java, le serveur lève une NoSuchBeanDefinitionException ("No matching bean of type [org.hibernate.SessionFactory]")

Ce qualifier correspond au code suivant:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
 
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface MainSessionFactory {
 
}
Qui est appliqué ainsi

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
 
public abstract class GenericHibernateDAO<E extends IEntity<ID>, ID extends Serializable>
implements IGenericDAO<E, ID> {
 
	@Autowired
	@MainSessionFactory
	private SessionFactory sessionFactory;
[....]

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
 
@Profile("dev")
@ComponentScan(basePackages = "com.gh.auditv2")
@Configuration
@EnableTransactionManagement
@EnableLoadTimeWeaving(aspectjWeaving = AspectJWeaving.ENABLED)
@PropertySource("classpath:/databaseConfiguration.properties")
public class DBConfigDev implements LoadTimeWeavingConfigurer  {
	/**Logger.*/
	private static final Logger logger = LoggerFactory.getLogger(DBConfigDev.class);
	/**Environnement.*/
	@Autowired
	private Environment env;
	/**Datasource.*/
	@Resource(name = "dataSource", lookup = "java:comp/env/jdbc/oracle")
	private DataSource ds;
	/**Props.*/
	private static Properties hibernatePropz;
	/**Session Factory.*/
	@MainSessionFactory
	public SessionFactory sf;
 
	@Override
	public LoadTimeWeaver getLoadTimeWeaver() {
		ReflectiveLoadTimeWeaver rltw = new ReflectiveLoadTimeWeaver();
		return rltw;
	}
 
	/**
         * SessionFactory.
         * @return session factory
         * @throws NamingException naming exception
         */
	@Bean
	public AnnotationAndHticSessionFactoryBean sessionFactoryBean() throws NamingException {
		AnnotationAndHticSessionFactoryBean sfb = new AnnotationAndHticSessionFactoryBean();
		sfb.setDataSource(ds);
		sfb.setPackagesToScan(new String[] {"com.gh.auditv2.domain"});
		sfb.setHibernateProperties(hibernateProps());
		sfb.setHibMappingProperties("hib-mapping.properties");
		sfb.setExcludesHbmsList(new String[] {""});
		sf = sfb.getObject();
		return sfb;
	}
 
	/**
         * Configuration de la datasource.
         * @return datasource
         * @throws NamingException naming exception
         */
	@Bean
	public DataSource dataSource() throws NamingException {
		logger.info("Configure datasource ...");
		return ds;
	}
 
	@Bean
	public SessionFactory sessionFactory() {
		return sf;
	}
 
	/**
         * Propriétés hibernate.
         * @return props
         */
	@Bean
	public Properties hibernateProps() {
		if (hibernatePropz == null) {
			logger.info("Set hibernate props");
			hibernatePropz = new Properties();
			hibernatePropz.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
			hibernatePropz.setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
			hibernatePropz.setProperty("hibernate.max_fetch_depth", env.getProperty("hibernate.max_fetch_depth"));
		}
 
		return hibernatePropz;
	}
 
	/**
         * Configuration du transactionManager.
         * @return transaction Manager.
         * @throws NamingException naming exception
         */
	@Bean
	public HibernateTransactionManager transactionManager() throws NamingException {
		HibernateTransactionManager txManager = new HibernateTransactionManager();
		txManager.setSessionFactory(sessionFactoryBean().getObject());
		return txManager;
	}
 
	/**
         * exception Translation.
         * @return exceptionTranslation
         */
	@Bean
	public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
		return new PersistenceExceptionTranslationPostProcessor();
	}
Je cherche donc des exemples qui me permettrait de faire fonctionner ce code

Merci d'avance !