• Spring4.0学习笔记(2) —— 自动装配


    Spring IOC 容器可以自动装配Bean,需要做的是在<bean>的autowire属性里指定自动装配的模式

    1)byType 根据类型自动装配,若IOC 容器中有多个目标Bean类型一致的Bean,Spring将无法判定哪个Bean最适合该属性,不能执行自动装配

    2)byName 根据名称自动装配,必须将目标Bean名称和属性名设置的完全相同

    配置方法:

    Address.java

    package com.spring.autowire;
    
    public class Address {
        private String city;
        private String location;
        public String getCity() {
            return city;
        }
        public void setCity(String city) {
            this.city = city;
        }
        public String getLocation() {
            return location;
        }
        public void setLocation(String location) {
            this.location = location;
        }
        @Override
        public String toString() {
            return "Address [city=" + city + ", location=" + location + "]";
        }
    }

    2、Car.java

    package com.spring.autowire;
    
    public class Car {
        private String brand;
        private String price;
        public String getBrand() {
            return brand;
        }
        public void setBrand(String brand) {
            this.brand = brand;
        }
        public String getPrice() {
            return price;
        }
        public void setPrice(String price) {
            this.price = price;
        }
        @Override
        public String toString() {
            return "Car [brand=" + brand + ", price=" + price  + "]";
        }
    }

    3、Person.java

    package com.spring.autowire;
    
    public class Person {
        private String name;
        private String age;
        private Address address;
        
        @Override
        public String toString() {
            return "Person [address=" + address + ", age=" + age + ", car=" + car
                    + ", name=" + name + "]";
        }
        private Car car;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getAge() {
            return age;
        }
        public void setAge(String age) {
            this.age = age;
        }
        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;
        }
    }

    4、配置文件bean-autowire.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-3.0.xsd">
    
        <bean id="car" class="com.spring.autowire.Car">
            <property name="brand" value="Audi"></property>
            <property name="price" value="300000"></property>
        </bean>
        
        <bean id="address" class="com.spring.autowire.Address">
            <property name="city" value="beijing"></property>
            <property name="location" value="fangshan"></property>
        </bean>
        
        <bean id="person" class="com.spring.autowire.Person" autowire="byName">
        </bean>
    </beans>

    5、测试

    package com.spring.autowire;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class main {
        public static void main(String[] args) {
            ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-autowire.xml");
            Person person = (Person)ctx.getBean("person");
            System.out.println(person);
        }
    }

      可以使用autowire 属性指定自动装配的方式,byName 根据bean 的名字和当前bean的setter风格的属性名进行自动装配 

      byType根据bean的类型和当前bean的属性类型进行自动装配,若 IOC 容器中有1个以上的类型的bean,则抛出异常              

  • 相关阅读:
    C++中字符串与字符串函数的使用
    面试题17:打印从1到最大的n位数
    动态规划:0-1背包
    POJ 2386 Lake Counting (简单深搜)
    HDU 2612 Find a way 简单广搜
    POJ 3669 Meteor Shower (BFS+预处理)
    深搜(DFS)广搜(BFS)详解
    HDU6055 Regular polygon(计算几何)
    hdu 6047 Maximum Sequence 贪心
    hdu 6045 Is Derek lying?(思维推导)
  • 原文地址:https://www.cnblogs.com/cklovefan/p/5283755.html
Copyright © 2020-2023  润新知