【问题标题】:How setter works inside Spring Framework?在 Spring Framework 中,setter 是如何工作的?
【发布时间】:2015-01-25 14:18:15
【问题描述】:

我是 spring 框架的新手。实际上,我实际上是在用spring 做一个实验。

看看这个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。您会看到两者具有相同的名称。

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

<?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 标记的内部。我已将属性名称声明为message。我不明白,当我将名称命名为 messageee 时,它给出的错误如下:

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'?

但是当我将名称命名为 message 时。它运行成功。但是我没有任何消息的方法或任何具有这个相似名称的变量。那么,setter 实际上是如何工作的呢?能详细点吗?

我们将不胜感激!

【问题讨论】:

    标签: java spring spring-mvc setter getter-setter


    【解决方案1】:

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

    property 是一个来自 Java Beans 规范的术语。 bean 的属性foo 是可以使用称为getFoo()(或isFoo() 用于布尔值)的getter 方法访问和/或使用称为setFoo() 的setter 方法设置的数据。

    这些方法在内部做什么,它们是否获取/设置变量,变量是否也被命名为 foo 或其他任何东西,完全无关紧要。重要的是 getter/setter 的名称。

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

    【讨论】:

    • 是的.. 你真的明白我的意思了!!.. 所以,当我给属性命名为message ..Spring 将寻找一个名为setMessage().. 他们是@987654328 @通过这个意义?..这就是我们所说的Ioc containerauto-wiredSpring的功能?
    • 我不明白你在问什么。自动装配只是让 Spring 通过类型和注释发现 bean 依赖项,而不是通过在 XML 中定义所有内容。
    • 对不起我的英语不好..我是学生!..你知道吗,当我使用setFoo(String messageee).. bean 文件开始嘲笑错误.. 但是当我更改我的属性名称时作为foo。最后它停止给出这些错误。 :)
    【解决方案2】:

    Spring IoC容器也支持setter注入,这是Spring中依赖注入的首选方式。 Setter 注入使用类文件中的 set* 方法来获取可在 Spring XML 配置中配置的属性名称。

    从配置的角度来看,setter 注入更容易阅读,因为被设置的属性名称与被注入的值一起作为属性分配给 bean。

    为了确定属性名称,Spring 遵循JavaBeans Specification

    【讨论】:

      【解决方案3】:

      首先,您将字段与属性混合在一起 - 而且您在 applicationContext.xml 中的属性名称是错误的(应该是 messageee

      你需要使用@Autowired注解:

      1) 字段,即messageee

      2) 设置器,即setMessage()

      如果你在想“那是什么!!???”阅读 Spring 使用 bean 的基本特性,以及 Spring 如何使用 IoC 框架获取 POJO(普通旧 Java 对象)并对其进行配置。在此处阅读@Autowired - How does autowiring work in Spring?

      那么你应该没问题:

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

      顺便说一句……通过使用非常基本的 Java 东西来研究 Spring 的好方法……祝你好运!

      【讨论】:

      • 你的意思是@Autowired
      猜你喜欢
      • 1970-01-01
      • 2022-11-05
      • 2011-01-03
      • 2014-03-23
      • 2014-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多