Spring入门笔记(一)---入门配置

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/imsupermanvip/article/details/78483647

一、导入Jar包

二、配置xml

在src目录下创建并编写配置文件applicationContext.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">
</beans>

三、编写POJO类

package com.my.pojo;

public class Category {

    private Integer id;

    private String name;

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "Category [id=" + id + ", name=" + name + "]";
    }

}

四、在spring配置文件中配置beans

<bean name="c" class="com.my.pojo.Category">
        <property name="name"  value="category 1" />
        <property name="id"  value="1001"></property>
    </bean>

五、测试

package com.my.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.my.pojo.Category;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] {"applicationContext.xml"});
        Category c = context.getBean("c", Category.class);
        System.out.println(c);
    }

}

猜你喜欢

转载自blog.csdn.net/imsupermanvip/article/details/78483647