好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

使用@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中并未导入

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

只是导读

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<?xml version= "1.0" encoding= "utf-8" ?>

<beans xmlns= "http://HdhCmsTestspringframework.org/schema/beans"

     xmlns:xsi= "http://HdhCmsTestw3.org/2001/xmlschema-instance"

     xsi:schemalocation="http: //HdhCmsTestspringframework.org/schema/beans

         https: //HdhCmsTestspringframework.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才有效

?

1

2

3

4

5

6

7

<!-- 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.解决

原来代码

?

1

2

@autowired

private final accountdao accountdao;

报错:field injetion is not recommended

改为

?

1

2

3

4

5

6

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

如下图所示

重新启动,错误消失~

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

原文链接:https://blog.csdn.net/jaquechen/article/details/106314745

查看更多关于使用@Autowired注解引入server服务层方法时报错的解决的详细内容...

  阅读:25次