Spring框架详细了解和我的第一个Spring项目

前面用MyBatis代替经典的dao层来连接数据库,后面学习稳定框架Spring
来完善MVC的架构

Spring的官方解释

Spring框架是一个Java平台,为开发Java应用程序提供全面的基础架构支持。Spring处理基础结构,因此您可以专注于应用程序。
Spring使您能够从“普通的Java对象”(POJO)构建应用程序,并将企业服务非侵入性地应用于POJO。此功能适用于Java SE编程模型以及全部和部分Java EE。

◆目的:解决企业应用开发的复杂性
◆功能:使用基本的JavaBean代替EJB(开发和部署多层结构的、分布式的、面向对象的Java应用系统的跨平
台的构件体系结构),并提供了更多的企业应用功能
◆范围:任何Java应用
Spring是一个轻量级控制反转(IOC)和面向切面(AOP)的容器框架。

IOC技术介绍:

控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度。最常见的方式是依赖注入(Dependency Injection,简称DI)
Class A中用到了Class B的对象b,一般情况下,需要在A的代码中显式的new一个B的对象。
采用依赖注入技术之后,A的代码只需要定义一个私有的B对象,不需要直接new来获得这个对象,而是通过相关的容器控制程序来将B对象在外部new出来并注入到A类里的引用中

模块

Spring框架包含组织为约20个模块的功能。这些模块分为核心容器,数据访问/集成,Web,AOP(面向方面的编程),检测,消息传递和测试,如下图所示:
在这里插入图片描述

需要的jar包的介绍:

spring-core-4.1.6.RELEASE.jar

核心模块提供了框架的基本组成部分,包含IOC和依赖注入功能。

spring-beans-4.1.6.RELEASE.jar

Bean模块提供BeanFactory,它是一个工厂模型的复杂实现

spring-context-4.1.6.RELEASE.jar

上下文模块建立在有核心和Bean模块提供的坚实的基础上,他是访问定义和配置的任何对象的媒介,AppliactionContext接口是上下文接口的重点

spring-expression-4.1.6.RELEASE.jar

模块提供了一种功能强大的表达式语言,用于在运行时查询和操作对象图。它是对JSP 2.1规范中指定的统一表达语言(统一EL)的扩展。该语言支持设置和获取属性值,属性分配,方法调用,访问数组,集合和索引器,逻辑和算术运算符,命名变量以及按名称从Spring的IoC容器中检索对象的内容。它还支持列表投影和选择以及常见的列表聚合。

构建第一个Spring项目:

__先构建一个web项目–>在web.WEB-INF下构建lib库导入需要的jar包
我把整理好的需要的jar包放在下面:
https://download.csdn.net/download/qq_43171656/12650800
在src目录下新建xml文件命名为官方推荐名字:applicationContext-service.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 definitions here -->
</beans>

Spring的运行原理:

在这里插入图片描述

Bean的作用域(单态模式singleton\原型模式prototype)

scope=“prototype”
如果要创建两个不同的bean对象
方式1:

<?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">
    <beans>
        <!--下面等同于UserServiceImpl userServiceImpl = new UserServiceImpl(),把对象实例放入了Spring容器中-->
        <!--scope不写的话默认情况下是单态模式scope="singleton",声明scope="prototype",当前变成了原型模式-->
        <bean id="userServiceImpl" class="com.spring.service.impl.UserServiceImpl"></bean>
        <!--下面等同于UserServiceImpl userServiceImpl2 = new UserServiceImpl(),把对象实例放入了Spring容器中-->
        <bean id="userServiceImpl2" class="com.spring.service.impl.UserServiceImpl"></bean>

    </beans>
</beans>

方式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">
    <beans>
        <!--下面等同于UserServiceImpl userServiceImpl = new UserServiceImpl(),把对象实例放入了Spring容器中-->
        <!--scope不写的话默认情况下是单态模式scope="singleton",声明scope="prototype",当前变成了原型模式-->
        <bean id="userServiceImpl" class="com.spring.service.impl.UserServiceImpl" scope="prototype"></bean>
         </beans>
