首页 > 编程语言 > 详细

Spring的xml文件编写示例

时间:2021-04-26 10:40:34      阅读:20      评论:0      收藏:0      [点我收藏+]
<?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

Spring的xml文件编写示例

原文:https://www.cnblogs.com/septembercold/p/14702811.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!