Java Dynamic Agent, XML, regular

Source: Internet
Author: User
Tags tag name

15.1 Dynamic agents
After learning the spring framework, the spring framework has one of the core ideas, AOP, (aspact-oriented-programming-oriented slicing) and the principle of AOP is the dynamic agent mechanism of Java, in the dynamic agent mechanism of Java, There are two important classes or interfaces, one is Invocationhandler (Interface), the other is proxy (class), and this class and interface is required to implement our dynamic proxy.
The foundation of 15.1.1 Dynamic agent
Fundamentals of dynamic Agents: interfaces must be available
In Java, interfaces are used to define uniform behavior specifications: interfaces.

An interface must have an implementation class:
Interface SuperStar Super Star
Code of Conduct:
void Sing (int money);
void liveshow (int moeny);
void Sleep ();
?
An interface must have an implementation class:
1. L Implements SuperStar
2. B Implements SuperStar
Proxy class:

Class proxy classes are classes that are created at run time to implement the specified list of interfaces (called proxy interfaces). The proxy instance is an instance of the proxy class. Each proxy instance has an associated call handler object that implements the interface Invocationhandler. A method call on a proxy instance through one of its proxy interfaces will be dispatched to the Invoke method of the instance invocation handler

Static Object newproxyinstance (ClassLoader loader, class[] interfaces, Invocationhandler h) Returns the proxy instance for the specified interface, The proxy instance assigns the method call to the specified invocation handler.

15.1.2 Dynamic agent function:
Intercept and control all behavior of the Proxied object
15.1.3 Invocationhandler Call Processor
Interface Invocationhandler Each proxy instance has an associated call handler. When a method is called on a proxy instance, the method call is encoded and dispatched to the Invoke method of its calling handler.

15.1.4 case


15.2 XML
Summary: XML is all called extensible Markup Language, meaning Extensible Markup Language.

Markup to decorate text information.

Extensible---tags can be arbitrarily defined.

XML technology: With data-related technologies, XML technology in the enterprise is often used to store data and transfer data, XML is popular because the XML language is not related to any programming language, XML can be used in PHP, Java,. NET any programming language.

15.2.1 writing an XML configuration file
XML is written with some rules in it:
1) The suffix name of the XML file is. xml
2) XML has only one root tag
3) XML tag is the angle bracket wrapped keyword in pairs appear, there is a start tag has an end tag, the keyword is custom, XML can also have empty tags/self-closing tags
4) XML allows attributes, attributes are also customized as needed, attribute format: property = "attribute value", and multiple attributes are separated by spaces
5) XML is case-sensitive
The composition of XML
1) Document declaration: <?xml version= "1.0" encoding= "UTF-8"?>

2) root tag, for example: <store>

3) Other labels, e.g.:<name>

4) attributes, for example: category= "mobile phone digital"

5) text, for example: Huawei Mobile

6) Comments, such as:<!--This is the root tag of the XML document--

15.2.2 constraints Introduction DTD, Schema


15.2.3 XML parsing:

Practical Scenarios FOR XML:

In actual development, we generally use a variety of frameworks for enterprise development, and these frameworks will generally have some common functions have been written, we need to do just follow the framework provided by the constraints of the framework configuration can be, when we use XML configuration framework, and then run, The bottom of the framework parses our configuration XML document to get useful information to implement certain functions according to our needs.

Therefore, we seldom write XML constraints and parse XML ourselves in real-world development.


@Test
public void Test1 () throws Documentexception {
?
1. Create a Saxreader parser
Saxreader Saxreader = new Saxreader ();
2. Call the Read method to read the XML configuration file and get a Document object
Document document = Saxreader.read ("books.xml");
3. Call the Getrootelement method to get the root tag object
Element root = Document.getrootelement ();
4. Call the elements method to get the child label array
list<element> bookelements = root.elements ();
5. Traversing the bookelements array
for (Element book:bookelements) {
6. Call the AttributeValue method, pass in the property name, get the corresponding property value
String author = book.attributevalue ("author");
System.out.println ("author =" + author);
?
7. Get Child Tags
list<element> elements = book.elements ();
8. Traverse Child Tags
for (Element e:elements) {
9. Get tag name, and tag body data
String name = E.getname ();
String text = E.gettext ();
SYSTEM.OUT.PRINTLN (name + "=" + text);
}
}
}
?
Output Result:
Author = Zhang San Feng
Name = Java from getting started to mastering
Price = 98
Author = extermination Division too
Name = Java Programming idea
Price = 998
@Test
public void Test2 () throws Documentexception {
1. Create a Saxreader parser
Saxreader Saxreader = new Saxreader ();
2. Call the Read method to read the XML configuration file and get a Document object
Document document = Saxreader.read ("Beans.xml");
3. Call the Getrootelement method to get the root tag object
Element root = Document.getrootelement ();
4. Call the elements method to get the child label array
list<element> beanelements = root.elements ();
5. Call the elements method to get the child label array
for (Element bean:beanelements) {
6. Call the AttributeValue method, pass in the property name, get the corresponding property value
String id = bean.attributevalue ("id");
String className = Bean.attributevalue ("ClassName");
System.out.println (id + "=" + ClassName);
7. Call the elements method to get the child label array
list<element> propelements = bean.elements ();
8. Call the Propelements method to get the child label array
for (Element prop:propelements) {
9. Call the AttributeValue method, pass in the property name, get the corresponding property value
String name = Prop.attributevalue ("name");
String value = Prop.attributevalue ("value");
SYSTEM.OUT.PRINTLN (name + "=" + value);
}
}
}
?
Output Result:
001 = Cn.itcast.bean.User
Username = Zhangsan
Password = 123456
002 = Cn.itcast.bean.User
Username = LiSi
Password = 654321

15.3 Regular Expressions

15.3.1 Regular Concept
Egular expression regex: In Java, the regular use of a method parameter of the String class is the name of the regex

Three methods with a regex parameter for the String class:
Split (String regex) cut.
A matches (String regex) match.
ReplaceAll (string regex, string replacement) replaced.

15.3.2 Symbol Introduction
1. [] The range of values to be taken. 0-9 values from 0 to 9 are established.
Note: [0-9] \\d can be used to indicate
2. {} indicates the number of times the ' value/character ' can occur in the previous condition.
Description: {4,11} at least 4 times, up to 11 times.
{0,1} at least 0 times, up to one time. Can I use it? Said.
{1,} at least 1 times, up to an unlimited number of times can be used to represent.
{0,} at least 0 times, up to an unlimited number of times. You can use * to indicate.
3. () indicates grouping. You can use the $ symbol on the second parameter of the ReplaceAll method to refer to the previous grouping, and the grouping number automatically starts at 1.

         15.3.3 case
                     
public class ReplaceAllTest4 {
   public static void Main (string[] args) {
?
       //13311111946, 133****1946
?
       string phone = "13311 111946 ";
?
       /*
          source data: 13311111946
      &NBSP ;   Part I: 133     Rules one: 1[34578]\\d
          Part two: 1111     Rule two: \ \D{4}
          Part III: 1946     rule three: \\d{4}
        */
?
       string result = Phone.replaceall ("(1[34578]\\d) (\\d{4}) (\\d{4})", "$1****$3");
      &NBSP;SYSTEM.OUT.PRINTLN ("result =" + result);
 }
}
?
Output result:
result = 133****1946

Java dynamic Agent, XML, regular

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.