springboot使用@@Configuration加载多数据源的applicationcontext.xml

TJkaklf 2018-07-19 07:54:43

刚学习springboot,加载多数据源的applicationcontext.xml时出现了这个问题,在bean中也加入了@Qualifier,为什么还会出这样的问题
...全文
735 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
TJkaklf 2018-07-20
  • 打赏
  • 举报
回复
引用 1 楼 zssazrael 的回复:
清理并重新编译,如果是用模块的方式嵌入其它模块,那么可能需要清理、调整其它模块的依赖。
不行,应该是springboot对bean注入的问题,
<bean id="jdbcTemplateWrite" class="org.springframework.jdbc.core.JdbcTemplate">
<qualifier value="dataSourceWrite"/>
</bean>
将配置文件的jdbcTemplateWrite改成这样不报datasource的错误了,现在的错误是下面这样的:
===============================================================================
Parameter 0 of constructor in work.dao.impl.SqlDaoImpl required a bean of type 'org.springframework.jdbc.core.JdbcTemplate' that could not be found.
- Bean method 'jdbcTemplate' in 'JdbcTemplateAutoConfiguration' not loaded because @ConditionalOnMissingBean (types: org.springframework.jdbc.core.JdbcOperations; SearchStrategy: all) found beans 'jdbcTemplateOnlyRead', 'jdbcTemplateAdminSelf', 'jdbcTemplateHistoryWrite', 'jdbcTemplateWriteMysql', 'jdbcTemplateRead', 'jdbcTemplateReadMysql', 'jdbcTemplateHistoryRead', 'jdbcTemplateWrite'

幽饮烛 2018-07-20
  • 打赏
  • 举报
回复
清理并重新编译,如果是用模块的方式嵌入其它模块,那么可能需要清理、调整其它模块的依赖。
TJkaklf 2018-07-20
  • 打赏
  • 举报
回复
引用 10 楼 x324235r325 的回复:
看你xml .....
xml里有jdbcTemplate的bean,我把它注销掉,在ConfigClass里配置,是可以的,看来springboot不能再xml中读取jdbcTemplate,
Finley-Ning 2018-07-20
  • 打赏
  • 举报
回复
看你xml .....
TJkaklf 2018-07-20
  • 打赏
  • 举报
回复
引用 6 楼 welan123123 的回复:
去掉之后你要更新啊,或者直接从buidpath里去掉就好了

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
这个依赖可以删掉,但是buildpath中包不能删,因为@SpringBootApplication这个注解就是这个包里的,删了怎么启动微服务?
TJkaklf 2018-07-20
  • 打赏
  • 举报
回复

引用 6 楼 welan123123 的回复:
去掉之后你要更新啊,或者直接从buidpath里去掉就好了
奥,我再试试
TJkaklf 2018-07-20
  • 打赏
  • 举报
回复
问题没解决,换了一种方式.springboot不能再xml中配置JdbcTemplate吗??不希望对数据加载方式有太多的更改,如果有大神知道怎么做,请指教
奔跑的小鱼儿 2018-07-20
  • 打赏
  • 举报
回复
去掉之后你要更新啊,或者直接从buidpath里去掉就好了
TJkaklf 2018-07-20
  • 打赏
  • 举报
回复
package org.springframework.boot.autoconfigure.jdbc;

import javax.sql.DataSource;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

@Configuration
@ConditionalOnClass({DataSource.class, JdbcTemplate.class})
@ConditionalOnSingleCandidate(DataSource.class)
@AutoConfigureAfter({DataSourceAutoConfiguration.class})
public class JdbcTemplateAutoConfiguration {
private final DataSource dataSource;

public JdbcTemplateAutoConfiguration(DataSource dataSource) {
this.dataSource = dataSource;
}

@Bean
@Primary
@ConditionalOnMissingBean({JdbcOperations.class})
public JdbcTemplate jdbcTemplate() {
return new JdbcTemplate(this.dataSource);
}

@Bean
@Primary
@ConditionalOnMissingBean({NamedParameterJdbcOperations.class})
public NamedParameterJdbcTemplate namedParameterJdbcTemplate() {
return new NamedParameterJdbcTemplate(this.dataSource);
}
}

我现在不想用springboot自带的数据源加载的方法,用我自己xml中的datasources和jdbctemplate,
在pom文件中我去掉了
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
依赖,但是没屌用!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
TJkaklf 2018-07-20
  • 打赏
  • 举报
回复
引用 3 楼 welan123123 的回复:
@ConditionalOnMissingBean 仅仅在当前上下文中不存在某个对象时,才会实例化一个Bean
我知道这个注解什么意思了
底层中:
@Configuration
@ConditionalOnClass({DataSource.class, JdbcTemplate.class})
@ConditionalOnSingleCandidate(DataSource.class)
@AutoConfigureAfter({DataSourceAutoConfiguration.class})
public class JdbcTemplateAutoConfiguration {
private final DataSource dataSource;

public JdbcTemplateAutoConfiguration(DataSource dataSource) {
this.dataSource = dataSource;
}

@Bean
@Primary
@ConditionalOnMissingBean({JdbcOperations.class})
public JdbcTemplate jdbcTemplate() {
return new JdbcTemplate(this.dataSource);
}

JdbcTemplateAutoConfiguration的jdbcTemplate上有这个注解,当读取到有jdbcoperations时就不执行这个方法,我现在不想要springboot自动注入的这个jdbcTemplate,用我xml中配置的8个jdbcTemplate,怎么才能忽略它
奔跑的小鱼儿 2018-07-20
  • 打赏
  • 举报
回复
@ConditionalOnMissingBean 仅仅在当前上下文中不存在某个对象时,才会实例化一个Bean

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