Spring Boot Auto Scan

Source: Internet
Author: User

When spring boot and mybatis are integrated, spring boot annotations cannot scan to annotations under packages other than the application class when the interface is scanned, such as:

The app is the application class, the Productmapper class:

@Mapper Public Interfaceproductmapper {@Insert (Insert into products (Pname,type,price) VALUES (#{pname},#{type},#{price})     Public intAdd (product product); @Delete ("Delete from products where id=#{arg1}")     Public intDeletebyid (intID); @Update ("Update products set Pname=#{pname},type=#{type},price=#{price} where Id=#{id}")     Public intUpdate (product product); @Select ("SELECT * FROM Products where ID=#[ARG1}")     PublicProduct GetById (intID); @Select ("SELECT * from Productsorder by id DESC")     PublicList<product>querybylists ();}

When the app is running, the backend will report that Productmapper is not found in this class:

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying Bean of type ' com.self.spring.mapper.ProductMapper ' availableat Org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean (Defaultlistablebeanfactory.java : 353) at Org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean ( defaultlistablebeanfactory.java:340) at Org.springframework.context.support.AbstractApplicationContext.getBean ( abstractapplicationcontext.java:1090) at Com.self.spring.springboot.App.main (app.java:17)

 

causes the above error: The Bean assembly default rules for Springboot projects are scanned from top to bottom based on the package location where the application class resides! "Application class" refers to the Springboot project entry class. The location of this class is critical:

If the package containing the application class is: io.github.gefangshuai.app , only the package io.github.gefangshuai.app and all its child packages will be scanned, and if the service or DAO is in a package that is not io.github.gefangshuai.app under its child package, it will not be scanned!

 

Workaround

The first type: The Application class is placed under the parent package, and all annotated classes are placed under the same or under its child package

Second: Specify the URL of the package to be scanned for annotations, the solution to the problem above can be annotated on the application class to scan the class under the Mapper interface package.

@SpringBootApplication @mapperscan (basepackages= "Com.self.spring.springboot.mapper")  Public class App {    publicstaticvoid  main (string[] args) {        = Springapplication.run (App.class, args);        System.out.println (Context.getbean (productmapper. class ));        Context.close ();    }}

Spring Boot Auto Scan

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.