<?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就是java对象 , 由Spring创建和管理-->
    <!--class指向类的路径-->
    <bean id="abcdefg" class="com.rabbit.Hello">
        <property name="name1314159" value="Spring1314567"/>
    </bean>
</beans>

使用演示:
实体类Hello代码:

public class Hello {
    private String name1314159;

    public String getName1314159() {
        return name1314159;
    }
    
    public void setName1314159(String name1314159) {
        this.name1314159 = name1314159;
    }
    
    public void show(){
        System.out.println("Hello"+name1314159);
    }

}

测试:

@Test
    public void HelloSpring(){
        //根据路径读取xml文件
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //根据id创建对象,取值
        Hello hello = (Hello) context.getBean("abcdefg");
        hello.show();
    }

结果:
HelloSpring1314567

posted on 2021-04-26 00:38  时寒很苦恼  阅读(177)  评论(0)    收藏  举报