jndi為什么用的很少了,boot jndi數據源 spring_使用Spring Boot配置JNDI數據源 -Roy教程

 2023-10-02 阅读 28 评论 0

摘要:在這篇文章中,我們將看到如何使用Spring Boot配置JNDI數據源。JNDI數據源與JDBC數據源非常相似。JNDI數據源訪問在應用程序服務器中預定義和配置并作為JNDI資源或服務發布的數據庫連接。無需像使用JDBC數據源那樣指定驅動程序和數據庫,只需在應用程序服務器中指

在這篇文章中,我們將看到如何使用Spring Boot配置JNDI數據源。JNDI數據源與JDBC數據源非常相似。JNDI數據源訪問在應用程序服務器中預定義和配置并作為JNDI資源或服務發布的數據庫連接。無需像使用JDBC數據源那樣指定驅動程序和數據庫,只需在應用程序服務器中指定JNDI資源名稱。當您必須在以下環境之間移動應用程序時,JNDI可以提供幫助:開發->集成->測試->生產。如果將每個應用程序服務器配置為使用相同的JNDI名稱,則每個環境中可以具有不同的數據庫,但無需更改代碼。您只需要在新環境中刪除可部署的WAR文件。

(總體思路:在application.properties中配置JNDI名稱和數據庫連接信息,然后在Spring Boot應用中使用JNDI名稱調用)

Oracle配置:

jndi為什么用的很少了,#datasource

spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver

spring.datasource.url=jdbc:Oracle:thin:@//:/

spring.datasource.username=scott

java resourcebundle?spring.datasource.password=tiger

spring.datasource.jndiName=jdbc/myDataSource

#disable schema generation from Hibernate

spring.jpa.hibernate.ddl-auto=none

jndiobjectfactorybean配置、#DB dialect - override default one

spring.jpa.database-platform=org.hibernate.dialect.Oracle12cDialect

MySQL配置:

#datasource

springboot配置數據源,spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver

spring.datasource.url=jdbc:mysql://localhost/roytuts

spring.datasource.username=root

spring.datasource.password=root

boolquerybuilder用法。spring.datasource.jndiName=jdbc/myDataSource

#disable schema generation from Hibernate

spring.jpa.hibernate.ddl-auto=none

屬性配置類

jndi和jdbc區別,創建DatabaseProperties屬性類,從application.properties文件中加載數據庫連接參數的鍵/值對。

對于Spring Boot版本1.5.9,請使用以下配置:

@Configuration

@EnableTransactionManagement

java使用es條件查詢,@EnableJpaRepositories(basePackages = "com.roytuts.spring.boot.jndi.datasource.repository")

public class AppConfig {

@Bean

public DatabaseProperties databaseProperties() {

return new DatabaseProperties();

}

@Bean

public TomcatEmbeddedServletContainerFactory tomcatFactory() {

return new TomcatEmbeddedServletContainerFactory() {

@Override

protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(Tomcat tomcat) {

tomcat.enableNaming();

return super.getTomcatEmbeddedServletContainer(tomcat);

}

@Override

protected void postProcessContext(Context context) {

ContextResource resource = new ContextResource();

resource.setName(databaseProperties().getJndiName());

resource.setType(DataSource.class.getName());

resource.setProperty("factory", "org.apache.tomcat.jdbc.pool.DataSourceFactory");

resource.setProperty("driverClassName", databaseProperties().getDriverClassName());

resource.setProperty("url", databaseProperties().getUrl());

resource.setProperty("password", databaseProperties().getPassword());

resource.setProperty("username", databaseProperties().getUsername());

context.getNamingResources().addResource(resource);

}

};

}

@Bean(destroyMethod = "")

public DataSource jndiDataSource() throws IllegalArgumentException, NamingException {

JndiObjectFactoryBean bean = new JndiObjectFactoryBean();

bean.setJndiName("java:comp/env/" + databaseProperties().getJndiName());

bean.setProxyInterface(DataSource.class);

bean.setLookupOnStartup(false);

bean.afterPropertiesSet();

return (DataSource) bean.getObject();

}

@Bean

public EntityManagerFactory entityManagerFactory() throws SQLException, IllegalArgumentException, NamingException {

HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();

vendorAdapter.setDatabase(Database.ORACLE);

vendorAdapter.setShowSql(true);

LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();

factory.setJpaVendorAdapter(vendorAdapter);

factory.setPackagesToScan("com.roytuts.spring.boot.jndi.datasource.entity");

factory.setDataSource(jndiDataSource());

factory.afterPropertiesSet();

return factory.getObject();

}

@Bean

public PlatformTransactionManager transactionManager()

throws SQLException, IllegalArgumentException, NamingException {

JpaTransactionManager txManager = new JpaTransactionManager();

txManager.setEntityManagerFactory(entityManagerFactory());

return txManager;

}

}

對于Spring Boot 2.2.1,請使用以下配置文件:

@Configuration

@EnableTransactionManagement

@EnableJpaRepositories(basePackages = "com.roytuts.spring.boot.jndi.datasource.repository")

public class AppConfig {

@Bean

public DatabaseProperties databaseProperties() {

return new DatabaseProperties();

}

@Bean

public TomcatServletWebServerFactory tomcatFactory() {

return new TomcatServletWebServerFactory() {

@Override

protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {

tomcat.enableNaming();

return super.getTomcatWebServer(tomcat);

}

@Override

protected void postProcessContext(Context context) {

ContextResource resource = new ContextResource();

resource.setType("org.apache.tomcat.jdbc.pool.DataSource");

resource.setName(databaseProperties().getJndiName());

resource.setProperty("factory", "org.apache.tomcat.jdbc.pool.DataSourceFactory");

resource.setProperty("driverClassName", databaseProperties().getDriverClassName());

resource.setProperty("url", databaseProperties().getUrl());

resource.setProperty("password", databaseProperties().getUsername());

resource.setProperty("username", databaseProperties().getPassword());

context.getNamingResources().addResource(resource);

}

};

}

@Bean(destroyMethod = "")

public DataSource jndiDataSource() throws IllegalArgumentException, NamingException {

JndiObjectFactoryBean bean = new JndiObjectFactoryBean();

bean.setJndiName("java:comp/env/" + databaseProperties().getJndiName());

bean.setProxyInterface(DataSource.class);

bean.setLookupOnStartup(false);

bean.afterPropertiesSet();

return (DataSource) bean.getObject();

}

@Bean

public EntityManagerFactory entityManagerFactory() throws SQLException, IllegalArgumentException, NamingException {

HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();

vendorAdapter.setDatabase(Database.MYSQL);

vendorAdapter.setShowSql(true);

LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();

factory.setJpaVendorAdapter(vendorAdapter);

factory.setPackagesToScan("com.roytuts.spring.boot.jndi.datasource.entity");

factory.setDataSource(jndiDataSource());

factory.afterPropertiesSet();

return factory.getObject();

}

@Bean

public PlatformTransactionManager transactionManager()

throws SQLException, IllegalArgumentException, NamingException {

JpaTransactionManager txManager = new JpaTransactionManager();

txManager.setEntityManagerFactory(entityManagerFactory());

return txManager;

}

}

[b]詳細點擊標題見原文[/b]

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/109954.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息