Injection of day38 10-spring's bean properties

Source: Internet
Author: User

After processing the bean, if it is to return the bean, then nothing is done directly to return the class to you intact.

It is logically enhanced when it executes some logical methods, such as time monitoring, rights management, logging, and so on.

It's OK to do the normal class enhancement, because normally you call the Add or find () method of this class.

Enhanced methods: Inheritance, decorator mode, dynamic Proxy.

The disadvantage of decorators is that if you have too many methods in your interface, and you only need to enhance one of them, you have to write a particularly special number of methods in this class. And all the other methods are returned intact to others.

Dynamic agents: the most flexible. To generate a proxy class for this class, proxy.newproxyinstance () generates a proxy class for the Bean class.

Public Object Postprocessafterinitialization (Final object Bean, String beanname) {

Not all classes are enhanced, and if you want to enhance all classes, generate the proxy class here.

Any method that invokes a business is equivalent to the Invoke () method that executes Invocationhandler

}

In fact, part of the idea of Spring AOP is based on post-processing beans.

 PackageCn.itcast.spring3.demo5;Importorg.junit.Test;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classSPRINGTEST5 {@Test Public voidDemo5 () {//Spring injects properties in two ways: one is the injection of the constructorApplicationContext ApplicationContext =NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); Car Car= (CAR) applicationcontext.getbean ("Car"));        SYSTEM.OUT.PRINTLN (car); } @Test Public voidDemo6 () {//There is also the injection of setter methodsApplicationContext ApplicationContext =NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); CAR2 car2= (CAR2) applicationcontext.getbean ("Car2");            System.out.println (CAR2); }            }
 PackageCn.itcast.spring3.demo5; Public classCAR2 {PrivateString name; PrivateString Price; PublicString GetName () {returnname;} Public voidsetName (String name) { This. Name =name;} PublicString GetPrice () {returnPrice ;} Public voidSetprice (String price) { This. Price =Price ;} @Override PublicString toString () {return"Car2 [name=" + name + ", price=" + Price + "]";} }
 PackageCn.itcast.spring3.demo5; Public classCar {PrivateString name; PrivateDouble Price;  PublicCar () {Super(); }     PublicCar (String name, Double price) {Super();  This. Name =name;  This. Price =Price ; } @Override PublicString toString () {return"Car [name=" + name + ", price=" + Price + "]"; }                    }
<?XML version= "1.0" encoding= "UTF-8"?><!--don't go to Schema,schema is file, local file, you have to lead that head -<Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" ><!--demo1 Quick Start ================================================= -<!--The interface and implementation classes are configured in this configuration file, and the full path to the implementation class can be used for factory reflection. -      <!--use a <bean> tag to set the class's information, using the id attribute as an identifier for the class. -    <!--interfaces, implementation classes, and configuration files are also available. -    <!--now there is a factory spring for us to provide, in fact, is to parse this XML file -    <!--will you write this factory yourself? You use dom4j to find the bean tag inside, find the attribute value of class, and then you can class.forname () to generate an instance of the class. In fact spring did, but the factory was provided by spring.  -    <BeanID= "HelloService"class= "Cn.itcast.spring3.demo1.HelloServiceImpl">         <!--using the <property> Tag Injection property value refers to the normal value ref refers to the object -    < Propertyname= "Info"value= "Preach Intelligence podcast"></ Property>    </Bean>    <!--demo1 Quick Start -    <!--instantiation of the Demo2bean -    <!--by default, a parameterless construction method is used. -    <!--<bean id= "Bean1" class= "Cn.itcast.spring3.demo2.Bean1" ></bean> -    <!--<bean name= "Bean1" class= "Cn.itcast.spring3.demo2.Bean1" ></bean> -    <!--The second use of static factory instantiation cannot write class, because now spring does not create objects for you directly. -    <!--<bean id= "bean2" class= "Cn.itcast.spring3.demo2.Bean2Factory" factory-method= "getBean2" ></bean> /c4> -    <!--The third instance of using instance factory instantiation -    <!--<bean id= "bean3" factory-bean= "bean3factory " factory-method= "GetBean3" ></bean> -    <!--to instantiate the Bean3factory first -    <!--<bean id= "bean3factory" class= "Cn.itcast.spring3.demo2.Bean3Factory" ></bean> -    <!--instantiation of Demo2bean ====================== end -        <!--Demo3bean scope of action ======================= -    <!--<bean id= "Customer" class= "Cn.itcast.spring3.demo3.Customer" scope= "prototype" ></bean> -    <!--<bean id= "Product" class= "cn.itcast.spring3.demo3.Product" init-method= "Setup" destroy-method= "teardown" Scope= "Singleton" > <property name= "name" value= "Air Conditioning" > -       <!--inject the property name of the product class in -       <!--</property> </bean> -       <!--the life cycle of Demo4bean ======================= -       <BeanID= "CustomerService"class= "Cn.itcast.spring3.demo4.CustomerServiceImpl"Init-method= "Setup"Destroy-method= "Teardown">                 < Propertyname= "Name"value= "Itcast"></ Property>       </Bean>       <!--The post-processing bean is automatically called by the spring container without your tube, and we have an ID for us to get it in the program. But this class is not available to us and is automatically called by spring. Cn.itcast.spring3.demo4.MyBeanPostProcessor is a post-processing bean -        <Beanclass= "Cn.itcast.spring3.demo4.MyBeanPostProcessor"></Bean>       <!--Demo5bean Attribute Injection ============================================================================================= ======================================== -       <!--injection of construction methods -       <BeanID= "Car"class= "Cn.itcast.spring3.demo5.Car">       <!--<constructor-arg name= "name" value= "BMW" ></constructor-arg> -        <!--Use this tag to inject properties into a class -        <!--<constructor-arg name= "Price" value= "1000000" ></constructor-arg> -        <!--Use this tag to inject properties into a class -        <Constructor-argIndex= "0"type= "Java.lang.String"value= "Mercedes-Benz"></Constructor-arg><!--Use this tag to inject properties into a class -        <Constructor-argIndex= "1"type= "Java.lang.Double"value= "2000000"></Constructor-arg><!--Use this tag to inject properties into a class -              </Bean>              <BeanID= "Car2"class= "Cn.itcast.spring3.demo5.Car2">       <!--in the <property> tag, name is the attribute name, value is the values of the normal property, ref: References other objects -       < Propertyname= "Name"value= "Porsche"></ Property>       < Propertyname= "Price"value= "5000000"></ Property>       </Bean>              </Beans>

Injection of day38 10-spring's bean properties

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.