spring-简单配值以及测试

第一步:创建实体类

public class Hello {
    private String str;

第二步;创建application.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.xsd">

    <!--bean就是java对象 , 由Spring创建和管理-->
<!--    bean标签属性
        id:标签名,被用来获得标签的值
        class:实体类全限定类名;被用来

        
-->
    <bean id="hello" class="com.kuang.pojo.Hello">
<!--        
        property属性
        name:实体类属性;
        value:具体的值
        ref:映入spring容器中已经创建好的对象
-->
        <property name="str" value="Spring"/>
    </bean>

</beans>

 

第三步:测试

//读取spring配置文件,也叫上下文对象
        ApplicationContext context = new
                ClassPathXmlApplicationContext("beans.xml");
        //取出spring容器中的对象
        Hello hello = (Hello) context.getBean("hello");
        System.out.println(hello);

 ...

posted @ 2021-11-15 11:25  江南0o0  阅读(30)  评论(0编辑  收藏  举报