Spring AOP及MethodInterceptor攔截器實現方法攔截以及切入點函式阻止執行
阿新 • • 發佈:2019-02-16
import com.rhxy.bean_new.personnel.Employee;
import com.rhxy.bean_new.personnel.User;
import com.rhxy.dao_new.EmployeeDAO;
import com.rhxy.dao_new.UserDAO;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import java.lang.reflect.Method;
/**
* Created by Administrator on 2016/11/5 0005.
*/
public class SendMsgInterceptor implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invo) throws Throwable {
Object[] args = invo.getArguments();
Method method = invo.getMethod();
if (!"sendMsg".equals(method.getName())) {
Object obj = invo.proceed();
return obj;
}
try {
if (args.length >= 2) {
String IMAccount = (String) args[2];
UserDAO userDAO = (UserDAO) Utils.getBean("userDAO");
if (null != userDAO) {
User user = userDAO.getUsersByIMAccount(IMAccount);
if (null != user) {
EmployeeDAO employeeDAO = (EmployeeDAO) Utils.getBean("employeeDAO");
Employee employee = employeeDAO.get(user.getEmployeeId());
//業務邏輯判斷 通過IMAcount資訊獲取到員工,如果該員工狀態不為離職的情況下,通過 invo.proceed();它來對目標物件方法呼叫,返回一個Object物件
//否則不做任何操作,直接返回
if (employee != null && employee.getEntry() != null && employee.getEntry().getStatus() != null && !"".equals(employee.getEntry().getStatus())) {
Object obj = invo.proceed();
return obj;
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return args;
}
}