Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4032806
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:46:03+00:00 2026-05-20T11:46:03+00:00

When I try to do like below and I have a RequestMapping controller class

  • 0

When I try to do like below and I have a RequestMapping controller class within Spring MVC that instantiates and calls an external class, I am unable to use dependency injection within that class, such as if I try to use

@Resource(name = "savedsearchesService")
        private SavedSearchesService savedsearchesService;

I get a nullpointer exception. Instead I have to pass SavedSearchesService savedsearchesService from my controller method into the other external method to get it to work.

I’m wondering if anyone can point out why it is this way as I am curious and if there is something I am missing as far as how to do this properly. Thanks

@Controller
@RequestMapping("")
public class MainController {

    @Resource(name = "savedsearchesService")
    private SavedSearchesService savedsearchesService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String getPersons(Model model, HttpServletRequest request)
            throws IOException {

        HttpSession session = request.getSession();


        SomeExternalClass someExternalClass = new SomeExternalClass ();
        someExternalClass.Main();
    }

}

Here is the example of the external classs:

public class SomeExternalClass {

    @Resource(name = "savedsearchesService")
    private SavedSearchesService savedsearchesService;

    public void Main () {
        savedsearchesService.get();

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:46:04+00:00Added an answer on May 20, 2026 at 11:46 am

    The problem is with this line:

    SomeExternalClass someExternalClass = new SomeExternalClass();
    

    All classes which depends on springs dependency injection must be spring managed, which is not the case whenever you instantiate classes with the new keyword.

    There are several alternatives. To mention some:

    • Inject SomeExternalClass into your controller.

    • Create a spring-managed factory which hides instantiation logic (such as applicationContext.getBean())

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code that looks like: //System.Data.IDataRecord dr try { Consolidated = Utility.NullConvert.ToBool(dr[Constants.Data.Columns.cConsolidated], false);
Like all developers with OCD I try to organise my namespaces within our project
I have a method pointer like below: typedef void (MMsnInternalCallBacks::* FuncPtr)(); FuncPtr iSoapActionComplete; I
I have a query like below... SELECT contents.id, contents.title, contents.createdBy, (SELECT userGroup FROM suser_profile
i have just taken over an app that has a lot of code like
I have a class below , namespace PocketWeb.AppClass { public class ApiBase { public
I try to achive the opposite of here . I have an abstract class,
I would try something like this but it isn't allowed. function GetDynamicModulesProperties() { var
I'm bored with surrounding code with try catch like this.. try { //some boring
Using something like this: try: # Something... except Exception as excep: logger = logging.getLogger(component)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.