问题
I am newbie to Spring Cloud and wanted to write a simple "hello world" RDS program.
My configuration is stored in the xml config as follows.
<aws-context:context-credentials>
...
</aws-context:context-credentials>
<aws-context:context-region region="ap-southeast-1" />
<jdbc:data-source db-instance-identifier="amazonrds"
password="${password}" read-replica-support="false">
<jdbc:pool-attributes initialSize="1"
defaultCatalog="Records" testOnBorrow="true" validationQuery="SELECT 1" />
</jdbc:data-source>
And I have a simple DB service class which reads like this.
@Service
public class JdbcRecordService implements RecordService {
private final JdbcTemplate jdbcTemplate;
@Autowired
public JdbcRecordService(DataSource datasource) {
this.jdbcTemplate = new JdbcTemplate(datasource);
}
@Override
@Transactional(readOnly = true)
public List<Record> all() {
return this.jdbcTemplate.query("SELECT * FROM Records", new RowMapper<Record>() {
@Override
public Record mapRow(ResultSet resultSet, int rowNum) throws SQLException {
return new Record(resultSet.getString("Name"), resultSet.getString("Value"));
}
});
}
@Override
@Transactional
public void store(Record record) {
this.jdbcTemplate.update("INSERT INTO Records(Name, Value) VALUES (?,?)", record.getKey(), record.getValue());
}
}
And the Application class is like this.
@SpringBootApplication
@EnableAutoConfiguration
@Configuration
@ComponentScan
@ImportResource("classpath:aws-config.xml")
public class SpringBootAwsApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootAwsApplication.class, args);
}
}
I know I am missing some connectors here, but I can't figure out what it is. Currently it throws this error, when building.
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jdbcRecordService' defined in file [C:\Users\codev\gitlab\springaws\target\classes\base\rds\JdbcRecordService.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.apache.tomcat.jdbc.pool.DataSource]: : No qualifying bean of type [org.apache.tomcat.jdbc.pool.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.apache.tomcat.jdbc.pool.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
It basically says, there is no bean candidate that can autowire the construction with a DataSource parameter. What am I missing here?
EDIT:My issue got fixed just by re-ordering the depending in my POM.xml. I still don't know which specific dependency conflict caused this issue but just moving aws-java-sdk
to the top of the dependency list fixed my issues.
来源:https://stackoverflow.com/questions/32136013/spring-cloud-with-rds-hello-world