Spring

Spring

  • 2002年,首次推出了Spring框架的雏形:interface21!
  • Spring框架以interface21为基础,经过重新设计,并不断丰富其内涵,于2004年3月24日,发布了1.0正式版。
    在这里插入图片描述

官方文档:https://spring.io/projects/spring-framework#learn

下载地址:https://repo.spring.io/release/org/springframework/spring/

GitHub:https://github.com/spring-projects/spring-framework
在这里插入图片描述

Maven依赖:(webmvc可以引入其他相关依赖)

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.8.RELEASE</version>
</dependency>

总结一句话:Spring是轻量级的控制反转(IOC),面向切面编程(AOP)的框架。

1. 第一个Spring程序

环境搭建

  1. 导入依赖
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.2.8.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/junit/junit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13</version>
    </dependency>
</dependencies>
  1. 创建pojo实体类
public class HelloStr {
   
    private String str;

    public void show(){
   
        System.out.println(str);
    }
  //注意一定要有set方法,因为Spring需要set注入属性的值
    public void setStr(String str) {
   
        this.str = str;
    }

    @Override
    public String toString() {
   
        return "HelloStr{" +
                "str='" + str + '\'' +
                '}';
    }
}
  1. 配置xml(暂时起名beans.xml) 正式应为ApplicationContext.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">

    <bean id="myhello" class="com.xchao.pojo.HelloStr">
      <!--对象中的属性注入,需要有set方法支持
				value="具体的值,基本数据类型或String。。。"
				ref="Spring中注册的引用类型对象"
			-->
        <property name="str" value="hello的属性str值"></property>
    </bean>
</beans>

在这里插入图片描述

  1. 测试
  @Test
    public void Test01(){
   
        //ClassPathXmlApplicationContext("beans.xml")通过类路径下的xml文件获取
      	//获得Spring上下文对象获取Spring容器
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        HelloStr myhello1 = (HelloStr) context.getBean("myhello");
        myhello1.show();
    }
  • **获取Spring上下文拿到Spring容器方式:**看源码 ctrl+鼠标右键
    在这里插入图片描述

所谓IOC:一句话搞定,对象由Spring创建,管理,装配。

2. 属性注入方式 (构造器注入)

  1. 无参构造,属性通过set注入,必须有set方法
<bean id="myhello" class="com.xchao.pojo.HelloStr">
      <!--对象中的属性注入,需要有set方法支持
				value="具体的值,基本数据类型或String。。。"
				ref="Spring中注册的引用类型对象"
			-->
        <property name="str" value="hello的属性str值"></property>
    </bean>
  1. 构造器注入属性
public class HelloStr {
   
    private String str;

    public HelloStr(String str) {
   //有参构造
        this.str = str;
       System.out.println("创建对象");//在使用有参构造生成对象时输出
    }
  ...
}

bean.xml
<!--构造器创建,通过参数下标注入-->
  	<bean id="myhello1" class="com.xchao.pojo.HelloStr">
        <constructor-arg index="0" value="下标驻入属性"></constructor-arg>
    </bean>
<!--构造器创建,通过参数类型赋值-->
    <bean id="myhello2" class="com.xchao.pojo.HelloStr">
      								<!--注意:类型为基本类型可直接如int,引用类型需写全路径名-->
        <constructor-arg type="java.lang.String" value="属性类型注入"></constructor-arg>
    </bean>
<!--构造器创建,通过参数名注入-->
    <bean id="myhello3" class="com.xchao.pojo.HelloStr">
        <constructor-arg name="str" value="属性名注入"></constructor-arg>
    </bean> 

测试:

 @Test
    public void Test01(){
   
        //获得Spring上下文对象
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        HelloStr myhello1 = (HelloStr) context.getBean("myhello1");//只get得到myhello1
        myhello1.show();
    }

在这里插入图片描述

总结:在配置文件加载的时候,对象就已经初始化了,默认为单例模式。

通过Spring上下文来getBean可获得对象的单例。

3. ApplicationContext.xml中的标签配置

  1. 别名
<alias name="" alias=""></alias>
  1. bean
<bean id="myhello1" class="com.xchao.pojo.HelloStr" name="alias1" scope="prototype">
        id:唯一标识名
  			class:包名+类名
  			name:别名1,别名2...
  			scope="singleton"默认单例模式
  			scope="prototype"原型模式
</bean>
  1. import,合并配置文件时使用
  • beans1.xml
  • beans2.xml
  • beans3.xml
  • ApplicationContext.xml
<import resource="beans1.xml"</import>   
<import resource=&#
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值