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 6544783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:30:26+00:00 2026-05-25T11:30:26+00:00

I have a hard time figuring out how to set a property with the

  • 0

I have a hard time figuring out how to set a property with the help of spring annotations.

I have an abstract base class.

abstract class AbstractTest{
 private static Session session;

 @BeforeClass
 public static void initApplication() throws Exception {
  session = new Session();
  ...
 }

 public Session getSession(){

I have an test class extending my AbstractTest.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class RealTest extends AbstractTest{

 @Autowired
 Service service;

I have a service that needs to make use of the session object and I want it to be “autoset” to the session object.

public class ServiceImpl implements Service {

// @AutoSomething how to make this work?
private Session session;

The spring file that is automatically used for my RealTest thanks to the @ContextConfiguration Annotation

<bean id="Service" class="...ServiceImpl" >
 <property name="session">
  getSession()?? // What's the syntax or how to do this?
 </property>
  • 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-25T11:30:26+00:00Added an answer on May 25, 2026 at 11:30 am
    1. Read about bean scopes. It doesn’t really make sence the kind of injection You’re trying to do. You should not inject the session itself to the business service classes. You should use the session scoped beans instead.

    2. The test class itself is not a part of the test ApplicationContext, so You cannot autowire the values created in the test class to the test class. Anyway, why would You do that? You already have it in the test class, so why not just use simple setter in @Before public void setUp() {} method? Or see the next point.

    3. If You have classes that have dependecies created inside the test class, than the @ContextConfiguration will be of no help here. You can use AnnotationConfigApplicationContext by creating inside the test class an inner @Configuration class and configure the service class using Spring Java Config.

    i.e.:

    @ContextConfiguration
    public class RealTest extends AbstractTest {
    
        @Autowired
        private ApplicationContext appCtx;
    
        private Service service;
    
        @Before
        public void setUp() {
            AnnotationConfigApplicationContext testCtx = new AnnotationConfigApplicationContext();
            testCtx.setParent(this.appCtx);
            testCtx.register(RealTestCfg.class);
            testCtx.refresh();
    
            this.service = BeanFactoryUtils.beanOfType(testCtx, Service.class);
        }
    
        @Configuration
        public static class RealTestCfg {
    
            @Bean
            public Session session() {
                return AbstractTest.session;
            }
    
            @Bean
            public Service service() {
                return new Service();
            }
        }
    }
    

    The @ContextConfiguration and parent ApplicationContext is optional, if You don’t have any other dependencies. And AbstractTest.session needs to be protected static, or have protected static accessor.

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

Sidebar

Related Questions

Having a hard time figuring out the best way to do this... I have
I have a hard time figuring out the correct configuration. I have an URL
Ok, I have been having a VERY hard time figuring this out. I guess
I have hard time figuring out the correct jQuery selector for my problem. What
I am coming from C# background and I have hard time figuring out how
I have a hard time figuring this one out, it's about mistakes that can
I am having a hard time figuring out how to return the current date
I'm having a hard time figuring out how to getText() as a readable String
I'm having a hard time figuring out how to pass by reference in Java.
I'm having a hard time figuring out how to prevent Paperclip from deleting the

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.