Implementation principle: Call and return by passing the service bean name, execution method, and parameters through the reflection mechanism.
Advantage: you only need to provide an external interface service. You only need to operate the service bean in the container and call it through the interface. Adding service beans does not need to add external interfaces.
The Code is as follows:
Interface Class
Public interface ProxyService {/*** webservice call proxy * @ param beanName bean or class name * @ param functionName the called function name * @ param params parameter * @ return * @ throws Exception * /Object proxy (String beanName, string functionName, String... params) throws Exception ;}
Implementation class:
The service is based on spring and implements the spring ApplicationContextAware interface to get the service bean conveniently.
@ Servicepublic class ProxyServiceImpl implements ProxyService, ApplicationContextAware {protected final Logger logger = LoggerFactory. getLogger (getClass (); @ Resourceprivate ApplicationContext applicationContext; @ Overridepublic void setApplicationContext (ApplicationContext applicationContext) throws BeansException {this. applicationContext = applicationContext;}/*** use a proxy to execute the business method. Method Data */@ SuppressWarnings ("rawtypes") @ Overridepublic Object proxy (String beanName, String functionName, string... params) throws ServiceException {// The parameter determines if (StringUtils. isEmpty (beanName) {throw new Exception ("error: beanName is empty. ");} if (StringUtils. isEmpty (functionName) {throw new Exception ("error: functionName is empty. ");} // obtain the service bean Object bean = getBean (beanName); if (bean = null) {throw new Exception (" error: bean is not exist. ");} if (params = null | params. length = 0) {logger. warn ("proxy params is empty. ");} Method method = null; // processing if (params = null | params. length = 0) {try {// retrieve the service bean method = bean. getClass (). getMethod (functionName);} catch (SecurityException e) {logger. error ("proxy getMethod SecurityException:" + e. getMessage (); e. printStackTrace ();} catch (Exception e) {logger. error ("proxy invoke IllegalArgumentException:" + e. getMessage (); e. printStackTrace (); throw new Exception ("error: get method Exception:" + e. getMessage () ;}} else {// processing a parameter call // processing the call method parameter Class [] paraTypes = new Class [params. length]; for (int I = 0; I <paraTypes. length; I ++) {paraTypes [I] = String. class;} try {// obtain the service bean method = bean. getClass (). getMethod (functionName, paraTypes);} catch (Exception e) {logger. error ("proxy invoke IllegalArgumentException:" + e. getMessage (); e. printStackTrace (); throw new Exception ("error: get method Exception:" + e. getMessage () ;}}if (method = null) {throw new Exception ("error: function is not exist. ");} Object rs = null; try {// call the returned data rs = method. invoke (bean, params);} catch (Exception e) {logger. error ("proxy invoke IllegalArgumentException:" + e. getMessage (); e. printStackTrace (); throw new Exception ("error: invoke method Exception:" + e. getMessage ();} return rs;}/*** get bean Object * @ param beanName * @ return */private Object getBean (String beanName) {Object bean = null; bean = applicationContext. getBean (beanName); if (bean = null) {try {Class
Classe = Class. forName (beanName); bean = classe. newInstance ();} catch (InstantiationException e) {logger. error ("getBean InstantiationException:" + e. getMessage (); e. printStackTrace ();} catch (IllegalAccessException e) {logger. error ("getBean IllegalAccessException:" + e. getMessage (); e. printStackTrace ();} catch (ClassNotFoundException e) {logger. error ("getBean ClassNotFoundException:" + e. getMessage (); e. printStackTrace () ;}} logger. debug ("getBean (), beanName:" + beanName); return bean ;}}
The call method is as follows:
ProxyService. proxy ("testservice", "say", "helloword ");
Testservice is a bean instance in spring.
Business Method with say as testservice
Helloword is a parameter
The preceding methods can be used with remote calls (such as webservice) to call APIs externally as a proxy. You only need to implement one external interface to call multiple internal business services of the service.