Spring的基本使用

Spring的使用

导包

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.3.3</version>
</dependency>

实体类:

public class Hello {
    private String str;

    @Override
    public String toString() {
        return "Hello{" +
                "str='" + str + '\'' +
                '}';
    }

    public void setStr(String str) {
        this.str = str;
    }

    public String getStr() {
        return str;
    }

    public Hello(String str) {
        this.str = str;
    }

    public Hello() {
    }
}

配置文件:

<?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
                           https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="hello" class="cn.gbl.pojo.Hello">
        <property name="str" value="123"/>
    </bean>
</beans>

测试:

@Test
public void Hello(){
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    Hello hello = context.getBean("hello", Hello.class);
    System.out.println(hello);
}
posted @ 2021-01-31 14:07  xxgbl  阅读(60)  评论(0)    收藏  举报