Apache Beanutils is a project under the Apache Open source software organization that is widely used in spring, Struts, hibernate and other frameworks, with thousands of jar packages dependent on it. It is reflected and introspective through the JDK
Features that provide many useful features that are not directly provided by the JDK. I found the official entry document, translated in my own language, I hope you correct me.
As early as possible from JavaBean, this name comes from a Java API for the component architecture, writing Java classes according to JavaBeans design principles will make it easier for developers to understand what your class can offer, as if allowing
JavaBeans tools to use the introspection capabilities of Java to know the properties and operations provided by your class. and presented in a visually appealing way on the development tool (I understand that when you use Eclipse or IntelliJ idea, the decimal point after the object name
Pop-up method list).
The JavaBeans specification defines a complete set of attributes to determine whether any Java class is JavaBean, and you should consider reading this document as an important part of your Java programming skills. Some important features are as follows:
- The flag qualifier for a class must be public and provide a public parameterless constructor. This will allow tools and applications to dynamically create new instances of your bean without knowing in advance which Java class name will be used. (There was a discussion on StackOverflow, and I also took part in the answer)
- Since you have a parameterless constructor, the behavior of the configuration bean must be separated from the initialization, which is usually done by defining a series of properties. Through them you can modify the bean's behavior or data. The name of the property is usually named by the hump.
- Typically, each property has a public getter and setter method to get or set the property value, respectively. The JavaBeans specification defines a naming convention.
Public classEmployee { PublicEmployee ();//Zero-arguments Constructor PublicString getfirstname (); Public voidsetfirstname (String firstName); PublicString getlastname (); Public voidsetlastname (String lastName); PublicDate gethiredate (); Public voidsethiredate (Date hiredate); Public BooleanIsmanager (); Public voidSetmanager (Booleanmanager); PublicString getfullname ();}
- There is an exception to the Boolean variable, and if you think Ismanager is easier to understand than GetManager, you can use Ismanager to name
Why do I need Apache beanutils? The standard Java syntax mechanism makes it easy for you to get FullName in employee through Getfullname (). For example
Employee employee = ...; System.out.println ("Hello" + employee.getfirstname () + "!");
But when you are in a more complex environment, you may not be able to know in advance which Bean will be used, which property is to be obtained or modified, what do you do? The Java language provides classes like Java.beans.Introspector, the ability to check classes at run time, and to determine the Setter/getter method name of a property, plus the capability of Reflection to execute the method dynamically. However, these APIs are difficult to use and expose a lot of unnecessary detail at the bottom of the Java class. The API in Beanutils is designed to simplify them at run time, rather than at compile time. All JavaBean supported attribute types can be divided into three categories-some are supported by the JavaBean specification, and some are only accepted by beanutils packets.
- Simple. There is only one value that can be made or modified. Int,java.lang.string, or more complex objects defined by the Java language, other references, or class libraries.
- Indexed. A property with subscript stores an ordered set.
- mapped-as an extension of the standard JavaBean APIs, beanutils that any attribute that has a Java.util.Map value is "Mapped". You can set/get a single value by using a string key.
Let's get started with an example of your own writing. Paste into the editor to run directly.
Packagebeanutils;ImportJava.util.HashMap;ImportJava.util.Map;/*** Created by Andrew on 2015/12/4.*/ Public classEmployee {String firstName; String LastName; Employee[] subordinate; Map<string, address>address; PublicEmployee () {firstName= "Adnrew"; LastName= "Chen"; Subordinate=Newemployee[]{NewEmployee ("Shirley", "Liu"),NewEmployee ("Alex", "Wang")}; Address=NewHashmap<>(); Address.put ("Home",NewAddress ("Changsha Yuelushan")); } PrivateEmployee (String firstname,string lastName) { This. FirstName =FirstName; This. LastName =LastName; } PublicAddress getaddress (String type) {returnaddress.get (type); } Public voidsetaddress (String type, address address) { This. Address.put (type, address); } PublicEmployee Getsubordinate (intindex) { returnSubordinate[index]; } Public voidSetsubordinate (intIndex, Employee subordinate) { This. subordinate[index] =subordinate; } PublicString Getfirstname () {returnFirstName; } Public voidsetfirstname (String firstName) { This. FirstName =FirstName; } PublicString Getlastname () {return This. LastName; } Public voidsetlastname (String lastName) { This. LastName =LastName; } @Override PublicString toString () {returnfirstname+ "" +LastName; }}
Packagebeanutils;Importorg.apache.commons.beanutils.BeanUtils;Importorg.apache.commons.beanutils.PropertyUtils;Importjava.lang.reflect.InvocationTargetException;/*** Created by Andrew on 2015/12/4.*/ Public classBeanutilstest { Public Static voidMain (string[] args) {Employee Employee=NewEmployee (); Try{System.out.println (String) propertyutils.getsimpleproperty (employee,"FirstName")); System.out.println (String) propertyutils.getsimpleproperty (employee,"LastName")); System.out.println (Propertyutils.getindexedproperty (Employee,"Subordinate[0]")); System.out.println (Propertyutils.getmappedproperty (Employee,"Address (Home)")); System.out.println (Propertyutils.getnestedproperty (Employee,"Address (home). City")); } Catch(illegalaccessexception e) {e.printstacktrace (); } Catch(InvocationTargetException e) {e.printstacktrace (); } Catch(nosuchmethodexception e) {e.printstacktrace (); } }}
For simple attributes, a direct call to Propertyutils.getsimpleproperty (employee, "FirstName") is available.
For the indexed attribute, Propertyutils.getindexedproperty (employee, "subordinate[0") or Propertyutils.getindexedproperty ( Employee, "subordinate", 0) to obtain.
For the mapped attribute, Propertyutils.getmappedproperty (employee, "Address (Home)") Propertyutils.getmappedproperty (Employee, " Address "," home ") to obtain.
For more complex nested properties, assume that you get the city property of an employee's address in the key= "Home" object. We can use the standard notation:
String City = employee.getaddress ("Home"). Getcity ();
With Propertyutils, you can use the delimiter "." As JavaScript to get nested properties
String city = (string) propertyutils.getnestedproperty (employee, "Address (Home)");
You can view the document API in more detail.
You will then update the Getting started on dynamic beans.
Apache Beanutils official Getting Started document