• Spring——Spring第一个小案例


    一、创建 Web Project 项目,为了方便自动加载 jar 包

    二、导入 Spring 依赖的 Jar 包

    三、创建 实体类 Student

    public class Student {
    
        private String name;
        private int age;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        @Override
        public String toString() {
            return "Student [name=" + name + ", age=" + age + "]";
        }
    }

    四、在 src 目录下创建一个名称为 ApplicationContext.xml 的文件

    <?xml version="1.0" encoding="UTF-8"?>
    <!--  
        beans : 根节点
        xmlns : 命名空间
       -->
    <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 id="stu" class="cn.bdqn.entity.Student">
                <!-- 找的是实体类 Student 里的 Set 方法 -->
                <property name="name" value="小明" />
                <property name="age" value="18" />
            </bean>
    </beans>

     五、在 test 类 测试

    public static void main(String[] args) {
            
            //实例化容器
            ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
            //更具ApplicationContext.xml 文件 的bean id 找到对应的类
            Student stu = (Student)ctx.getBean("stu");
            System.out.println(stu);
        }

    为什么使用 new ClassPathXmlAppliatonContext 来创建容器对象   ,由源码得知:

  • 相关阅读:
    内存映射
    docstring show under decorator
    eventlet dbpool for postgresql &mysql
    za python
    Install MySQL 5.0 Max on FC3
    vi
    ff chrome tips
    20101004网站部署更新
    sqlalchemy type &elixir type
    20100930网站部署更新日志
  • 原文地址:https://www.cnblogs.com/szj-ang/p/7464197.html
Copyright © 2020-2023  润新知