Home > Java > javaTutorial > body text

How to solve SpringBoot @Componet annotation injection failure

PHPz
Release: 2023-05-14 11:55:06
forward
2463 people have browsed it

    @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")
    Copy after login

    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;
    }
    Copy after login

    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=""/>
    Copy after login

    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.*”>
    Copy after login

    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!

    Related labels:
    source:yisu.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template