一、首先寫一個Java項目,主要是做爲後面web層依賴的一個jar包,這個java項目只須要一個接口和一個註冊器java
以下:web
接口:app
package com.duduy.up.web;ide
public interface ICallName {
public void excute();
public String getValue();
}this
註冊器.net
package com.duduy.up.web.main;接口
import com.duduy.up.web.ICallName;get
public class Main {
private ICallName callName;it
public void regCallName(ICallName callName) {
this.callName = callName;
}io
public void start() {
callName.excute();
System.out.println("依賴jar包裏面拿的值:"+callName.getValue());
}
}
二、web層依賴上面的那個Java項目(必須打成jar包),實現上面java的接口
這個實現類有兩個做用,一個是依賴的上面的java項目可以監聽到並執行web的excute方法,一個是將web的傳值傳遞下去
package com.duduy.up.web.service;
import com.duduy.up.web.ICallName;
public class CallName implements ICallName {
private StringBuffer sb= new StringBuffer("");
public CallName(String... args) {
if(null!=args){
for(String arg:args){
sb.append(arg+",");
}
}
}
@Override
public void excute() {
System.out.println("實現來自於web層");
}
@Override
public String getValue(){
return sb.toString();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Main main = new Main();
//註冊實現類
main.regCallName(new CallName("1","2","3","5","move","hhh"));
main.start();
response.getWriter().append("Served at: ").append(request.getContextPath());
}
這樣便實現了依賴jar包向上調用web層方法 其實本質上是監聽模式