zoukankan      html  css  js  c++  java
  • 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 来创建容器对象   ,由源码得知:

  • 相关阅读:
    struts2重点——ValueStack和OGNL
    struts2基础——请求与响应、获取web资源
    struts2基础——最简单的一个例子
    servlet、filter、listener、interceptor之间的区别和联系
    服务器端组件
    自定义JSTL标签和函数库
    常见前端效果实现
    HTTP Cookie/Session
    获取动态SQL查询语句返回值(sp_executesql)
    WPF数据绑定
  • 原文地址:https://www.cnblogs.com/szj-ang/p/7464197.html
Copyright © 2011-2022 走看看