The use of Beanpostprocessor to manipulate objects before or after initialization of an object

Source: Internet
Author: User


Import Java.lang.reflect.Field;
Import Org.springframework.beans.beansexception;import Org.springframework.beans.factory.config.BeanPostProcessor;
public class Uppercasemodifier implements Beanpostprocessor {
@Overridepublic object Postprocessafterinitialization (object bean, String name)Throws Beansexception {TODO auto-generated Method StubSystem.out.println ("Object" + name + "instantiation complete");return bean;}
@Overridepublic object Postprocessbeforeinitialization (object bean, String name)Throws Beansexception {System.out.println ("Object" + Name + "Start instantiation");Fetch all attributes of beanfield[] fields = Bean.getclass (). Getdeclaredfields ();for (int i=0; i<fields.length; i++) {Determine the Name fieldif (Fields[i].getname (). Equals ("name")) {Set this field to accessFields[i].setaccessible (TRUE);try {String original = (string) fields[i].get (bean);System.out.println (original);Fields[i].set (Bean, original.touppercase ());} catch (Exception e) {E.printstacktrace ();}}}return bean;}
The above code implements the lowercase letters in the Name property value into uppercase letters; The Beanpostprocessor interface consists of two methods:
Postprocessafterinitialization: Operation after instantiation
Postprocessbeforeinitialization: Operation before instantiation
to introduce the class in the configuration file: <bean id="Uppercasemodifier" class="Uppercasemodifier" ></bean>The slag has to be stuck in the Bug.


From for notes (Wiz)

The use of Beanpostprocessor to manipulate objects before or after initialization of an object

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.