Springboot Startup class:
@SpringBootApplication Public class Mainapp { publicstaticvoid main (string[] args) { Springapplication.run (Mainapp. class , args);} }
JPA Database Operation classes:
Public Interface extends Mongorepository<user, string> {}
Unit Test class:
@RunWith (Springjunit4classrunner.class) @SpringApplicationConfiguration (Mainapp.class) Public classuserdaotest { @Autowired Private Userdao Userdao; @AutowiredPrivateObjectmapper Objectmapper; @Test Public voidAdd ()throwsjsonprocessingexception { for(inti = 1; I <= 5; i++) {User User=NewUser (); User.setname ("Test" +i); User.setsex ((i%2 = = 0)? 0:1); User.setphone ("1511111000" +i); User.setcreatetime (NewDate ()); User Save=userdao.save (user); System.out.println ("Save Result:" +objectmapper.writevalueasstring (save)); } }}
Execution Result:
Org.springframework.beans.factory.BeanCreationException:Error creating Bean with Name ' userdaotest ': injection of autowired dependencies failed; Nested exception is org.springframework.beans.factory.BeanCreationException: Could not Autowire field:private Com.mlxs.springboot04.mongodb.dao.UserDao Userdaotest.userdao; Nested exception is Org.springframework.beans.factory.NoSuchBeanDefinitionException:No qualifying bean of type [ Com.mlxs.springboot04.mongodb.dao.UserDao] found for dependency:expected at least 1 bean which qualifies as Autowire Cand Idate for this dependency. Dependency annotations: {@org. springframework.beans.factory.annotation.Autowired (Required=true)}
Workaround:
Adding @enablemongorepositories annotations to the Springboot startup class, labeling the path to the Mongorepository class
@SpringBootApplication = {"Com.mlxs.springboot04.mongodb.dao"}) Public class Mainapp { publicstaticvoid main (string[] args) { Springapplication.run (Mainapp. class , args);} }
Springboot when using Jpa+mongodb, xxxrepository cannot autowired the problem