First I have a data access layer interface:
Public interface Studentdao {
void Save (Student stu);
}
and implementation classes:
1.mysql Implementation Class
public class Studentdaoimplbymysql implements studentdao{
public void Save (Student stu) {
System.out.println (Stu.getname () + "Saved by orcacle!");
}
}
2.oracle Implementation Class
public class Studentdaoimplbyorcacle implements studentdao{
public void Save (Student stu) {
System.out.println (Stu.getname () + "Saved by MySQL!");
}
}
Then my business logic Layer interface:
Public interface Studentservice {
public void Add (Student stu);
}
My business Logic Layer implementation class (there is an interface to the data access layer):
public class Studentserviceimpl implements Studentservice {
Private Studentdao DAO;
public void Add (Student stu) {
Dao.save (Stu);
}
Public Studentdao Getdao () {return DAO; }
public void Setdao (Studentdao dao) {This.dao = DAO; }
}
And then the main is my tool class application:
public class Application {
Private map<string, object> Map = null;
Public application () throws IOException, Instantiationexception, Illegalaccessexception, ClassNotFoundException, Documentexception, Introspectionexception, IllegalArgumentException, invocationtargetexception {
Map = new hashmap<string, object> ();
To read an XML file using dom4j
Saxreader read = new Saxreader ();
Read file
Document document = Read.read (Application.class.getClassLoader (). getResourceAsStream ("App.xml"));
Get root node
Element rootelement = Document.getrootelement ();
Gets the collection of child nodes under the root node
list<element> elements = rootelement.elements ();
for (Element element:elements) {
ID is identity name (random), class is type, and class is found based on ID value
String id = element.attribute ("id"). getValue ();
String clas = Element.attribute ("class"). GetValue ();
Get the class class name and get the instance object
Object object = Class.forName (clas). newinstance ();
Continue to loop child nodes under a child node
For (Element Element2: (list<element>) element.elements ()) {
There is a property in the Studentserviceimpl implementation class that is the interface of the Studentdao layer
The value of this name must match the Studentserviceimpl property name
String name = Element2.attribute ("name"). GetValue ();
Ref points to DAO's an implementation class Studentdaoimpl's identity name (that is, id)
String ref = Element2.attribute ("ref"). GetValue ();
Property descriptor parameter One is an alias of the interface: Studentdao DAO
One is an instance of the implementation class CN.JNTI.DAO.STUDENTDAOIMPLBYMYSQL
PropertyDescriptor PD = new PropertyDescriptor (Name,object.getclass ());
Writemethod is the method of public void Setdao (Studentdao dao), which is how it is injected.
Method Writemethod = Pd.getwritemethod ();
Since the key value in my map collection is an Id,value value, ref is studentdaoimplbymysql at this time
Studentdaoimplbymysql is the key to the Cn.jnti.dao.StudentDaoImplByMySql object in the collection.
object is Map.get (ref) Gets an instance of this object
Writemethod.invoke (object, Map.get (ref));
}
All that's in the map collection is the proxy object.
Handle Han = new Handle (object);
Returns a proxy object
Object = Proxy.newproxyinstance (Object.getclass (). getClassLoader (), Object.getclass (). Getinterfaces (), Han);
Put the proxy object into the collection
Map.put (ID, object);
}
}
Public Object Getbean (String name) {
The return is a proxy object
return Map.get (name);
}
}
*************
My handle class:
*************
public class Handle implements invocationhandler{
Private Object obj;
Public Handle (Object obj) {
This.obj = obj;
}
@Override
public object invoke (object proxy, Method method, object[] args) throws Throwable {
Filter out the pre-and post-execution methods of the service layer
if (Method.tostring (). Contains ("Service")) {
return Method.invoke (obj, args);
}
Add pre-and post-execution methods only at the DAO layer
Dobefor ();
Object object=method.invoke (obj, args);
Doafter ();
return object;
}
private void Dobefor () {System.out.println ("before execution!") "); }
private void Doafter () {System.out.println ("after Execution"); }
}
***************
My app.xml.
****************
<?xml version= "1.0" encoding= "UTF-8"?>
<beans>
<bean id= "Studentdaoimplbymysql" class= "CN.JNTI.DAO.STUDENTDAOIMPLBYMYSQL" ></bean>
<bean id= "studentdaoimplbyorcacle" class= "Cn.jnti.dao.StudentDaoImplByOrcacle" ></bean>
<bean id= "Studentserviceimpl" class= "Cn.jnti.service.StudentServiceImpl" >
<property name= "DAO" ref= "Studentdaoimplbymysql" ></property>
</bean>
</beans>
****************
My test class:
public class Teststudent {
Studentservice Service=null;
@Test
public void Studentadd () throws IOException, ClassNotFoundException, Instantiationexception, Illegalaccessexception, IllegalArgumentException, Documentexception, Introspectionexception, invocationtargetexception{
Application App=new application ();
Service = (Studentservice) app.getbean ("Studentserviceimpl");
Student stu=new Student ("Xiaoming", 18);
A proxy object is returned, and the method that invokes the proxy object will walk dobefor, and Doafter
Service.add (Stu);
}
}
Sring control inversion (inversion of CONTROL,IOC) is also known as Dependency injection (Dependency Injection,di) principle with reflection and proxy implementations