zoukankan      html  css  js  c++  java
  • spring之helloworld

    一句话简单概括spring:spring是一个轻量级控制反转(IOC)和面向切面(AOP)的编程框架。

    用spring框架搭建helloworld级项目:

    1、pom.xml加入依赖

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
         version>4.3.9.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.3.9.RELEASE</version>
    </dependency>

    2、绑定一个实体类并注入值:

    普遍方式:

    <?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">
    
        <!-- services -->
    
        <bean id="user" class="pojo.User">
            <property name="id" value="1001"/>
            <property name="uesrname" value="张三"/>
            <property name="pswd" value="abc123"/>
        </bean>
    
        <!-- more bean definitions for services go here -->
    
    </beans>

    解析:

    构造器索引方式:

    <bean id="user" class="pojo.User">
        <constructor-arg index="0" value="1001"/>
        <constructor-arg index="1" value="张三"/>
        <constructor-arg index="2" value="abc123"/>
        <constructor-arg index="3" value="花园街18号"/>
    </bean>

    3、获取实体类的值:

    public void test(){
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
            User user = (User)applicationContext.getBean("user");
            System.out.println(user.getUesrname());
    }

    *当实体类在beans.xml上注册后,就会自动创建一个对象。

    我们要使用该实体类的对象直接获取即可。

  • 相关阅读:
    最能激怒程序猿的十句话()
    程序员是如何被外行给逼疯的?
    Linux 平台安装Oracle Database 12c
    替代恐慌你有吗?程序员会被深度学习技术淘汰吗?
    1006 换个格式输出整数 (15 分)C语言
    1021 个位数统计 (15 分)C语言
    1010 一元多项式求导 (25 分)C语言
    1009 说反话 (20 分)C语言
    1008 数组元素循环右移问题 (20 分)C语言
    1056 组合数的和 (15 分)C语言
  • 原文地址:https://www.cnblogs.com/wmskywm/p/13617003.html
Copyright © 2011-2022 走看看