鍍金池/ 教程/ Java/ Spring內(nèi)部Bean實(shí)例
Spring EL hello world實(shí)例
Spring使用@Autowired注解自動(dòng)裝配
MapFactoryBean實(shí)例
Bean InitializingBean和DisposableBean實(shí)例
Spring使用@Required注解依賴(lài)檢查
安裝Spring工具套件到Eclipse
CGLIB is required to process @Configuration classes
Spring EL Arrays, Lists, Maps實(shí)例
Spring由類(lèi)型(Type)自動(dòng)裝配
Spring在bean配置文件中定義電子郵件模板
Spring發(fā)送帶附件郵件
ListFactoryBean實(shí)例
Struts 2 + Spring + Hibernate集成實(shí)例
Spring bean引用實(shí)例
Spring JdbcTemplate batchUpdate()實(shí)例
Spring SetFactoryBean實(shí)例
Spring通過(guò)setter方法注入
Spring EL hello world實(shí)例
在bean配置文件中的電子郵件模板
Spring發(fā)送帶附件的Email
Spring EL 方法調(diào)用實(shí)例
Spring自動(dòng)裝配Beans
Spring由構(gòu)造方法自動(dòng)裝配
Spring使用@Required注解依賴(lài)檢查
Spring SimpleJdbcTemplate查詢(xún)示例
自定義@Required-style注解
Spring+JDBC實(shí)例
Spring EL三元運(yùn)算(if-then-else)實(shí)例
JdbcTemplate+JdbcDaoSupport實(shí)例
Spring自動(dòng)掃描組件
Spring EL方法調(diào)用實(shí)例
Spring自動(dòng)裝配Beans
Spring EL bean引用實(shí)例
Spring依賴(lài)注入(DI)
Spring EL Lists,Maps實(shí)例
Spring通過(guò)構(gòu)造方法依賴(lài)注入
通過(guò)MailSender發(fā)送電子郵件
Spring AOP實(shí)例(Pointcut, Advisor)
Spring EL運(yùn)算符實(shí)例
Spring JavaConfig實(shí)例
Spring依賴(lài)注入 (DI)
Spring EL bean引用實(shí)例
Spring由AutoDetect自動(dòng)裝配
Spring由構(gòu)造方法自動(dòng)裝配
Spring JdbcTemplate查詢(xún)實(shí)例
Spring SimpleJdbcTemplate batchUpdate()實(shí)例
Spring+JDBC實(shí)例
如何注入值到Spring bean屬性
Spring SimpleJdbcTemplate類(lèi)命名參數(shù)實(shí)例
Spring PropertyPlaceholderConfigurer實(shí)例
Spring JdbcTemplate batchUpdate() 實(shí)例
Spring EL與ExpressionParser測(cè)試
Spring內(nèi)部Bean實(shí)例
加載多個(gè)Spring bean配置文件
Spring+Hibernate+MySql實(shí)例(注解)
Spring AOP+AspectJ 在XML配置實(shí)例
Spring自動(dòng)代理創(chuàng)建者實(shí)例
Spring EL正則表達(dá)式實(shí)例
測(cè)試 Spring EL與ExpressionParser
java.lang.ClassNotFoundException: org.exolab.castor.xml.XMLExcep
Spring PropertyPlaceholderConfigurer實(shí)例
Spring bean配置繼承
Spring使用Setter依賴(lài)注入
Spring自動(dòng)裝配@Qualifier實(shí)例
Spring依賴(lài)檢查
Spring hello world實(shí)例
Spring依賴(lài)注入servlet會(huì)話(huà)監(jiān)聽(tīng)器
Spring自定義@Required-style注解
Spring bean作用域
Spring AOP攔截器的序列
注入值到Spring Bean的屬性
Spring JdbcTemplate+JdbcDaoSupport實(shí)例
Spring教程
Spring自動(dòng)裝配@Qualifier實(shí)例
Spring MapFactoryBean例子
Spring Bean作用域?qū)嵗?/span>
Cannot proxy target class because CGLIB2 is not available
Spring AOP攔截器的順序
Spring集合(List, Set, Map, and Properties)實(shí)例
Spring SimpleJdbcTemplate類(lèi)命名參數(shù)實(shí)例
Spring通過(guò)自動(dòng)檢測(cè)自動(dòng)裝配
Spring+Hibernate+MySql實(shí)例
Spring JavaConfig @Import實(shí)例
訪(fǎng)問(wèn)MessageSource的bean(MessageSourceAware)
Spring JdbcTemplate查詢(xún)實(shí)例
Spring JavaConfig @Import實(shí)例
Spring Object/XML映射實(shí)例
Spring Bean引用例子
Spring AOP+AspectJ注解實(shí)例
java.lang.ClassNotFoundException: org.apache.xml.serialize.XMLSe
Spring依賴(lài)檢查
Spring JavaConfig實(shí)例
Spring AOP+AspectJ注解實(shí)例
Spring bean配置繼承
Spring自動(dòng)掃描組件
自動(dòng)代理創(chuàng)建者實(shí)例
Spring Bean init-method 和 destroy-method實(shí)例
Spring通過(guò)構(gòu)造方法注入
Spring過(guò)濾器組件自動(dòng)掃描
Spring構(gòu)造方法注入類(lèi)型歧義
Spring+JDBC實(shí)例
Spring EL正則表達(dá)式實(shí)例
Spring通過(guò)Gmail SMTP服務(wù)器MailSender發(fā)送電子郵件
Spring AOP+AspectJ在XML配置實(shí)例
SetFactoryBean實(shí)例
Spring注入日期到bean屬性-CustomDateEditor
Spring AOP通知實(shí)例 – Advice
Spring使用@Autowired注解自動(dòng)裝配
Spring AOP實(shí)例(Pointcut,Advisor)
Spring AOP在Hibernate的事務(wù)管理
Spring初學(xué)快速入門(mén)
Spring內(nèi)部bean實(shí)例
Spring bean加載多個(gè)配置文件
Spring + Hibernate+ MySql注解實(shí)例
Spring @PostConstruct和@PreDestroy實(shí)例
Spring依賴(lài)注入servlet會(huì)話(huà)監(jiān)聽(tīng)器
Spring ListFactoryBean實(shí)例
Spring松耦合的實(shí)例
Spring過(guò)濾器組件自動(dòng)掃描
Spring按名稱(chēng)(Name)自動(dòng)裝配
Spring AOP通知實(shí)例(Advice)
Spring AOP在Hibernate事務(wù)管理
Bean init-method和destroy-method實(shí)例
Spring EL操作符實(shí)例
Spring注入日期到bean屬性(CustomDateEditor)
資源加載程序的getResource()示例
Bean @PostConstruct和@PreDestroy實(shí)例
Struts2+Spring集成實(shí)例
Spring集合 (List,Set,Map,Properties) 實(shí)例
Spring按類(lèi)型(Type)自動(dòng)裝配
Spring由名稱(chēng)(Name)自動(dòng)裝配
Spring SimpleJdbcTemplate batchUpdate()實(shí)例
Spring hello world實(shí)例
Spring Bean InitializingBean和DisposableBean實(shí)例
Spring構(gòu)造方法注入類(lèi)型歧義
Spring EL三元操作符(if-then-else)實(shí)例
Spring+Hibernate+MySql實(shí)例
Spring松耦合實(shí)例

Spring內(nèi)部Bean實(shí)例

在Spring框架中,一個(gè)bean僅用于一個(gè)特定的屬性,這是提醒其聲明為一個(gè)內(nèi)部bean。內(nèi)部bean支持setter注入“property”和構(gòu)造器注入"constructor-arg“。
下面來(lái)看看一個(gè)詳細(xì)的例子,演示使用 Spring 內(nèi)部 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 + "]";
	}	
}
很多時(shí)候,可以使用 'ref' 屬性來(lái)引用“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>
在一般情況下,引用這樣也沒(méi)有問(wèn)題,但由于“yiibai” persion bean 只用于Customer bean,這是更好地聲明 “yiibai” person 作為一個(gè)內(nèi)部 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>
內(nèi)部 bean 也支持構(gòu)造器注入如下:
<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類(lèi)是沒(méi)有必要以一個(gè)內(nèi)部 bean 呈現(xiàn),它會(huì)簡(jiǎn)單地忽略Spring容器。

執(zhí)行結(jié)果:

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);
     
    }
}

輸出結(jié)果:

Customer [person=Person [address=address1, age=28, name=yiibai]]