Home >Java >javaTutorial >How to solve SpringBoot @Componet annotation injection failure

How to solve SpringBoot @Componet annotation injection failure

PHPz
PHPzforward
2023-05-14 11:55:062512browse

    @Component annotation injection failed

    Problem description

    In the springboot program writing, the @Component annotation is clearly added to the Person class. The container shows that the Bean object of the Person class cannot be found.

    I searched for many conventional solutions on the Internet, but found that none of them worked. Later I realized that it might be a problem with package scanning. Since my main program at that time was placed in a folder with the same name as the main program, according to The default package scanning rule - scans the classes in the package where the main program is located and all its sub-packages, then the Person class under the domain package will naturally not be scanned, so although @Component is marked, it will not take effect.

    SpringBoot @Componet注解注入失败如何解决

    Solution

    Add the Component annotation to the main program

    @ComponentScan("com.example")

    Let springboot successfully scan all packages under example, and then everything will fall into place. Problem Solving

    The role of SpringBoot @component

    I stepped on a pitfall. There is an interface. In the implementation class of this interface, the @Autowired annotation needs to be used. I was careless and did not implement the class. Adding the @Component annotation causes Spring to report an error that the class cannot be found

    Once annotations about Spring appear in the class, for example, I used the @Autowired annotation in the implementation class, and the annotated one The class is taken out from the Spring container, and the called implementation class also needs to be managed by the Spring container, plus @Component

    @Component("conversionImpl")
    //其实默认的spring中的Bean id 为 conversionImpl(首字母小写)
    public class ConversionImpl implements Conversion {
        @Autowired
        private RedisClient redisClient;
    }

    Introduction

    It is inevitable to encounter this annotation during development@ Component

    @Controller Controller (injection service)

    is used to mark the control layer, which is equivalent to the action layer in struts

    @ Service service (injection into dao)

    is used to mark the service layer, mainly used for business logic processing

    @Repository (implementation of dao access)

    is used to mark the data access layer, or it can also be said to be used to mark the data access component, that is, the DAO component.

    @Component (instantiate ordinary pojo into the spring container, equivalent to the configuration file )

    Generally refers to various components, that is to say, when our class does not belong to various classifications (when it does not belong to @Controller, @Services, etc.), we can use @Component to annotate this class .

    Example

    This is the writing format in the configuration file, such as applicationcontent.xml in spring mvc,

    Format:

    <bean id="" class=""/>

    In spring boot, since it uses zero configuration, you just need to add the @component annotation directly to the class.

    The following is the introduction of the component scanning component

    <context:component-scan base-package=”com.mmnc.*”>

    The above example is an example of introducing the Component component, where base-package represents all sub-packages that need to be scanned.

    Common points:

    Classes annotated by @controller, @service, @repository, and @component will all be included in the spring container for management

    The above is the detailed content of How to solve SpringBoot @Componet annotation injection failure. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete