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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:47:00+00:00 2026-05-23T04:47:00+00:00

I have this odd problem with Spring AOP and I am hoping someone can

  • 0

I have this odd problem with Spring AOP and I am hoping someone can shed some light on it.

I am using CGLIB proxies and when I execute the following aspect i get a null pointer:

@Aspect
@Component //i.e. Singleton
public class MyAspect {

    private static final Logger logger = LoggerFactory.getLogger(MyAspect.class);

    {

        logger.debug("new instance of MyAspect");  //Line 1
    }

    private AutowireCapableBeanFactory factory;

    @Inject
    public MyAspect(AutowireCapableBeanFactory factory) {

        if (factory ==null) {

            logger.debug("your factory is null");  //Line 2
        } else {

            logger.debug("your factory is not null");
        }
        this.factory = factory;
    }

    @AfterReturning(pointcut = "@annotation(com.domain.annotations.MyAnnotation)")
    public void doSomething() {

        if (factory ==null) {

            logger.debug("your factory is null again");  //Line 3
        }

                    // this is a request scoped bean
        MyRequest request = factory.getBean(MyRequest.class);  //Line 4



        if (request != null) {
            logger.debug("No");
            }
        else {
            logger.debug("Yes");
            }
    }
}

I am getting a NullPointerException at Line 4. I can see that the “factory” is null at Line 3 but it is not null when the instance is create at Line 2. I am also tracking the instances being created at Line 1.

This design works using interfaces (JDK dynamic proxies). Why do my instances variables become null at runtime and is there a work around to this?

  • 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-23T04:47:00+00:00Added an answer on May 23, 2026 at 4:47 am

    Long and short is that you can’t use Spring’s constructer based injection with Aspects. There’s a good forum discussion on this topic here.

    I personally use property-based injection for my aspects as suggested in the above forum. The example below allows you to inject a service into an auditing aspect. You don’t need any additional XML configuration, as long as you make your aspect implement ApplicationContextAware.

    @Aspect
    @Component("auditAspect")
    public class AuditAspect implements ApplicationContextAware {
    
    ApplicationContext context;
    
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        // do stuff with context here
    }
    
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            this.context = applicationContext;       
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hopefully someone can help me with this odd problem. I have a database which
I have odd problem: After starting server I got this error: undefined local variable
I have an extremely odd problem in my JUnit tests that I just can't
This is my first question on S.O. I have a very odd problem. Below
I have an odd problem...I'm using a documentation generator which generates a lot of
Okay, this problem is a bit odd. I have an application built in VS2010
That sounds pretty odd but i have this cod and i need to convert
Suppose I have this hypothetical, odd and unintuitive situation #include <iostream> struct A {
I have been banging my head against the wall with this odd behaviour on
This is odd. I have a Default.png, Default-Landscape.png, Default-Portrait.png and Default@2x.png. The normal default

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.