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上注册后,就会自动创建一个对象。

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

  • 相关阅读:
    网络知识
    DXP 技巧和龙芯3A装机
    312-金胜维 P系列2.5寸 480G SATA3 SSD固态硬盘
    NVIDIA Jetson TK1 开发板
    NVIDIA Jetson™ TX1
    XILINX FPGA 开发板 XC3S250E 核心板 学习板+12模块
    AES-OZ745 OZ745 Zynq-7000 开发板与套件
    北京太速科技——海外代购
    AR/VR增强现实 虚拟现实,嵌入式解决方案探讨
    270-VC709E 增强版 基于FMC接口的Xilinx Vertex-7 FPGA V7 XC7VX690T PCIeX8 接口卡
  • 原文地址:https://www.cnblogs.com/wmskywm/p/13617003.html
Copyright © 2011-2022 走看看