@Autowired注解引入server服务层方法报错 使用@Autowired注解引入server服务层方法时报错的解决

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

@Autowired注解引入server服务层方法报错 使用@Autowired注解引入server服务层方法时报错的解决

星辰志   2021-11-05 我要评论
想了解使用@Autowired注解引入server服务层方法时报错的解决的相关内容吗,星辰志在本文为您仔细讲解@Autowired注解引入server服务层方法报错的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:@Autowired注解,引入server服务层,下面大家一起来学习吧。

@Autowired注解引入server服务层方法时报错

contentTypeService in com.example.demo001.controller.ContentTypeController required a bean of type 'com.example.demo001.service.ContentTypeService' that could not be found

网上搜的方法:还行

方式一:@Autowried(required = false)设置required属性值为false,错误消失

方式二:用@Resource注解替换@Autowried注解,错误消失

JavaBean是特殊的Java类、使用Java语言书写,并且遵守JavaBean API规范。JavaBean与其他Java类相比而言独一无二的特征是:

1、提供一个默认的无参构造函数。

2、需要序列化并且实现了Serializable接口,

3、可能有一系列可读性属性,

4、可能有一系列的getter或setter方法。

JavaBean属性

一个JavaBean对象的属性应该是可访问的,这个属性可以是任意合法的Java数据类型,包括自定义Java类。

一个JavaBean对象的属性可以是可读写,或只读,或只写。JavaBean对象的属性通过JavaBean实现类中提供的两个方法来访问

方法

  • getPropertyName():举例来说,如果属性的名称为myName,那么这个方法的名字就要写成getMyName()来读取这个属性。这个方法也被称为访问器。
  • setPropertyName():举例来说,如果属性的名称为myName,那么这个方法的名字就要写成setMyName()来写入这个属性。这个方法也被称为写入器。

一个只读的属性只提供getPropertyName()方法,一个只写的属性只提供setPropertyName()方法。

@Autowried的作用是什么?

@Autowried是一个注释,它可以对类成员变量、方法及构造函数进行标注、让spring完成bean自动装配的工作。

@Autowried默认是按照类去匹配,配合@Qualifier指定按照名称去装配bean

错误真正原因是在serveImpl中并未导入

问题得到解决,附上网上解决问题的网址,以上哪两种方法也能用暂不清楚是怎么回事

只是导读

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
 
    <bean id="..." class="...">  
        <!-- collaborators and configuration for this bean go here -->
    </bean>
 
    <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>
 
    <!-- more bean definitions go here -->
 
</beans>

上述是原始的xml文件,如果此时的 <bean id=" " class=" "></bean>就相当于注释的 @Component而 @Service是@Component衍生,而autowire在xml是bean的属性,所以只有@Component存在@Autowire才有效

    <!-- byName会自动再容器上下文中查找,何自己对象set方法后面的值是对立的beanid -->
    <!-- byType必须保证类型全局唯一会自动再容器上下文中查找,和自己对象set方法后面的值是对立的bean -->
       <bean id="people" class="com.chen.pojo.People" autowire="byType">
           <property name="name" value="天明"></property>
            <property name="dog" ref="dog"></property>
          <property name="cat" ref="cat"></property>
       </bean>

关于@Autowired 注解时发生的错误

1. Field injetion is not recommended

2. spring boot自动注入出现Consider defining a bean of type ‘xxx' in your configuration问题

1.解决

原来代码

	@Autowired
	private final AccountDao accountDao;

报错:Field injetion is not recommended

改为

    //final 非必要
	private final AccountDao accountDao;
    @Autowired
    public AccountController(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

错误消失

注:还是警告的话,直接将@Autowired注解改为@Resource注解可以消除警告(不影响注入)

2.解决

原来将account 也就是entity 和dao 放在了controller的上层目录里

报错:Consider defining a bean of type ‘xxx' in your configuration

把entity和dao放在controler同级目录或者我放在了同级目录所在account包

如下图所示

在这里插入图片描述

重新启动,错误消失~

以上为个人经验,希望能给大家一个参考,也希望大家多多支持.

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们