zoukankan      html  css  js  c++  java
  • Spring+Maven学习实验- Spring 中给 Bean 属性注入value(二)

    1.修改pom.xml

       详细请看实验一

    2.创建Person和Customer类

    package com.shiyanlou.spring.innerbean;
    
    public class Person {
        private String name;
        private String address;
        private int age;
        
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getAddress() {
            return address;
        }
        public void setAddress(String address) {
            this.address = address;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        
        @Override
        public String toString() {
            return "Person [name=" + name + ", address=" + address + ", age=" + age + "]";
        }
        
        
    
    }
    package com.shiyanlou.spring.innerbean;
    
    public class Customer {
        private Person person;
        //带参构造方法
        public Customer(Person person){
            this.person = person;
        }
        public Customer() {}
        
        public Person getPerson() {
            return person;
        }
        public void setPerson(Person person) {
            this.person = person;
        }
        @Override
        public String toString() {
            return "Customer [person=" + person + "]";
        }
        
        
    }

    3.创建Customerbeans.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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
        
        <!--方法一:利用 ref 很好的引用了 Person ,但是,一旦 Person 被用在 Customer 下,也就不会被别的 Bean 引用-->
        <!--  
        <bean id="CustomerBean" class="com.shiyanlou.spring.innerbean.Customer">
           <property name="Person" ref="PersonBean"/>
        </bean>
        
        <bean id="Person" class="com.shiyanlou.spring.innerbean.Person">
           <property name="name" value="zoey"/>
           <property name="address" value="Wuhan"/>
           <property name="age" value="27"/>
        </bean>
       -->
       <!-- 方法二:在 Customer 的 Bean 中声明一个内部 Bean  -->
       <bean id="Customer" class="com.shiyanlou.spring.innerbean.Customer">
           <property name="person">
              <bean class="com.shiyanlou.spring.innerbean.Person">
                 <property name="name" value="zoey"/>
                 <property name="address" value="Wuhan"/>
                 <property name="age" value="27"/>
              </bean>
           </property>   
       </bean>
       
       <!-- 内部 Bean 也可以通过构造函数注入 -->
       <!-- 
           <bean id="Customer" class="com.shiyanlou.spring.innerBean.Customer">
            <constructor-arg>
                <bean class=com.shiyanlou.spring.innerBean.Person">
                    <property name="name" value="shiyanlou" />
                    <property name="address" value="chengdu" />
                    <property name="age" value="25" />
                </bean>
            </constructor-arg>
           </bean>
        -->
    </beans>

    4.创建测试类app.java

    package com.shiyanlou.spring.innerbean;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class App {
        private static ApplicationContext context;
    
        public static void main(String[] args) {
            context = new ClassPathXmlApplicationContext("CustomerBeans.xml");
            Customer customer = (Customer)context.getBean("Customer"); //Customer为Customerbeans.xml中的bean id
            //customer.toString();
            System.out.println(customer.toString());
    
        }
    
    }

    运行App.java后效果

  • 相关阅读:
    Java基础——clone()方法浅析
    Unity shader error: “Too many texture interpolators would be used for ForwardBase pass”
    ar 解压一个.a文件报错: xxx.a is a fat file (use libtool(1) or lipo(1) and ar(1) on it)
    How to set up "lldb_codesign" certificate!
    Unity-iPhone has Conflicting Provisioning Settings
    ETC1/DXT1 compressed textures are not supported when publishing to iPhone
    Xcode 提交APP时遇到 &ldquo;has one iOS Distribution certificate but its private key is not installed&rdquo;
    XCode iOS之应用程序标题本地化
    苹果电脑(Mac mini或Macbook或iMac)恢复出厂设置
    Unity 4.7 导出工程在XCode10.1上编译报错
  • 原文地址:https://www.cnblogs.com/zoeyqq/p/6547468.html
Copyright © 2011-2022 走看看