problem :

In the use of spring When , Annotations are often used @Autowired or @Resource Inject java Bean;

But when it comes to thread classes and test classes, it doesn't support injection .

Definition :

Thread class : Inherit thread Or implementation Runable The class of the interface .

Test class : Use junit Of jar Package classes for unit testing .

Solution :

Thread class solution : This is where it comes in ApplicationContextAware Interface acquisition ApplicationContext object , And then through getBean Get the object in the same way .

Test class solutions : Use @ContextConfiguration Annotation loading configuration file , for example :@ContextConfiguration(locations = { "/META-INF/spring/applicationContext.xml" }

About threads and junit More articles on injection failure

  1. Injection of autowired dependencies failed; autowire Auto injection failed , The test class has been initialized Spring Containers .

    1 serious : StandardWrapper.Throwable org.springframework.beans.factory.BeanCreationException: Error creat ...

  2. Spring Autowired Injection failure is always Null

    Report errors :NullPointerException analysis : The reason for the error is that the injection failed ? <context:annotation-config/> <context:component-scan ...

  3. AOP The problem of injection failure

    To enable the AOP after , Report such a similar mistake : Error creating bean with name 'bpmpSysUserService': Injection of autowired depend ...

  4. SpringAOP Lead to @Autowired Dependency injection failed

    I used to use springAOP Made an operation log record , This time, when you use it on other classes ,service The injection failed all the time , I found a lot of content on the Internet , I found that everyone has a similar situation , But it's not in line with my own situation . Later I summed up my situation and found that : Fang ...

  5. 【Intellij idea】spring in @Autowired Injection failed

    @Autowired Solutions to injection failure ? The existing solution is : open file-settings perhaps ctrl+alt+s -> Editor And then in Inspections Click on the search bar Input Sp ...

  6. spring boot Development @autowired Injection failed

    @autowired Injection failed The following error message appears : 2018-05-28 08:39:41.857 INFO 8080 --- [ restartedMain] org.hibernate.Versi ...

  7. 【Spring】Service Injection failed , Null pointer

    service Layer classes are useful @Service identification , But the null pointer , Injection failed , Probably because spring Of application Configuration and springmvc Configuration file configuration error for , Causing container conflict . spring and spr ...

  8. ssm in mapper Inject the legend of failure

    Recently, because I want to test a function , It takes the shortest time to start the service , Open the test program , But the usual framework has integrated all kinds of tripartite things , Think about it, then rebuild the simplest one ssm Frame bar . For reference : Simple ssm Latest build The build process is not ...

  9. Error creating bean with name &#39;unMblTotController&#39;: Injection failed

    The new guy today , Enter the company to do projects , Then I created a new package myself , The following error occurred y.UnsatisfiedDependencyException: Error creating bean with name 'unM ...

Random recommendation

  1. mysql Query a small knowledge point , It's not the same when the query result is empty as when the query is wrong

    $conn = new mysqli(....); $sql = ""; $query = $conn->query($sql); here , If the query is normal , There's data coming back , that $qu ...

  2. ( 13、 ... and )Packet socket and sockaddr_ll

    describe         This paper briefly describes the design of data link layer socket Two methods are used      Linux There are two ways to receive data link layer packets :    (1) The original way , That is to create a file of type SOCK_PACKET Of s ...

  3. Croppic – Free and open source jQuery Image clipping plug-in

    Croppic This open source jQuery The image clipping plug-in can meet the different needs of website developers . Just simply upload the image , You can achieve the image zooming and clipping functions you want . Because of the use of HTML5 FormData   Yes ...

  4. log4net stay Asp.net Mvc Application configuration and introduction in

    log4net stay .NET The position in the world is not much , This article focuses on configuration . Less introduction to the use of . Because on the Internet, you can search hundreds of thousands of articles on the Internet to tell you how to use . There's no nonsense about installation , It's simple .Nuget Search inside "log4n ...

  5. DHTMLX Common technology

    GRID Set the foreground color and background color for the line $dataItem->set_row_color("red"); // Set background color $dataItem->set_row_style(& ...

  6. 【 original 】Java Simple modification of the foundation jar In bag class

    Sometimes it needs to be changed a lot jar( Suppose these jar All in lib Catalog ) One of them jar One of the classes in , And there's no source code or ide, The easiest way to do this is : 1 Get into lib Catalog # cd lib # ls test.jar ...

  7. MySQL View Indexes stored procedure trigger function

        View :  It's a virtual table ( It's not real ), Its essence is based on SQL Statement to get a dynamic data set , And name it . Users only need to use the named view to get the result set , And can be used as a table . Its function is to facilitate query operation , reduce ...

  8. OSGI Enterprise application development ( Twelve )OSGI Web application development ( One )

    The previous article describes how to use OSGI Integration in applications Spring and Mybatis frame , This article begins with how to use OSGI technological development Web application . For the traditional Java EE application , The application involves Web The element is nothing more than Servle ...

  9. [C] grammar , Summary of knowledge points ( Two . Structure , Class alias , static, const)

    Structure Definition : struct Student{ // Defining structure Student, stu Is the object created         char a[17]; // There can be many different types of variables in a structure         ...

  10. 《Linux Kernel and Analysis 》 Week 7

    by 21035130 Wang Chuandong Linux How the kernel loads and starts an executable program One . EIF File format : 1.ELF The header is at the beginning of the file , Describe the overall format of the file , Saved the road map , Describe the organization of the document , That is, to generate the ...