@Autowired at @Service?

I'm having trouble getting @Autowired to work in a class annotated with @Service, the variable is automatically set to zero. Let me explain:

@Service
public class Searcher extends Thread implements ISearcher {
@Autowired
protected ISessionProvider sessionProvider; <-- always null
...
public Searcher() {
sessionProvider.doSomeStuff();
}

sessionProvider is always null here.

The strange thing is that the same auto-host in @Controller works:

@Controller
@RequestMapping("/search")
@Secured({ "ROLE_USER" })
public class SearchController  extends BaseController {
@Autowired
protected ISessionProvider sessionProvider; <-- does work 
@Autowired
protected ISearcher searcher;

The last line throws an exception because the Searcher constructor (implementing ISearcher) tries to access sessionProvider, which is null.

I'm not sure what I can do wrong, it looks like spring is not an autowire ISessionProvider in Searcher.

, spring Searcher SearchController, SessionProvider Searcher Searcher SearchController. , SessionProvider. ; -)

- ?

[]

  • - , , .
  • ( )
  • , "" , , "", spring , .
+5
4

Spring bean, beans. bean bean, bean null. .

/ bean, @PostConstruct , bean . :

@Service
public class Searcher extends Thread implements ISearcher {

    @Autowired
    protected ISessionProvider sessionProvider;

    public Searcher() {
        //nothing should be here...
    }

    @PostConstruct
    public void init() {
        sessionProvider.doSomeStuff();
    }
}
+9

Spring bean. , ISessionProvider , , null, , , NullPointerException.

2

  • , @PostConstruct
  • no-arg , , dependeny @Autowired field.

1: , @PostConstruct.

@Service
public class Searcher extends Thread implements ISearcher {
    @Autowired
    protected ISessionProvider sessionProvider;
    ...
    public Searcher() {}

    @PostConstruct
    public void init() {
        sessionProvider.doSomeStuff();
    }

2. .

@Service
public class Searcher extends Thread implements ISearcher { 

    protected final ISessionProvider sessionProvider;

    @Autowired
    public Searcher(ISessionProvider sessionProvider) {
        this.sessionProvider=sessionProvider;
        sessionProvider.doSomeStuff();
    }
}
+5

, , , Searcher, , "autwired" bean. , NPE. spring Searcher ( ), , .. no-argument, "autwired" bean .

- bean, @PostConstruct.

+1

I also get the same problem, but I can’t use @PostConstructbecause I do not use doinit. I have a simple method that gets the language from a lang service. My lang service is here null, but in another controller it is not null, it connects automatically.

@Service
@Configurable(preConstruction = true, autowire = Autowire.BY_NAME)  
public class FormSelectLanguageHandler extends SimpleTagSupport {

    @Autowired
    private LangService langService;

    public List<Lang> getLanguages(){
        Sessions session = (Sessions) getJspContext().findAttribute("SHLSESSION");
        List<Lang> languages=new ArrayList<Lang>();
        //List<Lang> allLanguages = langService.findAllLangs();
        List<Lang> all =new ArrayList<Lang>();
        if (isRenderLangLabel()) {
            all = langService.findByDisplayOrderAsc();
        } else {
            all = langService.findByNameOrderByNameAsc();
        }
        Long companyId = (null == session || null == session.getCompany())? null:session.getCompany().getId();
    }
}
0
source

Source: https://habr.com/ru/post/1539583/


All Articles