Spring Annotation Injection Failure issue

Source: Internet
Author: User
background

When making a tool class, the NullPointerException exception is reported, and after debug it is found that the injected service is null. The code is as follows:

Package com.gwall.utils;

public class Stringreplaceutil {

    @Resource
    private static systrepository systrepository;

    public static string replace (String oldstring) {

        Sqlbuilder builder = new Wherebuilder ();
        list<syst> systlist = Systrepository.findallbywhere (builder);

        Return "";
    }

}

resolution Process Preliminary analysis (static variables)

Because the injected property is static, the initial assertion is that the bean is not allowed to be injected into a static variable. Try:

Scenario One: Setter Injection (recommended)

Package com.gwall.utils;

public class Stringreplaceutil {


    private static systrepository systrepository;

    Public Systrepository getsystrepository () {
        return systrepository;
    }

    Spring injection cannot be injected into the static property, so it is injected with a setter. Add service annotations to the service to properly inject
    @Resource public
    void Setsystrepository (Systrepository systrepository) {
        Stringreplaceutil.systrepository = systrepository;
    }

    public static string replace (String oldstring) {

        Sqlbuilder builder = new Wherebuilder ();
        list<syst> systlist = Systrepository.findallbywhere (builder);

        Return "";
    }

}

Scenario Two: Define a variable injection

Find the solution online, you can refer to public
class Test {

    @Resource
    private systrepository systrepository;

    private static systrepository staticsystrepository;

    @PostConstruct
    void init () {
        staticsystrepository = systrepository;
    }

}
Results:

Still error second step analysis (Spring scan)

Spring is the management of beans through component scanning and automated assembly. Because the scan scope needs to be configured for component scanning, determine whether the class is within the scan range. Try:

Transfer classes to service scan locations based on configuration

Package Com.gwall.service.impl;

public class Stringreplaceutil {


    private static systrepository systrepository;

    Public Systrepository getsystrepository () {
        return systrepository;
    }

    Spring injection cannot be injected into the static property, so it is injected with a setter. Add service annotations to the service to properly inject
    @Resource public
    void Setsystrepository (Systrepository systrepository) {
        stringreplaceutil.systrepository = systrepository;
    }

    public static string replace (String oldstring) {

        Sqlbuilder builder = new Wherebuilder ();
        list<syst> systlist = Systrepository.findallbywhere (builder);

        Return "";
    }

}

Results:

Still error Third step analysis (Spring component Declaration)

Class is to be discovered and managed as a bean by spring and should first be declared as a component class

Package Com.gwall.service.impl;

@Service public
class Stringreplaceutil {


    private static systrepository systrepository;

    Public Systrepository getsystrepository () {
        return systrepository;
    }

    Spring injection cannot be injected into the static property, so it is injected with a setter. Add service annotations to the service to properly inject
    @Resource public
    void Setsystrepository (Systrepository systrepository) {
        stringreplaceutil.systrepository = systrepository;
    }

    public static string replace (String oldstring) {

        Sqlbuilder builder = new Wherebuilder ();
        list<syst> systlist = Systrepository.findallbywhere (builder);

        Return "";
    }

}

Try:

Add the corresponding component declaration annotations (@Component @controller@service@repository) results:

Success Summary whether add Component declaration annotations (@Component @controller@service@repository) are injected into the static variable within the scan range

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.