zoukankan      html  css  js  c++  java
  • Spring中使用inner bean

    有点类似java 内部类。看个demo。假设有下面的一个bean:

    public class Customer 
    {
    	private Person person;
     
    	public Customer(Person person) {
    		this.person = person;
    	}
     
    	public void setPerson(Person person) {
    		this.person = person;
    	}
     
    	@Override
    	public String toString() {
    		return "Customer [person=" + person + "]";
    	}
    }
    

      

    public class Person 
    {
    	private String name;
    	private String address;
    	private int age;
     
    	//getter and setter methods
     
    	@Override
    	public String toString() {
    		return "Person [address=" + address + ", 
                                   age=" + age + ", name=" + name + "]";
    	}	
    }
    

      

    <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-2.5.xsd">
     
    	<bean id="CustomerBean" class="com.mkyong.common.Customer">
    		<property name="person" ref="PersonBean" />
    	</bean>
     
    	<bean id="PersonBean" class="com.mkyong.common.Person">
    		<property name="name" value="mkyong" />
    		<property name="address" value="address1" />
    		<property name="age" value="28" />
    	</bean>
     
    </beans>
    

      

    <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-2.5.xsd">
     
    	<bean id="CustomerBean" class="com.mkyong.common.Customer">
    		<property name="person">
    			<bean class="com.mkyong.common.Person">
    				<property name="name" value="mkyong" />
    				<property name="address" value="address1" />
    				<property name="age" value="28" />
    			</bean>
    		</property>
    	</bean>
    </beans>
    

      我们也可以使用构造函数注入

    <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-2.5.xsd">
     
    	<bean id="CustomerBean" class="com.mkyong.common.Customer">
    		<constructor-arg>
    			<bean class="com.mkyong.common.Person">
    				<property name="name" value="mkyong" />
    				<property name="address" value="address1" />
    				<property name="age" value="28" />
    			</bean>
    		</constructor-arg>
    	</bean>
    </beans>
    

      Note
    注意,在inner bean中,id和name不是必须的,因为他们会被容器忽略。

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
     
    public class App 
    {
        public static void main( String[] args )
        {
        	ApplicationContext context = 
        	  new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"});
     
        	Customer cust = (Customer)context.getBean("CustomerBean");
        	System.out.println(cust);
     
        }
    }
    

      


    ==============================================================================

    本博客已经废弃,不在维护。新博客地址:http://wenchao.ren


    我喜欢程序员,他们单纯、固执、容易体会到成就感;面对压力,能够挑灯夜战不眠不休;面对困难,能够迎难而上挑战自我。他
    们也会感到困惑与傍徨,但每个程序员的心中都有一个比尔盖茨或是乔布斯的梦想“用智慧开创属于自己的事业”。我想说的是,其
    实我是一个程序员

    ==============================================================================
  • 相关阅读:
    System.arraycopy
    关于验证控件和javaSript验证的共存问题
    正则表达式经典
    css的一些基础的东西
    运用JAVASCRIPT,写一个类,类名:student,他的属性:name,age,tall,他的方法:getName,getAge,getTall
    转: ASP.NET 应用程序生命周期概述
    转:关于 Global.asax 文件
    今天又要过去了,学习点东西!
    javaScript对象和属性
    转载:.NET 2005 实现在线人数统计
  • 原文地址:https://www.cnblogs.com/rollenholt/p/2835101.html
Copyright © 2011-2022 走看看