• Spring内部bean实例


    在Spring框架中,一个bean仅用于一个特定的属性,这是提醒其声明为一个内部bean。内部bean支持setter注入“property”和构造器注入"constructor-arg“。
    下面来看看一个详细的例子,演示使用 Spring 内部 bean 。
    package com.yiibai.common;
    
    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 + "]";
    	}
    }
    package com.yiibai.common;
    
    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 + "]";
    	}	
    }
    很多时候,可以使用 'ref' 属性来引用“Person” bean到“Customer” Bean,person的属性如下:
    <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.yiibai.common.Customer">
    		<property name="person" ref="PersonBean" />
    	</bean>
    
    	<bean id="PersonBean" class="com.yiibai.common.Person">
    		<property name="name" value="yiibai" />
    		<property name="address" value="address1" />
    		<property name="age" value="28" />
    	</bean>
    	
    </beans>
    在一般情况下,引用这样也没有问题,但由于“yiibai” persion bean 只用于Customer bean,这是更好地声明 “yiibai” person 作为一个内部 bean,如下:
    <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.yiibai.common.Customer">
    		<property name="person">
    			<bean class="com.yiibai.common.Person">
    				<property name="name" value="yiibai" />
    				<property name="address" value="address1" />
    				<property name="age" value="28" />
    			</bean>
    		</property>
    	</bean>
    </beans>
    内部 bean 也支持构造器注入如下:
    <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.yiibai.common.Customer">
    		<constructor-arg>
    			<bean class="com.yiibai.common.Person">
    				<property name="name" value="yiibai" />
    				<property name="address" value="address1" />
    				<property name="age" value="28" />
    			</bean>
    		</constructor-arg>
    	</bean>
    </beans>
    注意:
    id 或 name 值在bean类是没有必要以一个内部 bean 呈现,它会简单地忽略Spring容器。

    执行结果:

    package com.yiibai.common;
    
    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);
         
        }
    }

    输出结果:

    Customer [person=Person [address=address1, age=28, name=yiibai]]
  • 相关阅读:
    flutter 计算时间日期 在当年的第几周
    Flutter Future 异步 FutureBuilder 获取数据后 加载Widget
    macOS server 中描述文件管理器开启提示“发生错误,代码为-1”
    VS Code 添加chrome调试及localhost 拒绝连接请求问题
    Finished with error: Error running pod install (Android Studio 运行flutter项目)
    flutter moudle 项目编译生成 .ios 和 .android 更改.xx项目代码不被覆盖 flutter make-host-app-editable
    flutter项目 通道Channel封装及使用案例
    react-native 调用 TouchableOpacity (触摸透明) 时报了一个警告
    webstorm2016.2 for mac 安装
    Computed read-only property vs function in Swift
  • 原文地址:https://www.cnblogs.com/soundcode/p/6367249.html
Copyright © 2020-2023  润新知