@Configuration
@ComponentScan(basePackages = {"com.test"})
@EnableJpaRepositories(basePackages = {"com.test.jpa.repository"})
@EnableAutoConfiguration
@EnableTransactionManagement
public class AppConfig {
@Bean(name = "dataSource1")
DataSource dataSource1(@Value("${db1.driverClassName}") String db1DriverClassName, @Value("${db1.url}") String db1Url, @Value("${db1.username}") String db1Username, @Value("${db1.password}") String db1Password) {
DataSource dataSource = new DataSource();
dataSource.setDriverClassName(db1DriverClassName);
dataSource.setUrl(db1Url);
dataSource.setUsername(db1Username);
dataSource.setPassword(db1Password);
return dataSource;
}
@Bean(name = "dataSource2")
DataSource dataSource2(@Value("${db2.driverClassName}") String db2DriverClassName, @Value("${db2.url}") String db2Url, @Value("${db2.username}") String db2Username, @Value("${db2.password}") String db2Password) {
DataSource dataSource = new DataSource();
dataSource.setDriverClassName(db2DriverClassName);
dataSource.setUrl(db2Url);
dataSource.setUsername(db2Username);
dataSource.setPassword(db2Password);
return dataSource;
}
@Bean(name = "jpaVendorAdapter")
HibernateJpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setDatabase(Database.ORACLE);
jpaVendorAdapter.setShowSql(false);
jpaVendorAdapter.setDatabasePlatform("org.hibernate.dialect.Oracle10gDialect");
return jpaVendorAdapter;
}
@Bean(name = "entityManagerFactory")
@Autowired
LocalContainerEntityManagerFactoryBean entityManagerFactory(@Qualifier("dataSource1") DataSource dataSource, HibernateJpaVendorAdapter jpaVendorAdapter) {
LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
entityManagerFactory.setDataSource(dataSource);
entityManagerFactory.setPersistenceXmlLocation("classpath:persistence.xml");
entityManagerFactory.setPersistenceUnitName("springdatajpa");
entityManagerFactory.setJpaVendorAdapter(jpaVendorAdapter);
return entityManagerFactory;
}
@Bean(name = "transactionManager")
JpaTransactionManager transactionManager(LocalContainerEntityManagerFactoryBean entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory.getObject());
return transactionManager;
}
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration.dataSource; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2: dataSource1,dataSource2
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:120)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:648)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:311)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:909)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:898)
at com.wth.service.logging.Application.main(Application.java:11)
谢谢,克里斯
为了使其工作,我使用了以下方法:
@SpringBootApplication(exclude = {
DataSourceAutoConfiguration.class,
HibernateJpaAutoConfiguration.class,
JpaRepositoriesAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class})
public class App...
这相当于:
@Configuration
@ComponentScan
@EnableAutoConfiguration(exclude = {
DataSourceAutoConfiguration.class,
HibernateJpaAutoConfiguration.class,
JpaRepositoriesAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class})
然后,我为每个数据源定义了一个单独的配置类,其定义如下:
@Configuration
@EnableJpaRepositories(
entityManagerFactoryRef="fooEntityManagerFactory",
transactionManagerRef="fooTransactionManager")
public class FooConfig {
public static final String FOO_QUALIFIER = "Foo";
@Bean @Qualifier(FOO_QUALIFIER)
public DataSource fooDataSource() { ... }
@Bean @Qualifier(FOO_QUALIFIER)
public EntityManagerFactory fooEntityManagerFactory() { ... }
@Bean @Qualifier(FOO_QUALIFIER)
public PlatformTransactionManager fooTransactionManager(
@Qualifier(FOO_QUALIFIER) EntityManagerFactory emf) { ... }
}
问题内容: 我有两个名为simple-core-impl和的项目simple-core-web。 这两个项目都是,spring based并且都具有一个父项目名称simple-core。 我simple-impl-config.xml在simple-core-impl项目simple-web-config.xml中simple-impl-config.xml。 我有一个具有类的bean:simpl
在能够根据add custom data source to Jaspersoft Studio将自定义数据源从java bean添加到报表中之后,我将进入使用Jasper报告的第二点。 我有一个主要的报告,它使用一个数据库作为它的数据源。然后,我将一个bean.xml数据源添加到报表中,并将一个表添加到主报表中,主报表使用这个bean.xml数据源来获取java Beans。 我的目标是从主报表
我试图创建一个Spring Boot应用程序,它连接两个数据源。我可以通过遵循Spring文档来实现这一点,但我面临的挑战是实现以下目标 null 是否有任何方法可以使用Spring Boot的自动配置功能来实现这一点,或者我必须基于概要文件创建不同的数据源bean。
问题内容: 我有一个运行Spring MVC的应用程序。 我需要它来访问我的应用程序中的2个不同的数据库(一个是PostgreSQL,另一个是MySQL数据库)。 如何仅使用批注或application.properties文件进行配置? 问题答案: 这是示例代码,希望对你有所帮助! application.properties DatabaseItemsConfig.java DatabaseU
类型异常报告 消息请求处理失败;嵌套异常是java.lang.NullPointerException 说明服务器遇到内部错误,导致它无法完成此请求。 例外情况 org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常为java.lang.nullpointerexception org.springframework.web.
问题内容: 我正在尝试将Django多数据库配置与MYSQL用作默认数据库,将redshift用作分析数据库。我的配置看起来像这样: 当我尝试迁移分析应用程序时,请使用以下命令 我看到以下错误: 关于如何解决此问题的任何想法?我正在使用和 问题答案: 特定的问题是:Django想要创建一个具有主键的迁移管理表来跟踪迁移历史。Redshift不支持。 但是,这种方法更普遍的问题是,您真的不希望在Re