2017-06-07 37 views
0

JTAを使用し、persistence.xmlを使用せずにデータベースに接続するには、アノテーションのみを使用しますか? そして、JNDIリンクでデータソースを作成するにはどうすればよいですか?JNDIリンク(Wildfly)でデータソースを作成する方法は?

私はWildfly_10のJavaからJNDIリンクがあります:JBossの/データソース/ PostgreDataSource

persistence.xmlのは

`<persistence-unit name="Unit1" transaction-type="JTA"> 
     <jta-data-source>java:jboss/datasources/PostgreDataSource</jta-data-source> 
     <properties> 
      <property name="hibernate.hbm2ddl.auto" value="update"/> 
     </properties> 
    </persistence-unit>` 

私のconfigファイルのスニペット:

@Configuration 
@EnableTransactionManagement 
@ComponentScan({ "net.myProg" }) 
@PropertySource(value = { "classpath:application.properties" }) 
@EnableJpaRepositories("net.myProg.repository") 
public class HibernateConfiguration { 

    @Autowired 
    private Environment environment; 

    @Bean 
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() { 
     LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean(); 
     entityManagerFactoryBean.setDataSource(dataSource()); 
     entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class); 
     entityManagerFactoryBean.setPackagesToScan("net.myProg.model"); 

     entityManagerFactoryBean.setJpaProperties(hibernateProperties()); 

     return entityManagerFactoryBean; 
    } 

    @Bean 
    public JpaTransactionManager transactionManager() { 
     JpaTransactionManager transactionManager = new JpaTransactionManager(); 
     transactionManager.setEntityManagerFactory(entityManagerFactory().getObject()); 

     return transactionManager; 
    } 

    @Bean 
    public DataSource dataSource() { 
     DriverManagerDataSource dataSource = new DriverManagerDataSource(); 
     dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName")); 
//  dataSource.setDriverClassName("org.postgresql.Driver"); 
     dataSource.setUrl(environment.getRequiredProperty("jdbc.url")); 
     dataSource.setUsername(environment.getRequiredProperty("jdbc.username")); 
     dataSource.setPassword(environment.getRequiredProperty("jdbc.password")); 
     return dataSource; 
    } 

    private Properties hibernateProperties() { 
     Properties properties = new Properties(); 
     properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect")); 
     properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql")); 
     properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql")); 
     //Lazy load initializayion. Hibernate сам открывает сессию для получения lazy объекта, если это нужно 
     properties.put("hibernate.enable_lazy_load_no_trans", environment.getRequiredProperty("hibernate.enable_lazy_load_no_trans")); 
//  properties.put("hibernate.hbm2ddl.auto", environment.getRequiredProperty("hibernate.hbm2ddl.auto")); 
     return properties; 
    } 

    @Bean 
    public PersistentTokenRepository persistentTokenRepository() { 
     JdbcTokenRepositoryImpl db = new JdbcTokenRepositoryImpl(); 
     db.setDataSource(dataSource()); 
     return db; 
    } 

} 

答えて

-1

を使用して、データソースのBeanを作成します。 jndi名

<?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:context="http://www.springframework.org/schema/context" 
    xmlns:ehcache="http://www.springmodules.org/schema/ehcache" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:util="http://www.springframework.org/schema/util" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.1.xsd 
     http://www.springmodules.org/schema/ehcache 
     http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd 
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
     http://www.springframework.org/schema/util 
     http://www.springframework.org/schema/util/spring-util-3.1.xsd"> 

    <bean id="appDataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> 
     <property name="jndiName"> 
      <value>java:jboss/datasources/xyzDataSouce</value> 
     </property> 
    </bean> 

</beans> 

あなたは、あなたが2つのファイルを作成することができますしたい場合は、この

<?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:context="http://www.springframework.org/schema/context" 
    xmlns:ehcache="http://www.springmodules.org/schema/ehcache" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:util="http://www.springframework.org/schema/util" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-4.2.xsd 
     http://www.springmodules.org/schema/ehcache 
     http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd 
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx-4.2.xsd 
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
     http://www.springframework.org/schema/data/jpa 
     http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd 
     http://www.springframework.org/schema/util 
     http://www.springframework.org/schema/util/spring-util-4.2.xsd">   

    <tx:annotation-driven transaction-manager="apptransactionManager" /> 

    <bean id="apptransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="appEntityManagerFactory" /> 
    </bean> 

    <bean id="appEntityManagerFactory" 
     class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="jpaVendorAdapter"> 
       <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/> 
     </property> 
     <property name="dataSource" ref="appDataSource" /> 
     <property name="packagesToScan"> 
      <list> 
       <value>net.something.something</value> 
      </list> 
     </property> 
     <property name="jpaPropertyMap"> 
      <props> 
       <prop key="hibernate.show_sql">${jpaVendorAdapter.hibernate.show_sql}</prop> 
       <prop key="hibernate.dialect">${jpaVendorAdapter.hibernate.dialect}</prop> 
       <prop key="hibernate.jdbc.batch_size">${jpaVendorAdapter.hibernate.jdbc.batch_size}</prop> 
       <prop key="hibernate.hbm2ddl.auto">${jpaVendorAdapter.hibernate.hbm2ddl.auto}</prop> 
       <prop key="hibernate.default_schema">${jpaVendorAdapter.hibernate.default_schema}</prop> 
       <prop key="org.hibernate.envers.audit_table_suffix">${jpaVendorAdapter.org.hibernate.envers.audit_table_suffix}</prop> 
       <prop key="org.hibernate.envers.audit_table_prefix">${jpaVendorAdapter.org.hibernate.envers.audit_table_prefix}</prop> 
       <prop key="org.hibernate.envers.revision_field_name">${jpaVendorAdapter.org.hibernate.envers.revision_field_name}</prop> 
       <prop key="org.hibernate.envers.revision_type_field_name">${jpaVendorAdapter.org.hibernate.envers.revision_type_field_name}</prop> 

      </props> 
     </property> 
    </bean> 

    <jpa:repositories base-package="net.something.something.core.dao" transaction-manager-ref="apptransactionManager"/>   

    <!-- Needed for @PersistenceContext annotation --> 
    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> 

    <tx:annotation-driven transaction-manager="apptransactionManager"/> 
    <aop:aspectj-autoproxy/> 

    <!-- JSR-303 bean validation --> 
    <bean id="validator" 
     class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> 
     <!-- <property name="providerClass" value="org.apache.bval.jsr303.ApacheValidationProvider" /> --> 
    </bean> 

</beans> 

のように、このデータソースを使用して、アプリケーション・コンテキストのXMLに、これら2つのファイルを追加することができますJPAリポジトリを使用して、すべてが正常に動作を開始する場合は

+0

これは注釈ではありません。あなたはxml設定ファイルを持っています。 – Adrenal1ne

+0

これはあなたに役立つかもしれませんhttps://stackoverflow.com/questions/19431517/how-to-retrieve-jndi-using-spring-configuration-instead-of-xml-configuration –

+0

良いリンク、thx。 – Adrenal1ne

0

ここHow to use JNDI DataSource provided by Tomcat in Spring?

@Configuration パブリッククラスMainConfig {

... 

@Bean 
DataSource dataSource() { 
    DataSource dataSource = null; 
    JndiTemplate jndi = new JndiTemplate(); 
    try { 
     dataSource = jndi.lookup("java:comp/env/jdbc/yourname", DataSource.class); 
    } catch (NamingException e) { 
     logger.error("NamingException for java:comp/env/jdbc/yourname", e); 
    } 
    return dataSource; 
} 
お答え下さい3210

}

関連する問題