</beans>

数据的的获取

package com.spring;

import com.spring.pojo.User;
import com.spring.service.impl.UserServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {
    @Test
    public void run2(){

        //加载Spring的核心配置文件
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext-service.xml");
        userServiceImpl usi =(UserServiceImpl)ac.getBean("userServiceImpl");
        System.out.println(usi);
    }
}

属性注入

基于xml的注入

所谓注入,可理解为对象的属性赋值
简单数据类型和引用数据类型注入

package com.spring;

import com.spring.pojo.User;
import com.spring.service.impl.UserServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {
    @Test
    public void run2(){

        //加载Spring的核心配置文件
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext-service.xml");
        User user =(User)ac.getBean("user");
        System.out.println(user);
    }
}

applictationContext-service.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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <beans>
        <bean id="user" class="com.spring.pojo.User">
            <!--给属性注入值:name映射pojo类中的属性名,value:指定要注入的值-->
            <property name="sid" value="1"></property>
            <property name="uname" value="张翰"></property>
            <property name="pwd" value="123"></property>
            <property name="spower" value="1"></property>
        </bean>
    </beans>
</beans>

通过构造器注入:
applicationContext-service.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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <beans>
        <bean id="user" class="com.spring.pojo.User">
            <!--通过构造器注入-->
           <constructor-arg name="sid" value="11"></constructor-arg>
           <constructor-arg name="uname" value="张三"></constructor-arg>
           <constructor-arg name="pwd" value="321"></constructor-arg>
           <constructor-arg name="spower" value="1"></constructor-arg>

            <!--注入引用数据-->
            <constructor-arg name="account" ref="account"></constructor-arg>
        </bean>

        <bean id="account" class="com.spring.pojo.Account">
            <constructor-arg name="aid" value="1"></constructor-arg>
            <constructor-arg name="money" value="1200"></constructor-arg>
        </bean>

    </beans>
</beans>

值得注意的是:在User实例中必须含有对应的构造方法

域属性自动注入(byName\byType;局部和全局配置)
autowire=“byName”
default-autowire=“byType”

局部自动注入:

<?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"
        >
       <beans>
        <!--<bean id="user" class="com.spring.pojo.User" autowire="byType">  局部根据类型自动注入-->
           <bean id="user" class="com.spring.pojo.User">
            <!--通过构造器注入-->
           <constructor-arg name="sid" value="11"></constructor-arg>
           <constructor-arg name="uname" value="张三"></constructor-arg>
           <constructor-arg name="pwd" value="321"></constructor-arg>
           <constructor-arg name="spower" value="1"></constructor-arg>
        </bean>

        <bean id="account" class="com.spring.pojo.Account">
            <constructor-arg name="aid" value="112"></constructor-arg>
            <constructor-arg name="money" value="2500"></constructor-arg>
        </bean>

    </beans>
</beans>

全局自动注入:

<?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"
        default-autowire="byName"
>
    <!--全局自动注入,放在beans标签中-->
    <beans>
           <bean id="user" class="com.spring.pojo.User">
            <!--通过构造器注入-->
           <constructor-arg name="sid" value="11"></constructor-arg>
           <constructor-arg name="uname" value="张三"></constructor-arg>
           <constructor-arg name="pwd" value="321"></constructor-arg>
           <constructor-arg name="spower" value="1"></constructor-arg>
        </bean>

        <bean id="account" class="com.spring.pojo.Account">
            <constructor-arg name="aid" value="112"></constructor-arg>
            <constructor-arg name="money" value="2500"></constructor-arg>
        </bean>

    </beans>
</beans>

空字符串或null的注入
使用value/>相当于:" "

<value/>:" "
<null/>:null
	<property name="name" ><value/></property> 
	<property name="name" ><null/></property> 

