博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring中使用inner bean
阅读量:7050 次
发布时间:2019-06-28

本文共 2279 字,大约阅读时间需要 7 分钟。

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 + 
"]"
;
    
}
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 + "
]";
    
}  
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<
beans 
xmlns=""
    
xmlns:xsi=""
    
xsi:schemaLocation="
    
">
  
    
<
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
>

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<
beans 
xmlns=""
    
xmlns:xsi=""
    
xsi:schemaLocation="
    
">
  
    
<
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
>

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<
beans 
xmlns=""
    
xmlns:xsi=""
    
xsi:schemaLocation="
    
">
  
    
<
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不是必须的,因为他们会被容器忽略。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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://bhvol.baihongyu.com/

你可能感兴趣的文章
快速了解linux压缩与解压
查看>>
SQL :多条记录取最前面一条或根据条件任取N条。。。。。。
查看>>
再谈javascript 获取服务器控件值的
查看>>
Android官方数据绑定框架DataBinding(三)
查看>>
ioctl 函数
查看>>
JAVA实战教程_JAVA案例开发之JAVA开发微信二维码大数据系统02
查看>>
我的友情链接
查看>>
关于swoole
查看>>
Linux下怎样搭建 SVN 服务器
查看>>
12 种编程语言的起源故事
查看>>
不同数据结构的比较原理
查看>>
SSH错误解决
查看>>
sqlalchemy备忘 笔记
查看>>
Python数值和字符串
查看>>
python学习笔记一
查看>>
Gym 100283F Bakkar In The Army
查看>>
POJ 2947 2947 Widget Factory 高斯消元
查看>>
ExtJS-3.4.0系列:Ext.TabPanel
查看>>
Sql Server系列:键和约束
查看>>
服务器负载感知的URL HASH
查看>>