Business scenario for Java reflection Mechanism-1

Source: Internet
Author: User
Tags reflection wsdl

A long time not to write things, the recent collation of things before the discovery that there are some good things, in fact, are some learning notes or some technical understanding of some of the feelings, feel very interesting, take out and share with you.

In this article we first describe the Java reflection mechanism, the Java reflection mechanism in fact, at 1.2, when we have no chance to use it. And there may not be such a scene need to use, a few years ago encountered a scene using the Java reflection mechanism in the appropriate, so the use of a moment, found that it really works, and then wrote a number of learning notes.

Now let's talk about the reflection mechanism of java.

First, introduce the Java reflection mechanism, the Java reflection mechanism is in the running state, for any class, can know all the properties and methods of the class, for any object, can call any of its methods and properties This dynamically acquired information, as well as the functionality of the method that dynamically invokes the object, is called the reflection mechanism of the Java language.

Here's a specific scenario: Distributed WebService Interface design.

Requirements: Only one interface method is published to achieve multiple business operations.

Looking at such design requirements, there are many ways to implement the other, I will not comment on the other, we only talk about using the Java reflection mechanism with the Axis configuration WebService implementation.

Design idea: Publish an interface, the interface uses a business class implementation, the business class has many specific business implementation methods.

For example: Save, get a list, modify, get an object entity. (Distributed system uses WebService communication completely)

Using axis to publish a Multi-parameter interface, a parameter is a function, a parameter is para. The value of the function corresponds to the method name in the business class, and para is the data object that the method uses, in JSON format.

So the WebService interface implementation class can easily select a method of the business implementation class through the reflection mechanism to implement the logic processing.

Code implementation:

Axis Configuration Multi-parameter

<?xml version= "1.0" encoding= "UTF-8"?>
<deployment xmlns= "http://xml.apache.org/axis/wsdd/"
Xmlns:java= "Http://xml.apache.org/axis/wsdd/providers/java" >
<globalConfiguration>
<parameter name= "AdminPassword" value= "admin"/>
<parameter name= "attachments. Directory "value="./attachments "/>
<parameter name= "Attachments.implementation"
Value= "Org.apache.axis.attachments.AttachmentsImpl"/>
<parameter name= "Sendxsitypes" value= "true"/>
<parameter name= "Sendmultirefs" value= "true"/>
<parameter name= "Sendxmldeclaration" value= "true"/>
<parameter name= "Axis.sendminimizedelements" value= "true"/>
<parameter name= "Disableprettyxml" value= "true"/>
<parameter name= "Dotnetsoapencfix" value= "true"/>
<parameter name= "Enablenamespaceprefixoptimization"
Value= "false"/>
<requestFlow>
<parameter name= "Scope" value= "session"/>
<parameter name= "Scope" value= "request"/>
<parameter name= "extension" value= "JWR"/>
</requestFlow>
</globalConfiguration>



Type= "Java:org.apache.axis.transport.local.LocalResponder"/>
Type= "Java:org.apache.axis.handlers.http.URLMapper"/>
Type= "Java:org.apache.axis.handlers.SimpleAuthenticationHandler"/>



<service name= "Ubsaservice" type= "" regenerateelement= "true"
Provider= "Java:rpc" style= "RPC" use= "encoded" validate= "true" >
<parameter name= "Scope" value= "Request" regenerateelement= "false"/>
<parameter name= "Allowedmethods" value= "Operateactivite"/>
<parameter name= "ClassName" value= "Com.test.interf.webservice.ServiceImpl"/>

<operation name= "Operateactivite" qname= "Operns:operateactivite" xmlns:operns= "Operateactivite" returnType= : string "
Xmlns:rns= "
Http://www.w3.org/2001/XMLSchema">
<parameter name= "function" type= "ns:string" xmlns:ns= "
Http://www.w3.org/2001/XMLSchema "/>
<parameter name= "para" type= "
ns:string"
Http://www.w3.org/2001/XMLSchema "/>
</operation>


</service>


<transport name= "http" >
<requestFlow>
</requestFlow>
<parameter name= "Qs:list"
Value= "Org.apache.axis.transport.http.QSListHandler"/>
<parameter name= "QS:WSDL"
Value= "Org.apache.axis.transport.http.QSWSDLHandler"/>
<parameter name= "Qs.method"
Value= "Org.apache.axis.transport.http.QSMethodHandler"/>

</transport>
<transport name= "Local" >
<responseFlow>
</responseFlow>
</transport>
</deployment>

Interface Class Code implementation:

Logger.info ("request parameter [function]:" +function);
Logger.info ("request parameter [para]:" +para);
String responsedata = "";
String result = "";
Ubsactivitewebbusiness ubsactivitewebbusiness = (ubsactivitewebbusiness) Custombeanfactory.getbean (" Ubsactivitewebbusiness ");

Webresponsejosn res = new Webresponsejosn ();
try {

class<? Extends ubsactivitewebbusiness> ubsactivite = Ubsactivitewebbusiness.getclass ();
Method method = Ubsactivite. GetMethod (function, new Class[]{string.class});
if (method!=null) {
result = (String) method.invoke (ubsactivitewebbusiness, New Object[]{para});
}else{
Res.setcode ("1");
Res.setmessage ("No processing method found. ");

}

catch (SecurityException e) {
E.printstacktrace ();
Res.setcode ("1");
Res.setmessage ("Operating Platform processing error [SecurityException]");

}

This design has the following advantages:

1: The interface is flexible and can be matched.

2: To judge the business method is simple and efficient, especially the configuration of spring dependency injection, fully efficient positioning business processing methods.

3: Clear and concise code.

The above is just a personal humble welcome to criticize correct.

Note: WebService Call code example:

public static void Main (string[] args) {

String urlstring = "http://127.0.0.1:8096/services/UBSAService?wsdl";
String NamespaceURI = urlstring.substring (0, Urlstring.length ()-5);
String xml = "{\" productid\ ": \" 123333\ ", \" aaaa\ ": \" Aaaaaiiii\ "}";
URL Servicewsdlurl;
Service service = new service ();
Call call;
try {
Servicewsdlurl = new URL (urlstring);
Call = (call) Service.createcall ();
Call.settargetendpointaddress (Servicewsdlurl);
Call.setoperationname (New QName (NamespaceURI, "operateactivite"));
Call.addparameter ("function", xmltype.xsd_string, parametermode.in);
Call.addparameter ("Para", xmltype.xsd_string, parametermode.in);
Call.setreturntype (xmltype.xsd_string);
String result = (string) call.invoke (new object[] {"Saveactivite", XML});
SYSTEM.OUT.PRINTLN (result);
catch (Serviceexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (Malformedurlexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
catch (RemoteException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.