zoukankan      html  css  js  c++  java
  • Spring入门(一)

    (一)什么是Spring
    Spring框架是个轻量级的Java EE框架。所谓轻量级,是指不依赖于容器就能运行的一个开源框架。
    EE开发分为三层结构:WEB层(Spring MVC) 业务层(Bean管理IOC) 持久层(Spring 的JDBC模板 、ORM模板用于整合其他的持久层框架)

    (二)开始
    IOC:即控制反转,是对象的创建不通过 手动 new,而是把对象的创建权交给Spring来完成。
    DI: 即依赖注入,意思是 Spring主动创建被调用类的对象,然后把这个对象注入到我们自己的类中,使我们能使用它。

    Spring例子:hello的实现

    第一步首先在IDEA下创建一个java项目,然后创建hello模块,设置为maven项目。

    第二部 加入相关的maven依赖:

      <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>4.3.18.RELEASE</version>
            </dependency>
     </dependencies>

    第三步:创建userService接口及userServiceImpl类
    首先是userService接口

    public interface userService {
        public void SayHello();
    }

    然后是userServiceImpl类文件

    public class userServiceImpl implements userService {
        public void SayHello() {
            System.out.println("Hello World");
        }
    }

    第四步:在main/resources文件夹下创建beans.xml文件完成相关的配置

    <?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
                            http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <bean id="userService" class="com.txp.dao.userServiceImpl"/>
    
    </beans>

    第五步:创建测试类

    public class testSpring {
        @Test
        public void test1(){
            //1.获取spring配置文件
            ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
            //2.由配置文件返回对象
            userService us = (com.txp.dao.userService) applicationContext.getBean("userService");
            us.SayHello();
        }
    }

    输出:这里写图片描述

    (三)Spring相关介绍
    id:Bean起个名字是唯一的
    name:没有为唯一属性要求,作用和id类似

    Spring的 Bean的属性注入 的属性注入 :

    第一种构造方法的式 :

        <bean id="car" class="com.txp.domain.Car">
            <constructor-arg name="name" value="qirui"/>
            <constructor-arg name="price" value="100"/>
        </bean>

    第二种set方法的属性注入

           <bean id="car" class="com.txp.domain.Car">
                 <property name="name" value="qi"/>
                 <property name="price" value="455"/>
            </bean>

    特别要注意的是还有对象类型的注入 使用的ref 如:

        <bean id="user" class="com.txp.domain.User">
            <property name="name" value="zhangsan"/>
            <property name="age" value="12"/>
            <!--方式一  SpEL方式注入要多个  #{' '}-->
            <property name="car" ref="#{'car'}"/>
            <!--方式二-->
            <property name="car" ref="car"/>
        </bean>

    以及名称空间p的属性注入的方式

         <bean id="car1" class="com.txp.domain.Car" p:name="宝马" p:price="1200000"/>
         <bean id="user1" class="com.txp.domain.User" p:name="sicong" p:age="12" p:car-ref="car1"/>

    在一个文件中引入另一个配置文件

           <import resource="path"/>

    采用扫描包的方式注入:beans.xml修改—-也叫组件扫描需要在需要的类上添加注解

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans.xsd
                             http://www.springframework.org/schema/context
                             http://www.springframework.org/schema/context/spring-context-4.3.xsd
    ">
    
    
        <context:component-scan base-package="com.txp.dao"/>
    
    </beans>

    同样的在userServiceImpl文件作出修改

    @Component(value = "userService")
    public class userServiceImpl implements userService {
        public void SayHello() {
            System.out.println("Hello World");
        }
    }

    Spring还提供了@Component的三个衍生注解上

    @Controller :Webceng
    @Service :业务层
    @Repository :持久层

    希望在知识中书写人生的代码
  • 相关阅读:
    Jmeter Ant Task如果报告中有错误,在邮件内容里面直接显示出来 系列2
    自动化测试的点点滴滴经验积累
    Java中通过SimpleDateFormat格式化当前时间:/** 输出格式:20060101010101001**/
    Good Bye 2015 A
    Codeforces Round #337 (Div. 2)B
    Codeforces Round #337 (Div. 2) A水
    hdu 1698 线段树 区间更新 区间求和
    hdu 1166线段树 单点更新 区间求和
    HDU2841 (队列容斥)
    15ecjtu校赛1006 (dfs容斥)
  • 原文地址:https://www.cnblogs.com/tongxupeng/p/10259540.html
Copyright © 2011-2022 走看看