首页 > 代码库 > 手动装配Bean
手动装配Bean
代码:
import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); Person person = (Person) ctx.getBean("person"); System.out.println(person); }} class Car { private String brand; private double price; public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public String toString() { return "Car [brand=" + brand + ", price=" + price + "]"; }} class Address { private String city; private String street; public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } @Override public String toString() { return "Address [city=" + city + ", street=" + street + "]"; }} class Person { private String name; private Address address; private Car car; public String getName() { return name; } public void setName(String name) { this.name = name; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } @Override public String toString() { return "Person [name=" + name + ", address=" + address + ", car=" + car + "]"; }}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="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.xsd"> <!-- 手动装配Bean --> <bean id="address" class="com.company.Address" p:city="WhuHan" p:street="Bayi"></bean> <bean id="car" class="com.company.Car" p:brand="Audi" p:price="300000"></bean> <bean id="person" class="com.company.Person" p:name="Roger" p:address-ref="address" p:car-ref="car"></bean> </beans>
结果:
Person [name=Roger, address=Address [city=WhuHan, street=Bayi], car=Car [brand=Audi, price=300000.0]]
对applicationContext.xml进行修改
<bean id="person" class="com.company.Person" p:name="Roger" ></bean>
结果:
Person [name=Roger, address=null, car=null]
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="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.xsd"> <!-- 手动装配Bean --> <bean id="address" class="com.company.Address" p:city="WhuHan" p:street="Bayi"></bean> <bean id="car" class="com.company.Car" p:brand="Audi" p:price="300000"></bean> <bean id="person" class="com.company.Person" p:name="Roger" autowire="byName"></bean></beans>
Person [name=Roger, address=Address [city=WhuHan, street=Bayi], car=Car [brand=Audi, price=300000.0]]
使用自动装配auotwire="byName"
将上面的autowire改成byType一样可以运行,结果也一样。
自动检测Bean
<context:component-scan base-package="com.company"> </context:component-scan>
https://my.oschina.net/u/1020238/blog/502599
https://my.oschina.net/u/1020238/blog/502930
手动装配Bean
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。