繁体   English   中英

JdbcTemlate - 没有可用类型的合格 bean

[英]JdbcTemlate - No qualifying bean of type available

我想在 xml JdbcTemlate 上进行配置。

它看起来像这样

<context:property-placeholder location="classpath:db/postgres.properties"/>

<bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${database.driverClassName}"/>
    <property name="url" value="${database.url}"/>
    <property name="username" value="${database.username}"/>
    <property name="password" value="${database.password}"/>
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <constructor-arg ref="dataSource"/>
</bean>


<bean id="namedJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
    <constructor-arg ref="dataSource"/>
</bean>

但我收到这样的错误

引起:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“jdbcMealServiceImpl”的bean时出错:通过字段“jdbcMealRepository”表达的不满意依赖; 嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用的“ru.demo.exercise.repository.jdbc.JdbcMealRepository”类型的合格 bean:预计至少有 1 个 bean 有资格作为自动装配候选。 依赖注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)} 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:639) 在 org.springframework. beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:116) 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:397) 在 org.springframework.beans.Abfactory.BeanAutowiresupport. populateBean(AbstractAutowireCapableBeanFactory.java:1429) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createWireBean1.AutowireCapableBeanFactory.createjava5(AbstractAutowireCapableBeanFactory.createWireBean)在 org.springframework.beans.factory.support.AbstractBeanFactory.lambda$do GetBean$0(AbstractBeanFactory.java:323) 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(Abstract21BeanFactory.java:322) ) 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) 在 org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) 在 org.springframework.beans。 factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1287) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207) at org.springframework.beans.beans.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207) at org.springframework.beans.beans.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207)注入(AutowiredAnnotationBeanPostProcessor.java:636) ... 61个常见的框架被省略了引起:org.springframework.beans.factory.NoSuchBeanDefinition 例外:没有可用的“ru.demo.exercise.repository.jdbc.JdbcMealRepository”类型的合格 bean:预计至少有 1 个 bean 有资格作为自动装配候选。 依赖注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)} 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1695) 在 org.springframework.beans。 factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1253) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207) at org.springframework.beans.beans.beans.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207)注入(AutowiredAnnotationBeanPostProcessor.java:636) ...省略了74个常见帧

这是我的 JdbcService:

@Service
public class JdbcMealServiceImpl implements MealService {
    @Autowired
    JdbcMealRepository jdbcMealRepository;

    @Override
    public Meal save(Meal meal) {
        return jdbcMealRepository.save(meal);
    }

    @Override
    public Meal get(int id) {
        return jdbcMealRepository.get(id);
    }

    @Override
    public List<Meal> getAll() {
        return jdbcMealRepository.getAll();
    }

    @Override
    public void delete(int id) {
        jdbcMealRepository.delete(id);
    }

    @Override
    public void update(Meal meal) {
        jdbcMealRepository.update(meal);
    }
}

Jdbc 存储库:

@Repository
public abstract class JdbcMealRepository implements MealRepository {

    private static final BeanPropertyRowMapper<Meal> ROW_MAPPER = BeanPropertyRowMapper.newInstance(Meal.class);

    @Autowired
    private final JdbcTemplate jdbcTemplate;

    private final SimpleJdbcInsert insertMeals;

    @Autowired
    private final NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Autowired
    protected JdbcMealRepository(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
        this.insertMeals = new SimpleJdbcInsert(jdbcTemplate)
                .withTableName("meals")
                .usingGeneratedKeyColumns("id");

        this.jdbcTemplate = jdbcTemplate;
        this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
    }

    @Override
    public Meal save(Meal meal) {
        MapSqlParameterSource map = new MapSqlParameterSource()
                .addValue("id", meal.getId())
                .addValue("date_time", meal.getDatetime())
                .addValue("description", meal.getDatetime())
                .addValue("calories", meal.getCalories());

        namedParameterJdbcTemplate.update("INSERT INTO meals(id, date_time, description, calories) " +
                "VALUES(id=:id, date_time=:date_time, description=:description, calories=:calories)", map);

        return meal;
    }

}

控制器:

@Controller
public class JspMealController {
    @Autowired
    JdbcMealServiceImpl mealService;

    @GetMapping(value = "/list")
    public String getAll(Model model) {
        model.addAttribute("meals", mealService.getAll());
        return "meals";
    }

    @GetMapping(value = "/createForm")
    public String addForm(Model model) {
        Meal meal = new Meal();
        model.addAttribute("mealsCreate", meal);

        return "createmealForm";
    }

    @PostMapping(value = "/create")
    public String save(@ModelAttribute("mealsCreate") Meal meal) {
        mealService.save(meal);
        return "redirect:/list";
    }

    @GetMapping(value = "/delete")
    public String deleteMeal(@RequestParam("mealId") int id) {
        mealService.delete(id);
        return "redirect:/list";
    }

    @GetMapping(value = "/updateForm")
    public String updateForm(@RequestParam("mealId") int id, Model model) {
        Meal meal = mealService.get(id);
        model.addAttribute("mealsUpdate", meal);

        return "updatemealForm";
    }

    @PostMapping(value = "/update")
    public String update(@ModelAttribute("mealsUpdate") Meal meal) {
        mealService.update(meal);
        return "redirect:/list";
    }
}

JdbcMealRepository 不应该是抽象类。 我们必须提供一个具体的实现类。 尝试从 JdbcMealRepository 类中删除抽象关键字。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM