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

  • SEARCH
  • Home
  • 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 473921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:14:07+00:00 2026-05-13T00:14:07+00:00

I would like to know if it’s possible to use Spring to resolve the

  • 0

I would like to know if it’s possible to use Spring to resolve the dependencies of an object created manually in my program. Take a look at the following class:

public class TestClass {

   private MyDependency md;

   public TestClass() {
   }

   ...

   public void methodThaUsesMyDependency() {
      ...
      md.someMethod();
      ...
   }

}

This TestClass is not a spring bean, but needs MyDependency, that is a spring bean. Is there some way I can inject this dependency through Spring, even if I instantiate TestClass with a new operator inside my code?

Thanks

  • 1 1 Answer
  • 1 View
  • 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-13T00:14:08+00:00Added an answer on May 13, 2026 at 12:14 am

    Edit: The method I’m describing in my original answer below is the general way to accomplish DI external of the container. For your specific need – testing – I agree with DJ’s answer. It’s much more appropriate to use Spring’s test support, for example:

    @Test
    @ContextConfiguration(locations = { "classpath*:**/applicationContext.xml" })
    public class MyTest extends AbstractTestNGSpringContextTests {
    
        @Resource
        private MyDependency md;
    
        @Test
        public void myTest() {
                ...
    

    While the above example is a TestNG test, there is also Junit support explained in 8.3.7.2. Context management and caching.


    General approach: Annotate your class with @Configurable and utilize AspectJ load-time or compile-time weaving. See 6.8.1 in the Spring documentation on AOP for more details.

    You can then annotate your instance variables with @Resource or @Autowired. Though they accomplish the same goal of dependency injection, I recommend using @Resource since it’s a Java standard rather than Spring-specific.

    Lastly, remember to consider using the transient keyword (or @Transient for JPA) if you plan on serializing or persisting the objects in the future. Chances are you don’t want to serialize references to your DI’d repository, service, or component beans.

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

Sidebar

Ask A Question

Stats

  • Questions 247k
  • Answers 247k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If the validators are added with Id's you can reference… May 13, 2026 at 8:45 am
  • Editorial Team
    Editorial Team added an answer Well, I've just had a massive D'oh moment. The request… May 13, 2026 at 8:45 am
  • Editorial Team
    Editorial Team added an answer Look at how Parameter is internally represented. If it is… May 13, 2026 at 8:45 am

Related Questions

Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I would like to know if it is possible to determine if a function
I would like to know if it is possible, To extract the userid from
I would like to know if it is possible, to select certain columns from
I would like to know if it is possible to copy/move files to a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.