Spring constructor injection of SLF4J logger - how to get injection target class?
Posted
by disown
on Stack Overflow
See other posts from Stack Overflow
or by disown
Published on 2010-06-14T14:46:06Z
Indexed on
2010/06/14
15:02 UTC
Read the original article
Hit count: 375
I'm trying to use Spring to inject a SLF4J logger into a class like so:
@Component
public class Example {
private final Logger logger;
@Autowired
public Example(final Logger logger) {
this.logger = logger;
}
}
I've found the FactoryBean
class, which I've implemented. But the problem is that I cannot get any information about the injection target:
public class LoggingFactoryBean implements FactoryBean<Logger> {
@Override
public Class<?> getObjectType() {
return Logger.class;
}
@Override
public boolean isSingleton() {
return false;
}
@Override
public Logger getObject() throws Exception {
return LoggerFactory.getLogger(/* how do I get a hold of the target class (Example.class) here? */);
}
}
Is FactoryBean even the right way to go? When using picocontainers factory injection, you get the Type
of the target passed in. In guice it is a bit trickier. But how do you accomplish this in Spring?
© Stack Overflow or respective owner