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

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

  • 相关阅读:
    Docker 介绍
    Docker 教程
    Python 删除文件与文件夹
    Python 读文件
    Python 打开文件(File Open)
    Python 异常处理(Try...Except)
    什么是拜占庭将军问题
    智能合约语言 Solidity 教程系列7
    给代币添加高级功能-代币管理、增发、兑换、冻结等
    一步步教你创建自己的数字货币(代币)进行ICO
  • 原文地址:https://www.cnblogs.com/wmskywm/p/13617003.html
Copyright © 2011-2022 走看看