【spring扫盲】hello spring

package com.course.coke.pojo;

// 实体类Hello
public class Hello { private String str; public String getStr() { return str; } public void setStr(String str) { this.str = str; } @Override public String toString() { return "Hello{" + "str='" + str + '\'' + '}'; } }

 

<?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="hello" class="com.course.coke.pojo.Hello">
        <property name="str" value="Hello String" />
    </bean>


</beans>

 

import com.course.coke.pojo.Hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest1 {
    public static void main(String[] args) {

        // 获取spring的上下文对象
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        // 操作spring中的bean即可
        Hello hello = (Hello) context.getBean("hello");
        System.out.println(hello.toString());

    }
}

 

// 执行结果
Hello{str='Hello String'}

 

posted @ 2021-01-18 15:08  愚人李愚  阅读(134)  评论(0编辑  收藏  举报