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