我有两个项目,一个具有DAO类和Model,另一个具有Rest Controller
项目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);
}
}
Java的AlphaDAO
@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);
}
}
AlphaServiceController.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);
}
如果我尝试运行rest控制器,则会收到错误提示
申请启动失败
描述:
com.xyz.web.wip.AlphaService.AlphaServiceController中的字段dao需要找不到类型为“ com.xyz.comp.wip.alphaComp.dao.AlphaDAO”的bean。
行动:
考虑在配置中定义类型为“ com.xyz.comp.wip.alphaComp.dao.AlphaDAO”的bean。
最佳答案
您的AlphaDao
类没有多大意义,您正在尝试自动连接两个字段,但仍然有一个构造函数。
Spring无法构建对象,因为构造函数上没有限定符。
您既可以进行构造函数注入,也可以进行字段注入,但不要同时使用两者。
我建议使用构造函数注入。
@Repository
public class AlphaDAO{
private final JdbcTemplate abcJdbc;
private final JdbcTemplate xyzJdbc;
@Autowired
public AlphaDAO(
@Qualifier("abcJdbc") JdbcTemplate abcJdbc,
@Qualifier("xyzJdbc") JdbcTemplate xyzJdbc){
this.abcJdbc = abcJdbc;
this.xyzJdbc = xyzJdbc;
}
同时从控制器中删除您的
@Bean
方法。