英文:
How does Spring deal with @Autowired on fields that required a parameter? In this case, JdbcTemplate with DataSource
问题
我有一个SpringBoot应用程序,我想要连接到我的MySQL数据库,并且我想使用JDBC(不使用JPA)来连接它。从我在文章中看到的内容来看,实现这一目标的一种方式是使用JdbcTemplate和DataSource对象。
现在我有一个RestController,在其中调用我的数据库,“CoffeeShop”,它有以下类/代码:
@RestController
public class MenuController {
@Autowired
private DataSource dataSource;
@Autowired
private JdbcTemplate jdbcTemplate;
private String menuQuery = "SELECT * FROM menu";
@CrossOrigin(origins = "http://localhost:4200")
@GetMapping(path="/menu")
public String getMenu(){
jdbcTemplate.query(menuQuery, (rs, rowNum) -> new Menu(rs.getString("name"))).forEach(
customer -> System.out.println(customer.getName()));
return "worked";
}
private List<Menu> organizeMenu() {
return null;
}
}
如果我的理解是正确的,我期望dataSource能够在我的application.properties文件中看到以下内容,然后Spring会自动识别到jdbcTemplate需要它?:
spring.jpa.hibernate.ddl-auto=none
spring.datasource.driverclassname = com.mysql.jdbc.Driver
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/CoffeeShop?useUnicode=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=springuser
spring.datasource.password=ThePassword
令我惊讶的是,这个工作了,我的代码查询了数据库并记录了正确的输出。但我不确定这是如何工作的,因为jdbcTemplate需要dataSource?
英文:
I have a SpringBoot app that I want to connect to my MySQL database and I want to connect it with JDBC (by itself, not using JPA). And from what I have seen on articles, one way to achieve this is with JdbcTemplate and DataSource objects).
Now I have a RestController where I call my database, "CoffeeShop" which has me with the following class/code:
@RestController
public class MenuController {
@Autowired
private DataSource dataSource;
@Autowired
private JdbcTemplate jdbcTemplate;
private String menuQuery = "SELECT * FROM menu";
@CrossOrigin(origins = "http://localhost:4200")
@GetMapping(path="/menu")
public String getMenu(){
jdbcTemplate.query(menuQuery, (rs, rowNum) -> new Menu(rs.getString("name"))).forEach(
customer-> System.out.println(customer.getName()));
return "worked";
}
private List<Menu> organizeMenu() {
return null;
}
}
If my understanding is correct, I expect that dataSource will be able to see in my application.properties file the following contents when being compiled and then Spring figures out that jdbcTemplate requires it?:
spring.jpa.hibernate.ddl-auto=none
spring.datasource.driverclassname = com.mysql.jdbc.Driver
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/CoffeeShop?useUnicode=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=springuser
spring.datasource.password=ThePassword
To my surprise this worked, my code queried the DB and logged the correct output. But I'm not sure how this worked since jdbcTemplate required dataSource?
答案1
得分: 4
查看Spring Boot Getting Started | Accessing Relational Data using JDBC with Spring指南,其中提到:
> Spring Boot支持H2(内存关系数据库引擎)并自动创建连接。因为我们使用spring-jdbc,Spring Boot会自动创建JdbcTemplate。@Autowired JdbcTemplate字段会自动加载它并使其可用。
这是Spring Boot提供的自动配置:从application.properties文件中自动创建和配置的完全功能的JdbcTemplate。
FYI: JdbcTemplate已经配置为使用DataSource,所以您不需要自动装配DataSource。正如您在自己的代码中所看到的,dataSource字段没有在任何地方使用,因此应该将其删除。
实际上,DataSource是从application.properties文件中自动配置的。
英文:
See the Spring Boot Getting Started | Accessing Relational Data using JDBC with Spring guide, which says:
> Spring Boot supports H2 (an in-memory relational database engine) and automatically creates a connection. Because we use spring-jdbc, Spring Boot automatically creates a JdbcTemplate. The @Autowired JdbcTemplate field automatically loads it and makes it available.
This is what you get with the auto-configuration provided by Spring Boot: Fully functional JdbcTemplate automatically created and configured from the application.properties file.
FYI: The JdbcTemplate is already configured to use the DataSource, so you don't need to auto-wire the DataSource. As you can see in your own code, the dataSource field isn't used anywhere, so you should remove it.
It is actually the DataSource that is auto-configured from the application.properties file.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论