• 手动装配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

    http://blog.csdn.net/evankaka/article/details/45023835

  • 相关阅读:
    Oozie时bin/oozied.sh start或bin/oozied.sh run出现Bootstrap进程无法启动,http://bigdatamaster:11000/oozie界面也无法打开?
    [ACM] POJ 2253 Frogger (最短路径变形,每条通路中的最长边的最小值)
    Echoprint系列--Android编译与调用
    shell编程之文本与日志过滤
    C++中搜索、截取字符串
    Swift中的UIKit重力学
    hbase0.96 put流程 源码分析
    [Docker]初次接触
    工作日志2014-08-25
    Flex和Servlet结合上传文件报错(二)
  • 原文地址:https://www.cnblogs.com/hongdada/p/6432275.html
Copyright © 2020-2023  润新知