Spring中使用inner bean

有点类似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
     xsi:schemaLocation="http://www.springframework.org/schema/beans
  
     < 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
     xsi:schemaLocation="http://www.springframework.org/schema/beans
  
     < 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
     xsi:schemaLocation="http://www.springframework.org/schema/beans
  
     < 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);
  
     }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值