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

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

  • 相关阅读:
    parted分区
    gitlab的还原
    hadoop2.7.3+spark2.0.1+scala2.11.8集群部署
    Daily record-September
    三次握手和四次挥手【转】
    “梅西式”程序员 【转】
    C++解析十-数据封装
    客户端、服务器代码实例
    网络整理
    C++解析九-数据抽象
  • 原文地址:https://www.cnblogs.com/wmskywm/p/13617003.html
Copyright © 2011-2022 走看看