空值注入

   <bean id="user" class="com.spring.pojo.User">
    <!--通过构造器注入-->
   <property name="sid" value="11"></property>
   <property name="uname"><value/></property>
   <property name="pwd" value=""></property>
   <property name="spower" value="1"></property>
</bean>

集合属性注入(array、set、list、map、properties)
applicationContext-service.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
        https://www.springframework.org/schema/beans/spring-beans.xsd"
>
    <beans>

        <bean id="data" class="com.spring.pojo.Data">
            <property name="arr">
                <array>
                     <value >长沙</value>
                     <value >株洲 </value>
                     <value >湘潭 </value>
                </array>
            </property>

            <property name="list">
                <list>
                    <value>11</value>
                    <value>12</value>
                    <value>13</value>
                </list>
            </property>

            <property name="map">
                <map>
                    <entry key="money" value="2000"></entry>
                    <entry key="age" value="19"></entry>
                </map>
            </property>

            <property name="set">
                <set>
                    <value>湖南</value>
                    <value>湖北</value>
                    <value>广东</value>
                    <value>广西</value>
                </set>
            </property>

            <property name="pro">
                <props>
                    <prop key="city">广州</prop>
                    <prop key="phone">1367898989898</prop>
                    <prop key="age">20</prop>
                    <prop key="url">www.baidu.com</prop>
                </props>
            </property>
        </bean>

    </beans>
</beans>

Date.java:

package com.spring.pojo;

import java.util.*;

public class Data {
    private String[]arr;
    private List<Integer> list;
    private Set<String> set;
    private Map<String,Integer> map;
    private Properties pro;

    public String[] getArr() {
        return arr;
    }

    public void setArr(String[] arr) {
        this.arr = arr;
    }

    public List<Integer> getList() {
        return list;
    }

    public void setList(List<Integer> list) {
        this.list = list;
    }

    public Set<String> getSet() {
        return set;
    }

    public void setSet(Set<String> set) {
        this.set = set;
    }

    public Map<String, Integer> getMap() {
        return map;
    }

    public void setMap(Map<String, Integer> map) {
        this.map = map;
    }

    public Properties getPro() {
        return pro;
    }

    public void setPro(Properties pro) {
        this.pro = pro;
    }

    @Override
    public String toString() {
        return "Data{" +
                "arr=" + Arrays.toString(arr) +
                ", list=" + list +
                ", set=" + set +
                ", map=" + map +
                ", pro=" + pro +
                '}';
    }
}

基于注解的DI注入

环境搭建:
导入aop包(spring-aop-4.1.6.RELEASE.jar), 添加context约束头信息(组件扫描器)
常用注解:
@Component
@Value:给属性的基本数据类型赋值,可以放在属性上,页可以放在属性的set方法上
@Scope:指定类是单态模式还是原型模式
给属性的应用数据类型注值
@Autowired : 设置对象类型的属性的值。但它是按照对象的类型来注入,跟注解的名字无关。
@Resource : 完成对象类型值的注入,按照名字注入

引入context schema的约束:

<?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.xsd"> <!-- bean definitions here -->
<!--    开始时,扫面packet下的所有类-->
    <context:component-scan base-package="com.spring.pojo"></context:component-scan>
</beans>
Spring的IOC(注解)

@Component:
表示当前修饰的类交给Spring容器管理
修饰一个类,将这个类交给Spring管理。

与@Component相同功能的还有三个衍生注解,都是用来修饰类:
@Repository:添加在Dao实现类上
@Service:添加在Service实现类上
@Controller:添加在Controller类上

Bean的其他的注解
生命周期相关的注解
@PostConstruct:初始化方法
@PreDestroy :销毁方法
Bean作用范围的注解
@Scope
singleton :默认是单例(常用)
prototype :多例(常用)

IOC的XML和注解开发比较

在这里插入图片描述
使用场景:
XML:可以使用任何场景
开发中,用来注入框架实例
基于xml操作的是.class文件

注解:有些地方用不了,这个类不是自己写的
开发中,用来注入自己写的java类
基于注解操作的是源代码

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值