Catégorie: Spring

24/05/2010

Permalink 16:21:56, Catégories: Java, Spring, 846 mots   French (FR) , fabou3377

Accès MySql via Spring et jndi, tomcat

1.Configuration

Placer le jar de mysql dans le répertoire lib de tomcat

2.Dossier META-INF, context.xml de l'application
 
<?xml version="1.0" encoding="UTF-8"?> 
<Context antiJARLocking="true" path="/myApp"> 
  <!-- Specify a JDBC datasource --> 
  <Resource name="jdbc/dbmyapp" auth="Container" type="javax.sql.DataSource" 
  username="user" password="password" driverClassName="com.mysql.jdbc.Driver" 
  url="jdbc:mysql://localhost:3306/database?autoReconnect=true" 
  validationQuery="select 1" maxActive="10" maxIdle="4" /> 
   
  <!-- To work with the authentification --> 
  <Realm className="org.apache.catalina.realm.DataSourceRealm" 
  dataSourceName="jdbc/database" 
  localDataSource="true" 
  roleNameCol="LB_ROLENAME" 
  userCredCol="LB_PASSWORD" 
  userNameCol="LB_USERNAME" 
  userRoleTable="T_ROLE" 
  userTable="T_USER"/> 
</Context> 

3. 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:aop="http://www.springframework.org/schema/aop" 
  xmlns:context="http://www.springframework.org/schema/context" 
  xmlns:flow="http://www.springframework.org/schema/webflow-config" 
  xmlns:jms="http://www.springframework.org/schema/jms" 
  xmlns:jee="http://www.springframework.org/schema/jee" 
  xmlns:lang="http://www.springframework.org/schema/lang" 
  xmlns:osgi="http://www.springframework.org/schema/osgi" 
  xmlns:tx="http://www.springframework.org/schema/tx" 
  xmlns:util="http://www.springframework.org/schema/util" 
  xmlns:p="http://www.springframework.org/schema/p" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd 
  http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-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/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd 
  http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd 
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd"> 
  <!--bean id="propertyConfigurer" 
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" 
  p:location="/WEB-INF/jdbc.properties" /> 
 
  <bean id="dataSource" 
  class="org.springframework.jdbc.datasource.DriverManagerDataSource" 
  p:driverClassName="${jdbc.driverClassName}" 
  p:url="${jdbc.url}" 
  p:username="${jdbc.username}" 
  p:password="${jdbc.password}" /--> 
 
 
  <!-- JPA --> 
 
  <tx:annotation-driven /> 
   
  <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="showSql" value="true" /> <!-- Prints used SQL to stdout --> 
  <property name="generateDdl" value="true" /> <!-- Generates tables. -->  
  <!-- <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />--> 
  <property name="databasePlatform" value="org.hibernate.dialect.MySQLInnoDBDialect" /> 
  </bean> 
  </property> 
  </bean> 
  <!-- 
  <bean id="dataSource" class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource" destroy-method="close"> 
  <property name="url" value="jdbc:mysql://localhost:3306/database" /> 
  <property name="user" value="username" /> 
  <property name="password" value="123456" /> 
  </bean> 
  --> 
  <!-- ###### JNDI Lookup ###### --> 
  <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean" lazy-init="true"> 
  <property name="jndiName" value="java:comp/env/jdbc/dbmyapp" /> 
  </bean> 
 
 
 
  <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
  <property name="entityManagerFactory" ref="entityManagerFactory"/> 
  <property name="dataSource" ref="dataSource"/> 
  </bean> 
 
 
   
  <context:annotation-config/> 
  <bean id="basicDao" class="ch.devit.dao.service.BasicDao" /> 
 
 
  <bean id="surveyService" class="ch.devit.dao.service.SurveyService" 
  p:basicDao-ref="basicDao"/> 
 
  <bean id="surveyController" class="ch.devit.controller.SurveyController" 
  p:userService-ref="surveyService" scope="prototype" /> 
   
</beans> 
 

Vous devez être identifié pour poster un commentaire.

Liste des blogs

Brutus - Le Blog de Fabrice Bourqui

Le blog de Fabrice Bourqui dit Brutus. Ingénieur en microtechnique de formation, développeur à temps partiel, je vous livre quelques astuces principalement sur les technologies Java et l'OS linux... Merci de votre visite.

Le blog de Fabou dit le Burk ou Brutus

Catégories


Rechercher

<  Janvier 2012  >
Lun Mar Mer Jeu Ven Sam Dim
            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          

Syndiquez ce blog XML

Articles :

Commentaires :

 
 
 
 
Partenaires

Hébergement Web