Property ‘dataSource’ is required in Spring, Java

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

Property 'dataSource' is required in Spring, Java

问题

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

@Configuration
@ComponentScan("com.foxminded.university")
public class SpringJdbcConfig {

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("org.postgresql.Driver");
        dataSource.setUrl("jdbc:postgresql://127.0.0.1:5432/university");
        dataSource.setUsername("maintainer");
        dataSource.setPassword("12345678");
        return dataSource;
    }
}

DAO类使用了这个Bean:

@Component
public class BuildingDao implements Dao<Building> {
    
    @Autowired
    private DataSource dataSource;
    private final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    
    private static final String SAVE_BUILDING = "Insert into buildings (name, floors) values (?,?)";

    @Override
    public void save(Building building) {
        jdbcTemplate.update(SAVE_BUILDING, building.getName(), building.getFloors());
    }
}

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

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

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

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

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

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

public class Main {
    public static void main(String[] args) {
        Building building = new SpringJdbcConfig().building();
        building.setName("hghgf");
        building.setFloors(2);
        BuildingDao buildingDao = new SpringJdbcConfig().buildingDao();
        buildingDao.save(building);
    }
}

如果您能解释如何正确使用@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

@Configuration
@ComponentScan(&quot;com.foxminded.university&quot;)
public class SpringJdbcConfig {

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(&quot;org.postgresql.Driver&quot;);
        dataSource.setUrl(&quot;jdbc:postgresql://127.0.0.1:5432/university&quot;);
        dataSource.setUsername(&quot;maintainer&quot;);
        dataSource.setPassword(&quot;12345678&quot;);
        return dataSource;
    }
 }

And dao-class uses this bean

@Component
public class BuildingDao implements Dao&lt;Building&gt; {
    
    @Autowired
    private DataSource dataSource;
    private final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    
    private static final String SAVE_BUILDING = &quot;Insert into buildings (name, floors) values (?,?)&quot;;

    @Override
    public void save(Building building) {
        jdbcTemplate.update(SAVE_BUILDING, building.getName(), building.getFloors());
    }
}

But when I try to run this query i get

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

    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

public class Main {
    public static void main(String[] args) {
        Building building = new SpringJdbcConfig().building();
        building.setName(&quot;hghgf&quot;);
        building.setFloors(2);
        BuildingDao buildingDao = new SpringJdbcConfig().buildingDao();
        buildingDao.save(building);
    }
}

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 配置您的应用程序。这将初始化并自动配置您大部分的需求。

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan("com.foxminded.university")
public class SpringBootWebApp {
  public static void main(String[] args) {
    ConfigurableApplicationContext context = SpringApplication.run(SpringBootWebApp.class, args);
    context.registerShutdownHook();
  }
}

之后,您可以使用 @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.

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan(&quot;com.foxminded.university&quot;)
public class SpringBootWebApp {
  public static void main(String[] args) {
    ConfigurableApplicationContext context = SpringApplication.run(SpringBootWebApp.class, args);
    context.registerShutdownHook();
  }
}

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

答案2

得分: 0

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

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

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

@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:

确定