Property ‘dataSource’ is required in Spring, Java

huangapple go评论125阅读模式
英文:

Property 'dataSource' is required in Spring, Java

问题

我刚开始学习Spring,现在我尝试创建基于Spring JDBC的DAO应用程序。
我按照以下方式创建了配置类:

  1. @Configuration
  2. @ComponentScan("com.foxminded.university")
  3. public class SpringJdbcConfig {
  4. @Bean
  5. public DataSource dataSource() {
  6. DriverManagerDataSource dataSource = new DriverManagerDataSource();
  7. dataSource.setDriverClassName("org.postgresql.Driver");
  8. dataSource.setUrl("jdbc:postgresql://127.0.0.1:5432/university");
  9. dataSource.setUsername("maintainer");
  10. dataSource.setPassword("12345678");
  11. return dataSource;
  12. }
  13. }

DAO类使用了这个Bean:

  1. @Component
  2. public class BuildingDao implements Dao<Building> {
  3. @Autowired
  4. private DataSource dataSource;
  5. private final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
  6. private static final String SAVE_BUILDING = "Insert into buildings (name, floors) values (?,?)";
  7. @Override
  8. public void save(Building building) {
  9. jdbcTemplate.update(SAVE_BUILDING, building.getName(), building.getFloors());
  10. }
  11. }

但是当我尝试运行这个查询时,我得到了以下错误:

  1. Exception in thread "main" java.lang.IllegalArgumentException: Property 'dataSource' is required

我该如何修复这个问题?据我所见,我使用了@Autowire错误,因为当我使用以下代码时,一切正常运行:

  1. private DataSource dataSource = new SpringJdbcConfig().dataSource();

但这是多余的关系,也是IoC的错误用法。

顺便说一下,在主函数中,我还必须以以下方式使用它:

  1. public class Main {
  2. public static void main(String[] args) {
  3. Building building = new SpringJdbcConfig().building();
  4. building.setName("hghgf");
  5. building.setFloors(2);
  6. BuildingDao buildingDao = new SpringJdbcConfig().buildingDao();
  7. buildingDao.save(building);
  8. }
  9. }

如果您能解释如何正确使用@Autowired,并将Bean注入主类,我将非常感谢。

英文:

I just started learning Spring, and now I try to crate Spring JDBC based DAO application.
I created config class in this way

  1. @Configuration
  2. @ComponentScan(&quot;com.foxminded.university&quot;)
  3. public class SpringJdbcConfig {
  4. @Bean
  5. public DataSource dataSource() {
  6. DriverManagerDataSource dataSource = new DriverManagerDataSource();
  7. dataSource.setDriverClassName(&quot;org.postgresql.Driver&quot;);
  8. dataSource.setUrl(&quot;jdbc:postgresql://127.0.0.1:5432/university&quot;);
  9. dataSource.setUsername(&quot;maintainer&quot;);
  10. dataSource.setPassword(&quot;12345678&quot;);
  11. return dataSource;
  12. }
  13. }

And dao-class uses this bean

  1. @Component
  2. public class BuildingDao implements Dao&lt;Building&gt; {
  3. @Autowired
  4. private DataSource dataSource;
  5. private final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
  6. private static final String SAVE_BUILDING = &quot;Insert into buildings (name, floors) values (?,?)&quot;;
  7. @Override
  8. public void save(Building building) {
  9. jdbcTemplate.update(SAVE_BUILDING, building.getName(), building.getFloors());
  10. }
  11. }

But when I try to run this query i get

  1. Exception in thread &quot;main&quot; java.lang.IllegalArgumentException: Property &#39;dataSource&#39; is required

How I can fix it? As I can see, I use @Autowired incorrectly, because everything works fine when I use

  1. private DataSource dataSource = new SpringJdbcConfig().dataSource();

But it is extra relation and mistake in terms of IoC.

By the way in main I also have to use this in this way

  1. public class Main {
  2. public static void main(String[] args) {
  3. Building building = new SpringJdbcConfig().building();
  4. building.setName(&quot;hghgf&quot;);
  5. building.setFloors(2);
  6. BuildingDao buildingDao = new SpringJdbcConfig().buildingDao();
  7. buildingDao.save(building);
  8. }
  9. }

I would be very grateful if you could explain how to use @autowired correctly and inject beans into the main class.

答案1

得分: 1

我建议您按照以下方式使用 Spring Boot 配置您的应用程序。这将初始化并自动配置您大部分的需求。

  1. @SpringBootApplication
  2. @EnableAutoConfiguration
  3. @ComponentScan("com.foxminded.university")
  4. public class SpringBootWebApp {
  5. public static void main(String[] args) {
  6. ConfigurableApplicationContext context = SpringApplication.run(SpringBootWebApp.class, args);
  7. context.registerShutdownHook();
  8. }
  9. }

之后,您可以使用 @Autowired 来注入所有那些您配置的 Spring 托管的 bean。

英文:

I would suggest you to use spring boot to configure your application as below. This will initialize and auto-configure most of your needs.

  1. @SpringBootApplication
  2. @EnableAutoConfiguration
  3. @ComponentScan(&quot;com.foxminded.university&quot;)
  4. public class SpringBootWebApp {
  5. public static void main(String[] args) {
  6. ConfigurableApplicationContext context = SpringApplication.run(SpringBootWebApp.class, args);
  7. context.registerShutdownHook();
  8. }
  9. }

After this, you can use @Autowire for all those spring managed beans you configure.

答案2

得分: 0

使用如下方式编写ComponentScan属性,并确保SpringJdbcConfig类位于包com.foxminded.university下:

  1. @ComponentScan(basePackages = "com.foxminded.university")
英文:

Write the ComponentScan attribute as follows and make sure SpringJdbcConfig class is under the package com.foxminded.university*

  1. @ComponentScan(basePackages = &quot;com.foxminded.university&quot;)

huangapple
  • 本文由 发表于 2020年9月22日 00:39:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63996538.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定