学习spring时使用静态工厂出现问题

来源:3-7 自由编程

軍度

2022-04-07 19:48:52

package cn.bhu.factory;


import cn.bhu.beans.Clothes;


public class ClothesFactory {

  public static Clothes create1() {

  Clothes one=new Clothes();

  one.setStyle("连衣裙");

  one.setColor("红色");

 

  return clothes;

  }

}

 

https://img.mukewang.com/climg/624ecf4709eb58f306620516.jpg

xml代码

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

        https://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="clothes1" class="cn.bhu.factory.ClothesFactory" factory-method="create1">

</bean>     


<bean id="clothes2" class="cn.bhu.beans.Clothes">

  <property name="style" value="小西装"></property>

  <property name="color" value="蓝色"></property>

</bean>

<bean id="person1" class="cn.bhu.beans.Person">

  <property name="name" value="女孩"></property>

  <property name="clothes" ref="clothes1"></property>

</bean>

<bean id="person2" class="cn.bhu.beans.Person">

  <property name="name" value="男孩"></property>

  <property name="clothes" ref="clothes2"></property>

</bean>



</beans>

问题描述:

clothes cannot be resolved to a variable这是报错内容

不知道为啥不好使

写回答

3回答

光头是强者的象征

2022-04-13

你没有名字为clothes对象,你的对象名字是one,return one就可以了

0

好帮手慕小脸

2022-04-08

同学你好,"明明是只幼刀"同学的回答是正确的,同学这里可将return clothes;更改为return one;

祝学习愉快~

0

明明是只幼刀

2022-04-08

clothes cannot be resolved to a variable提示用户"clothes"并不能解析为一个变量,是因为你在实例化Clothes类时将类名定义为了one,所以会提示错误.

0

0 学习 · 9886 问题

查看课程