I am working on a Spring project that creates beans with a context.xml. So in my code I have src -> main -> resrouces -> spring -> <application-name>-context.xml.
context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="object" >
<propery name="user" value="${property.user}">
<propery name="password" value="${property.password}">
<\bean>
<!-- Some More Beans -->
</beans>
However, the DAO I am using is in a different project and I do not have access to the code with the DAO so I cannot put logs directly in the class.
I noticed this post about Log spring Bean instantiation but, I was curious if there is a way to do this directly in the context.xml?
--- Update ---
Unfortunately the DAO I would be inheriting from is in a different project than the DAO that uses it would be inheriting I.E.
Parent class in a different repo
public class ObjectDao {
private String userName;
private String password;
public class ObjectDao(String userName, String password) {
this.userName = userName;
this.password = password
}
//some logic
}
Child DAO (in a separate repo)
-> src -> main - > resources -> spring -> context.xml
context.xml with properties to create beans (see above)
-> src -> main - > java -> util -> DaoUtil.class
public class DaoUtil {
private static String otherProperty = "Other Property";
private static String anotherProperty = "Another Property";
private ObjectDao objectDao; //context.xml stores the properties to build the "ObjectDao" described above
public DaoUtil() {
//Generic Constructor Code
}
//Some methods / logic
}
This is an old monolithic codebase that uses old standards. Essentially I want to log the username and password from ObjectDao without alternating any code if possible or minimize the code I am using.
CodePudding user response:
context.xml is only the way to describe the beans you would like to create, it doesn't contain any logic of what should be done upon the bean creation. In this case, you should decide whether it's enough to log only the Dao object creation or you want to log also calls to the DAO methods. First of all, revise the existing DAO code (even though you can't modify it). Maybe it already contains some logs and you can just set up your logging system (you probably have one if it's a real project). You don't do it in the context.xml though, so ask a more specific question about the logging system.
If it doesn't contain any logs, then:
If it's enough to log the bean creation read about bean post-processors, probably the easiest way to achieve what you want. This is a special kind of beans - just regular beans that spring treats slightly different - these a "technical" beans that run during the (other) beans instantiation. The BPP can check whether the bean is of type of your DAO and if it is - logs whatever is needed. I'm not placing an example, because there is one in the thread that was attached to the question.
If you also want to log other method calls - consider using some kind of proxy: instead of creating the bean just like this, use spring aop or even manual proxy (well, you can also use Bean Post Processors for that but its an advanced stuff really).
In a nutshell, if you have something like this:
interface MyDao {
void foo();
}
class MyDao implements MyDao{
void foo() {...}
}
You can create a proxy:
// this is in your application
class MyDaoProxy implements MyDao {
private MyDao real;
public MyDaoProxy(MyDao real) {
// log - created
this.real = real;
}
public void foo() {
// log foo called
real.foo();
}
}
Or you can use inheritance - inherit from DAO, add log message in the constructor and override the method foo so that it will log first and then call to super.foo()
.
The idea is that the proxy is indistinguishable from the real object for everyone that calls this proxy.
Spring AOP will allow using a similar techniques for many classes at once, but hope you get the idea
CodePudding user response:
@Mark Bramnik: Thank you. Unfortunately the DAO I would be inheriting from is in a different project than the DAO that uses it would be inheriting I.E.
Parent class in a different repo
public class ObjectDao {
private String userName;
private String password;
public class ObjectDao(String userName, String password) {
this.userName = userName;
this.password = password
}
//some logic
}
Child DAO (in a separate repo)
<project> -> src -> main - > resources -> spring -> <application-name>context.xml
context.xml with properties to create beans
<project> -> src -> main - > java -> util -> DaoUtil.class
public class DaoUtil {
private static String otherProperty = "Other Property";
private static String anotherProperty = "Another Property";
private ObjectDao objectDao; //context.xml stores the properties to build the "ObjectDao" described above
public DaoUtil() {
//Generic Constructor Code
}
//Some logic
}
This is an old codebase that uses old standards. Essentially I want to log the username and password from ObjectDao without alternating any code if possible or minimize the code I am using.