Spring阶段性学习总结(二)构造器的方式填充Bean

 1 package SpringBeanDeploy;
 2 
 3 public class Student {
 4     private  String name;
 5     private  int age;
 6     private  String sex;
 7 
 8     public Student(String name, int age) {
 9         this.name = name;
10         this.age = age;
11     }
12 
13     public Student(String name, int age, String sex) {
14         this.name = name;
15         this.age = age;
16         this.sex = sex;
17     }
18 
19     @Override
20     public String toString() {
21         return "Student{" +
22                 "name='" + name + '\'' +
23                 ", age=" + age +
24                 ", sex='" + sex + '\'' +
25                 '}';
26     }
27 }

 

 1 package SpringBeanDeploy;
 2 
 3 import org.springframework.context.support.ClassPathXmlApplicationContext;
 4 
 5 public class Main {
 6     public static void main(String[] args) {
 7         ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("SpringBeanDeploy/SpringapplicationContext.xml");
 8         Student student = (Student) applicationContext.getBean("Student");
 9         System.out.println(student);
10         student = (Student) applicationContext.getBean("Student1");
11         System.out.println(student);
12     }
13 }

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 5     <bean id="Student" class="SpringBeanDeploy.Student">
 6         <constructor-arg value="张浩" index="0"></constructor-arg>
 7         <constructor-arg value="18"></constructor-arg>
 8         <constructor-arg value="男" index="2"></constructor-arg>
 9     </bean>
10     <bean id="Student1" class="SpringBeanDeploy.Student">
11         <constructor-arg value="7" index="1"></constructor-arg>
12         <constructor-arg value="李奔" index="0"></constructor-arg>
13     </bean>
14 </beans>

 

转载于:https://www.cnblogs.com/zhang188660586/p/11557060.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值