我有兩個項目,一個使用DAO類和模型,另一個休息控制器訪問多張數據源用的JdbcTemplate導致錯誤
項目A:DAO類+型號
項目B:休息控制器
項目A
application.properties:
spring.abcDatasource.url=
spring.abcDatasource.username=
spring.abcDatasource.password=
spring.abcDatasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.xyzDatasource.url=
spring.xyzDatasource.username=
spring.xyzDatasource.password=
spring.xyzDatasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.initialize=false
DBConfiguration.java
@Configuration
public class DBConfiguration {
@Primary
@Bean(name = "abcDS")
@ConfigurationProperties(prefix = "spring.abcDatasource")
public DataSource abcDS() {
return DataSourceBuilder.create().build();
}
@Bean(name = "abcJdbc")
public JdbcTemplate abcJdbcTemplate(@Qualifier("abcDS") DataSource abcDS) {
return new JdbcTemplate(abcDS);
}
@Bean(name = "xyzDS")
@ConfigurationProperties(prefix = "spring.xyzDatasource")
public DataSource xyzDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "xyzJdbc")
public JdbcTemplate ebsJdbcTemplate(@Qualifier("xyzDS") DataSource xyzDatasource) {
return new JdbcTemplate(xyzDatasource);
}
}
AlphaDAO.Java
@Repository
public class AlphaDAO{
@Autowired
@Qualifier("abcJdbc")
private JdbcTemplate abcJdbc;
@Autowired
@Qualifier("xyzJdbc")
private JdbcTemplate xyzJdbc;
SqlParameterSource namedParameters;
public Collection<Alpha> findAll(String owner){
String sql = "SELECT * from alpha where OWNER in (:owner)" ;
NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(abcJdbc.getDataSource());
namedParameters = new MapSqlParameterSource("owner", owner);
List<Alpha> list = namedParameterJdbcTemplate.query(sql,namedParameters,
new BeanPropertyRowMapper(Alpha.class));
return list;
}
項目B休息控制器:
AlphaServiceApplication.java
@SpringBootApplication
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class AlphaServiceApplication extends SpringBootServletInitializer implements WebApplicationInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(AlphaServiceApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(AlphaServiceApplication.class, args);
}
}
AlphaServic eController.java
@RestController
public class AlphaServiceController {
private static final Logger logger = LoggerFactory.getLogger(AlphaServiceController.class);
@Autowired
AlphaDAO dao;
@CrossOrigin(origins = "http://localhost:4200")
@RequestMapping("/alpha")
public Collection<Alpha> index(@RequestBody String owner) {
return dao.findAll(owner);
}
如果我嘗試運行其他控制器我收到錯誤說
應用程序未能啓動
說明:
場道在com.xyz.web.wip.AlphaService.AlphaServiceController中需要一個類型爲'com.xyz.comp.wip.alpha Comp.dao.AlphaDAO'找不到。
操作:
考慮您的配置定義類型的豆 'com.xyz.comp.wip.alphaComp.dao.AlphaDAO'。
我在進行這些更改後收到相同的錯誤 – Jan69