歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Spring MVC底層的簡單實現

Spring MVC底層的簡單實現

日期:2017/3/1 9:49:09   编辑:Linux編程

Spring的主要功能是揉和struts和hibernate,使他們更好的配合,發揮更大的作用。
首先要實現struts對方法的調用,先寫一個實體類
public class Student {
private String username="haha";
private int age=89;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
//此方法便於後面的測試
public void test(){
System.out.println("hello world");
}
}

寫一個action,並且和實體類建立以來關系。
public class UserAction {
private Student stu;
public Student getStu() {
return stu;
}
public void setStu(Student stu) {
System.out.println("set方法: "+stu);
this.stu = stu;
}
public String register(){
System.out.println(stu.getUsername());
System.out.println(stu.getAge());
System.out.println("我是register");
return "success";
}
}

再寫一個strutsaction的測試類,獲取方法名,便於讀取方法
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Struts2ActionTest {

public static void main(String[] args)throws Exception {
//類的全路徑
String actionClass="service.UserAction";
//將要執行的方法名稱
String method="register";
//加載Action類並動態創建一個對象
Object action=Class.forName(actionClass).newInstance();
//通過這個對象得到自己的Class信息對象
Class cls=action.getClass();
//得到這個類的屬性變量
Field[] fields=cls.getDeclaredFields();
for(Field field:fields){
//得到屬性的類型
Class fieldClass=field.getType();
//創建這個屬性的對象
Object fieldObj=fieldClass.newInstance();
//得到屬性名
String fieldName=field.getName();
//構造setter方法
String setMethod="set"+fieldName.substring(0,1).toUpperCase()
+fieldName.substring(1);
System.out.println("setMethod: "+setMethod);
//通過方法名和參數列表得到具體的方法對象
Method prop_method=cls.getDeclaredMethod(setMethod,new Class[]{fieldClass});
//執行這個方法,並且傳入fieldObj參數(調用了setter方法進行賦值)
prop_method.invoke(action,fieldObj);
}
//得到Action中具體的執行方法
Method actionMethod=cls.getDeclaredMethod(method);
//執行這個方法
actionMethod.invoke(action);
System.out.println(actionMethod.invoke(action));
}
}

這樣就得到了UserAction裡的setStu方法,這樣就可以進入register方法,並且得到方法返回的“success”。

下面寫一個方法調用的類,通過包名+類名+方法名,實現方法的調用:

import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class InvokeHandle {
public static void callMethod(String className,String methodName){
try {
Object obj=Class.forName(className).newInstance();
Class cls=obj.getClass();
System.out.println("父類是:"+cls.getSuperclass().getName());
//得到這個類的屬性變量
Field[] fields=cls.getDeclaredFields();
System.out.println("成員變量有:");
for(Field f:fields){
System.out.println(f.getName()+" *** ");
}
System.out.println("=====================");
Method[] methods=cls.getDeclaredMethods();
System.out.print("方法有:");
for(Method method:methods){
System.out.println(method.getName()+" ");
}
System.out.println("=====================");
Method targetMethod=cls.getMethod(methodName,null);
targetMethod.invoke(obj);
} catch (Exception e) {
e.printStackTrace();
}
}

寫一個簡單的方法:
public class DepartService {
public void getDepartById(){
System.out.println("我通過id得到部門");
}
}

寫一個學生類測試一下:
public class StudentTest {

public static void main(String[] args) {
//傳統的調用方法
Student student=new Student();
student.setUsername("afei");
student.test();
System.out.println();
InvokeHandle.callMethod("service.DepartService","getDepartById");
}
}

有寫的不好的地方請多交流。
希望對你們有幫助!!!

Copyright © Linux教程網 All Rights Reserved