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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:07:27+00:00 2026-06-03T21:07:27+00:00

I am using Spring 3 AOP, and I have an aspect that requires access

  • 0

I am using Spring 3 AOP, and I have an aspect that requires access to the HttpServletRequest. It looks something like this:

@Aspect
public class MyAspect {

    @Autowired
    private HttpServletRequest httpServletRequest;

    public void init() {
        // Do something once...
    }

    @Before("my pointcut here...")
    private void myMethod() {
        // I need the httpServletRequest...
    }

    @After("my pointcut here...")
    private void myOtherMethod() {
        // I need the httpServletRequest...
    }
}

And is configured like this:

<bean id="myAspect" class="com.some.package.MyAspect" init-method="init" />

Is the init method only called once per IoC container, even though this is an aspect, and is the httpServletRequest thread safe? If it is not, what is the best way to get at it during execution of the advice and have it be thread safe? If at all possible I prefer not to use a thread local.

  • 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-06-03T21:07:29+00:00Added an answer on June 3, 2026 at 9:07 pm

    Is the init method only called once per IoC container

    It is called once per every bean instance. If bean has a singleton scope (which is the default case for aspects as well), it will only be called once. However you won’t have access to the httpServletRequest inside init() method – there is no request yet!

    is the httpServletRequest thread safe

    It is not but don’t worry. This is actually much more complex than it looks like. You are injecting HTTP servlet request (and obviously there can be several requests available at the same time) into a singleton object. Which one is injected? None (all?) of them! Spring creates some sophisticated proxy (called scoped proxy) and every time you access methods of injected httpServletRequest it delegates them to current (to thread) request. This way you can safely run your aspects in several threads – each will operate on a different physical request.

    This whole behaviour is described in great details in 4.5.4.5 Scoped beans as dependencies:

    […] If you want to inject (for example) an HTTP request scoped bean into another bean, you must inject an AOP proxy in place of the scoped bean. That is, you need to inject a proxy object that exposes the same public interface as the scoped object but that can also retrieve the real, target object from the relevant scope (for example, an HTTP request) and delegate method calls onto the real object.

    About ThreadLocal:

    I prefer not to use a thread local.

    Fortunately – Spring is using one for you. If you understand how ThreadLocal works – Spring puts current request into a thread local and delegates to thread-local instance when you access httpServletRequest proxy.

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

Sidebar

Related Questions

In my Spring context file I have something like this: <bean id=userCheck class=a.b.c.UserExistsCheck/> <aop:config>
I'm trying to make this 2-player web game application using Spring MVC. I have
I'm using Spring AOP to make a system log, my aop pointcut is like:
I have this odd problem with Spring AOP and I am hoping someone can
I am using aspectJ class for Exception Handling aspect in Spring. i need to
I have a problem. Namely: I'm using spring aop to handle exceptions. Some of
I am working on profiling a legacy application using Spring AOP and I want
Using Spring 2.5 tag library, I have an Integer value in a command form
When using spring security, specifically with @notation; what is the proper way to access
I'm using Spring, Spring Security, BlazeDS, Flex and spring-flex. I know that I can

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.