setter如何在Spring Framework中运行? [英] How setter works inside Spring Framework?

查看:156
本文介绍了setter如何在Spring Framework中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Spring Framework的新手。实际上,我正在用 spring 进行实验。

I'm new in spring Framework. And actually i was doing an experiment with spring actually.

看看这个 HelloWorld.java

public class HelloWorld {

    private String messageee;

    public void setMessage(String messageee){
        this.messageee=messageee;
    }

    public void show(){
        System.out.println("message: "+messageee);
    }
}

你在这个程序中看到,我有一个变量在外面声明为 private 命名为 messageee 和下一个使用 setter <参数化的变量命名为 messageee 。你看到两者都有相同的名字。

You see in this program, I've one variable which is outside declared as private named as messageee and next variable which is parametrized with setter named as messageee. You see both have same name.

好的..现在看看这个bean文件:

Okay.. Now look at this bean file:

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">

    <bean id="helloWorld" class="com.springframework.HelloWorld">
        <property name="message" value="Hello.. This is Spring Framework example."></property>
    </bean>

</beans>

在这里你可以看到 bean 标签。我已将属性名称声明为消息。我不明白,当我将名称命名为 messageee 时,它会给出一个错误:

Here you see inside bean tag. I've declared the property name as message. I don't understand, when i give the name as messageee it gives an error as:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloWorld' defined in class path resource [beans.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'messageee' of bean class [com.springframework.HelloWorld]: Bean property 'messageee' is not writable or has an invalid setter method. Did you mean 'message'?

但当我将名称命名为消息 。它运行成功。但我没有任何消息的方法或任何类似名称的变量。那么, setter 如何实际运作?请详细说明一下?

But when i give the name as message. It runs successfully. But i don't have any message's method or any kind of variables with this similar name. So, How setter works actually? Will you please elaborate?

帮助将不胜感激!

推荐答案

您将字段(或实例变量)与属性混淆。

属性是来自Java Beans规范的术语。 bean的属性 foo 是可以使用名为 getFoo()(或<$的getter方法访问的数据c $ c> isFoo()表示布尔值)和/或使用名为 setFoo()的setter方法设置。

property is a term coming from the Java Beans specification. A property foo of a bean is data that can be accessed using a getter method called getFoo() (or isFoo() for a boolean) and/or set using a setter method called setFoo().

这些方法在内部执行的操作,无论是否获取/设置变量,变量是否也被命名为foo或其他任何内容都完全无关紧要。重要的是getter / setter的名称。

What these methods do internally, whether they get/set a variable or not, whether the variable is also named foo or anything else, is completely irrelevant. What matters is the name of the getter/setter.

因此,当您定义bean并告诉Spring设置名为 message 的属性时, Spring将查找名为 setMessage()的方法。它不关心bean类的私有字段。

So, when you define your bean and tell Spring to set the property named message, Spring will look for a method called setMessage(). It doesn't care about the private fields of your bean class.

这篇关于setter如何在Spring Framework中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