I have a logger class that makes use of a service. Each time a new logger is created, I expect to have access to the singleton scoped logging service.
I autowire the
Just want to add my 2 cents.
I once encountered the same issue when I was not quite used to the life in the IoC world
. The @Autowired
field of one of my beans is null at runtime.
The root cause is, instead of using the auto-created bean maintained by the Spring IoC container (whose @Autowired
field is indeed
properly injected), I am newing
my own instance of that bean and using it. Of course this one's @Autowired
field is null because Spring has no chance to inject it.
Within your application context you have to do reference your Logger :
<context:component-scan base-package="com.platform"/>
<bean id="asyncLoggingService" class="com.platform.services.AsyncLoggingServiceImplementation" scope="prototype"/>
<bean id="ourLogger" class="com.platform.utils.OurLogger"/>
Then you've to inject it into your service :
@Service
public class AsyncAccountServiceImplementation implements AsyncAccountService
{
private static final String GATEWAY_IP_BLOCK = "1";
@Autowired
private OurLogger logger;
}
Use spring framework Unit test instead of JUnit test to inject your spring bean.
May be that will help you.
When you create an object by new, autowire\inject don't work...
See this link and this link for some workaround.
Anyway if you would inject a logger i suggest you this my answer to another topic.
If you are using AspectJ you can use @Configurable
:
@Configurable
public class OurLogger {
..
}
See: Using AspectJ to dependency inject domain objects with Spring