Spring Framework Learning Notes (5)--automatic assembly

Source: Internet
Author: User

1, the bean attribute can be automatically assembled by the Autowire attribute of the beans tag.

Create a new spring profile beans-autowire.xml, where we have configured 3 bean,address, Car, people, where people automatically assembles another two beans, The Autowire property of the bean tag is used, in this example, if the car property of the person bean that the car is printed to car2 is null, that is, the byname is automatically assembled according to the Bean setter style and is not installed. With Bytype, if you configure two beans of the same type, you will get an error.

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">    <BeanID= "Address"class= "Com.broadtext.beans.autowire.Address">        < Propertyname= "City"value= "Hefei"></ Property>        < Propertyname= "Street"value= "Feixilu"></ Property>    </Bean>        <BeanID= "Car"class= "Com.broadtext.beans.autowire.Car">        < Propertyname= "Brand"value= "Audi"></ Property>        < Propertyname= "Price"value= "300000"></ Property>    </Bean>        <!--You can use the Autowire property to specify the way automatic assembly, byname according to the Bean Setter style automatic assembly has not been installed, Bytype based on the type of bean and bean properties of the type of automatic assembly, more than one type matching is thrown exception  -    <BeanID= "Person"class= "Com.broadtext.beans.autowire.Person"Autowire= "ByName"P:name= "HJ"></Bean>  </Beans>

2, Address.java, Car.java, People.java, Main.java

Address.java

 PackageCom.broadtext.beans.autowire; Public classAddress {PrivateString City; PrivateString Street;  PublicString getcity () {returnCity ; }     Public voidsetcity (String city) { This. City =City ; }     PublicString Getstreet () {returnStreet; }     Public voidSetstreet (String Street) { This. Street =Street; } @Override PublicString toString () {return"Address [city=" + City + ", street=" + Street + "]"; }}

Car.java

 PackageCom.broadtext.beans.autowire; Public classCar {PrivateString brand; Private DoublePrice ;  PublicString Getbrand () {returnbrand; }     Public voidSetbrand (String brand) { This. Brand =brand; }     Public DoubleGetPrice () {returnPrice ; }     Public voidSetprice (DoublePrice ) {         This. Price =Price ; } @Override PublicString toString () {return"Car [brand=" + Brand + ", price=" + Price + "]"; }     PublicCar () {System.out.println ("Car ' s Construstor"); }}

People.java

 PackageCom.broadtext.beans.autowire; Public classPerson {PrivateString name; Privateaddress address; Privatecar Car;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicAddress getaddress () {returnaddress; }     Public voidsetaddress (address address) { This. Address =address; }     PublicCar Getcar () {returncar; }     Public voidSetcar (car car) { This. Car =car; } @Override PublicString toString () {return"Person [name=" + name + ", address=" + address + ", car=" + Car + "]"; }}

Main.java

 PackageCom.broadtext.beans.autowire;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classMain {/*** <p>description: Automatic assembly of test beans </p>*/     Public Static voidMain (string[] args) {ApplicationContext ctx=NewClasspathxmlapplicationcontext ("Beans-autowire.xml"); Person Person= (person) ctx.getbean (' person ');    SYSTEM.OUT.PRINTLN (person); }}

Spring Framework Learning Notes (5)--automatic assembly

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.