如何从xml获取不同类的所有bean

问题描述 投票:-1回答:1

所以我想从同一个xml文件中初始化两个类1st和2nd,并希望从主类访问它们。但是,当我尝试执行此操作时,出现错误“ Bean类[com.springframework.Cars]的无效属性'carname':Bean属性'carname'不可写或具有无效的setter方法。setter的参数类型吗?匹配吸气剂的返回类型?“

这是我的Beans.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
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <bean id = "helloWorld" class = "com.springframework.HelloSpring">
      <property name = "message" value = "Hello World!"/>
   </bean>
      <bean id = "helloWorld1" class = "com.springframework.HelloSpring">
      <property name = "message" value = "Hello!"/>
   </bean>
<bean id = "car1" class = "com.springframework.Cars">
      <property name = "carname" value = "chevy"/>
   </bean>
</beans>

'''这是我的一等班'''

package com.springframework;

public class HelloSpring {
    private String message;

       public void setMessage(String message){
          this.message  = message;
       }
       public void getMessage(){
          System.out.println("Your Message : " + message);
       }
}

'''

这是我的第二堂课'''

package com.springframework;

public class Cars {
    private String carname;
    public void setCar(String carname) {
        this.carname = carname;
    }
    public void getCar() {
        System.out.println("carname"+carname);
    }
}

'''这是我的主班'''

package com.springframework;
import java.util.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
        String[] s = context.getBeanDefinitionNames();
        for( int i=0;i<s.length;i++) {
            System.out.println(s[i]);
        }
          context = null;
    }

}

'''

java spring javabeans
1个回答
-1
投票

另外,请您放入文件夹结构以更清楚地了解是否正确加载了文件。

© www.soinside.com 2019 - 2024. All rights reserved.