2

I have a Logger Bean that gets injected everywhere I want to log something like this:

@Logger
Log log;

following this blog.

Now I want to also be able to use that logger bean in an unmanaged object. How do I make this object aware that there is a managed logger out there to use?

I've read somewhere that using the ApplicationContext is bad practice. But maybe it's the only way? If so, what would be the best way to do this? This seems like the way to go..?

Thanks!

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
Pete
  • 10,720
  • 25
  • 94
  • 139

1 Answers1

1

If you can't really make the bean managed (by declaring it in applicationContext.xml), yes, it is one of two ways. In a web application use WebApplicationContextUtils to get the application context.

Another way is to use @Configurable with aspectj weaving, which will make your unmanaged object - managed. But I prefer the first option somehow.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • Yeah, using the WebContext like this `WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();` was my first thought as well. However the module in question (implementing some spring security interfaces) is supposed to be standalone (even though it'll be used in a web app). Also that would mean having to mock a Web context in my unit tests to test the for certain logs which I wouldn't much like. But hmm.. since making the object managed is not an option might be thats my only hope.. – Pete Oct 12 '11 at 09:15