Spring类属性的配置

1.使用set方法存放类的属性
(1)类:

public class Student {
    private int stuNo;
    private String stuName;
    private int stuAge;
    
    public int getStuNo() {
        return stuNo;
    }

    public void setStuNo(int stuNo) {
        this.stuNo = stuNo;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public int getStuAge() {
        return stuAge;
    }

    public void setStuAge(int stuAge) {
        this.stuAge = stuAge;
    }

(2)IOC容器的配置:

<bean id="student" class="org.wpc.entity.Student" >
<!--   property:该class所代表的类的属性
        name:属性名
        value:属性值
     -->
        <property name="stuNo" value="2"></property>
        <property name="stuName" value="ls"></property>
        <property name="stuAge" value="24"></property>
    </bean>

(3)获取配置完的类

//Spring上下文对象:context
ApplicationContext context = new ClassPathXmlApplicationContext("org/wpc/xml/applicationContext.xml");
//执行springIOC容器中获取一个id为student的对象
Student student = (Student) context.getBean("student");

2.利用构造器放置类的属性
(1)类

public class Student {
    private int stuNo;
    private String stuName;
private int stuAge;
    public Student(int stuNo, String stuName, int stuAge) {
        this.stuNo = stuNo;
        this.stuName = stuName;
        this.stuAge = stuAge;
    }
}

(2)IOC容器的配置:

<bean id="student" class="org.wpc.entity.Student" >
<constructor-arg value="ls" name="stuName"></constructor-arg>
<constructor-arg value="1" name="stuAge"></constructor-arg>
<constructor-arg value="24" name="stuNo"></constructor-arg>
</bean>

(3)获取配置完的类

//Spring上下文对象:context
ApplicationContext context = new ClassPathXmlApplicationContext("org/wpc/xml/applicationContext.xml");
//执行springIOC容器中获取一个id为student的对象
Student student = (Student) context.getBean("student");

3.配置的类的属性存在引用类
(1)类:

public class Student {
    private int stuNo;
    private String stuName;
    private int stuAge;
    private Teacher stuTeacher;

    public Teacher getStuTeacher() {
        return stuTeacher;
    }

    public void setStuTeacher(Teacher stuTeacher) {
        this.StuTeacher = stuTeacher;
    }
    
    public int getStuNo() {
        return stuNo;
    }

    public void setStuNo(int stuNo) {
        this.stuNo = stuNo;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public int getStuAge() {
        return stuAge;
    }

    public void setStuAge(int stuAge) {
        this.stuAge = stuAge;
    }

(2)IOC容器的配置:

<bean id="student" class="org.wpc.entity.Student" >
<!--   property:该class所代表的类的属性
        name:属性名
        value:属性值
     -->
        <property name="stuNo" value="2"></property>
        <property name="stuName" value="ls"></property>
        <property name="stuAge" value="24"></property>
        <!--   使用ref对属性类进行引用     -->
        <property name="stuTeacher" ref="teacher"></property>
    </bean>

(3)获取配置完的类

//Spring上下文对象:context
ApplicationContext context = new ClassPathXmlApplicationContext("org/wpc/xml/applicationContext.xml");
//执行springIOC容器中获取一个id为student的对象
Student student = (Student) context.getBean("student");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值