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


这篇文章给大家介绍使用@Autowired注解引入server服务层方法时报错的解决方法,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

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

contentTypeService in.example.demo001.controller.ContentTypeController required a bean of type &#39.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="springframework.org/schema/beans"    xmlns:xsi="w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="springframework.org/schema/beans        https://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>就相当于注释的 ponent而 @Service是ponent衍生,而autowire在xml是bean的属性,所以只有ponent存在@Autowire才有效

<!-- byName会自动再容器上下文中查找,何自己对象set方法后面的值是对立的beanid -->    <!-- byType必须保证类型全局唯一会自动再容器上下文中查找,和自己对象set方法后面的值是对立的bean -->       <bean id="people" class=&quot.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 rmended

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

1.解决

原来代码

@Autowiredprivate final AccountDao accountDao;

报错:Field injetion is not rmended

改为

//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包

如下图所示

重新启动,错误消失~

关于使用@Autowired注解引入server服务层方法时报错的解决方法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


上一篇:Linux中快速检测端口的小技巧有哪些

下一篇:Java 画时钟遇到的问题及解决方法


Copyright © 2002-2019 测速网 www.inhv.cn 皖ICP备2023010105号
测速城市 测速地区 测速街道 网速测试城市 网速测试地区 网速测试街道
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!

热门搜索 城市网站建设 地区网站制作 街道网页设计 大写数字 热点城市 热点地区 热点街道 热点时间 房贷计算器