I found a lot of information about using @Autowired with a non-empty constructor here . But I can’t even solve my current problem.
I have an interface for logging. I register every record in the database. For this reason, it is important for me to know the name of the class in which the class uses the registrar.
My first idea: Create a new Logger implementation that has a non-empty constructor. So, when creating a new instance of this registrar, you must set the name.
My interface is as follows:
public interface Logger{
public void log(String name, String msg);
}
The implementation of this looks like this:
@Service
public class LoggerImpl implements Logger{
private String className;
LoggerImpl(String className){ this.className = className }
@Override
public void log(String name, String msg) {
}
And everywhere in my application I want to use this logger - Interface. For example, in some business classes, I use Logger this way:
@Service
public class ServiceName {
private Logger logger;
@Autowired
public ServiceName(){
logger = new LoggerImpl("ServiceName");
}
public void someMethod(){
logger.log("name", "this is a log!");
}
}
, NullPointerException, . . , LoggerImpl spring. ? . . spring Boot XML .