About BeanUtils
When you talk about BeanUtils, you probably don’t know which BeanUtils you’re referring to. Because it comes in a lot of bags, and one of the most popular ones is
(1) org.apache.com mons. Beanutils. Beanutils
(2) the org. Springframework. Beans. BeanUtils
What’s the difference between the two?
(1) Different properties of classes
apache:
springframework:
Apache’s are normal classes, springframework’s are abstract classes.
(2) The same methods are used in different ways
The name is the same, the method name is the same, the parameter type and number are the same, the function needs to be implemented is the same, but the results are very different.
Example: copyProperties (used to copy property values between objects)
apache:
springframework:
Sharp-eyed partners have noticed that the first argument to Apache is the target object and the second is the source object; The first argument to the springframework is the source object and the second is the target object. Be careful which package you refer to when developing.
Second, based on existing methods to extend
Because there are many class attributes to be reset during development, a lot of get and set methods take up your space and time, resulting in low development efficiency.
So in this extension of some often used methods for your reference.
1. Use org.apache.com mons. Beanutils. Beanutils. The populate method
Encapsulate the data in the Map into an entity class.
The extension encapsulates data in a Map into entity classes and supports date-format conversions.
/** * Copy the Map value to the entity ** @param target Target * @param params Map * @return T
*/
public static <T> T copyFields(T target, Map<String, Object> params) {
if(null == params || params.isEmpty()) {
return target;
}
try {
org.apache.commons.beanutils.BeanUtils.populate(target, params);
} catch (IllegalAccessException | InvocationTargetException e) {
LOGGER.error(e.getMessage(), e);
}
return target;
}Copy the code
Note: Date types cannot be converted, so you need to manually register a time converter yourself.
This is registered in a static block of code to convert a string in the format YYYY-MM-DD to Date.
Static {// Register the BeanUtils convertutils. register(new)Converter() {
@SuppressWarnings("rawtypes")
public Object convert(Class type, Object value) {
try {
return DateUtils.parseDate(String.valueOf(value), "yyyy-MM-dd");
} catch (ParseException e) {
LOGGER.error(e.getMessage(), e);
}
return null;
}
}, Date.class);
}Copy the code
2, use org. Springframework. Beans. BeanUtils. GetPropertyDescriptors method
Gets an attribute descriptor for a class
The extension allows you to copy the value of a non-empty field from one entity to another.
/** * Copy the value of a non-empty field from one entity to another entity in relation to the field name ** @param target Target entity * @paramsourceSource entity * @return T
*/
public static <T, S> T copyNotNullFields(T target, S source) {
Map<String, Object> params = getFiledValues(source, o -> (o ! = null)); T newTarget = copyFields(target, params);return newTarget;
}Copy the code
Based on the judgment of the attribute value, the qualified attribute and its value can be encapsulated into a Map.
/** * Reads the entity with the entity field name key and value, and encapsulates the value that meets the conditions into a Map ** @param obj source entity * @param predicate conditions * @return Map<String, Object>
*/
public static Map<String, Object> getFiledValues(Object obj, Predicate<Object> predicate) {
if(null == obj) {
returnCollections.emptyMap(); } Map<String, Object> map = Maps.newHashMap(); Arrays.asList(org.springframework.beans.BeanUtils.getPropertyDescriptors(obj.getClass()) .stream() .filter(o -> ! o.getName().equals("class"))
.forEach(o ->
{
String propertyName = o.getName();
Method readMethod = o.getReadMethod();
try {
Object propertyValue = readMethod.invoke(obj);
if(predicate.test(propertyValue)) { map.put(propertyName, propertyValue); } } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { LOGGER.error(e.getMessage(), e); }});return map;
} Copy the code
Three, heart
If you’re constantly wasting time on repetitive, unnecessary things during development, you need to find a way to avoid it, because it will slow down your development.
First of all, they should think about how to solve it well. If you can’t solve it, then you can refer to existing solutions (programming for the major search engines), learn their ideas of how to solve the problem, and see if you can live to learn and apply them.
Then we can optimize and encapsulate the existing wheels to make them more suitable for our business scenarios.