Spring学习第一天

----------------------------------Spring–介绍--------------------------------------------------------
Spring是什么
Spring是一个开源框架。
Spring是一个IROOC和AOP容器框架。
轻量级:Spring 是非侵入型的
依赖注入:
面向切面编程:
容器:
框架:
一站式:
在这里插入图片描述
安装一个插件—eclipse
在这里插入图片描述
----------------------------------------------------Spring–HelloWord--------------------------------------------------------
Jar包
commons-logging-1.1.3.jar
Spring-beans-4.0.0RELEASE.jar
Spring-context-4.0.0RELEASE.jar
Spring-core-4.0.0RELEASE.jar
spring-expression-4.0.0RELEASE.jar
Spring的配置文件:一个典型的Spring项目需要创建一个或多个Bean配置文件,这些配置文件用于Spring IOC容器里配置Bean。Bean的配置文件可以放在classpath下,也可以放在其他目录下。

package org.example;

public class SpringHelloWorld {
    private String name;

    public void hello(){
        System.out.println("hello:"+name);
    }

    public SpringHelloWorld() {
        System.out.println("HelloWorld's Constructor....");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        System.out.println("setname"+name);
        this.name = name;
    }
}

public class App
{
    public static void main( String[] args )
    {
      /*  //第一步:创建helloWorld的一个对象
        SpringHelloWorld helloWorld=new SpringHelloWorld();
        //第二步:为name属性赋值
        helloWorld.setName("atguigu");
        //第三步:调用hello方法
        helloWorld.hello();*/
        /*用了spring以后,第一步和第二步可以交给spring完成。*/
        //1.创建Spring的IOC容器对象
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationConfig.xml");
        //2.从IOC容器中获取Bean实例
        //SpringHelloWorld helloWorld= (SpringHelloWorld) ctx.getBean("helloWorld");
        //3.调用hello方法
        //helloWorld.hello();
    }
}

-------------------------------Spring–IOC&DI概述------------------------------------

Spring的Bean配置

IOC & DI 概述

配置bean
配置形式:基于xml文件的方式;基于注解的方式
Bean的配置方式:通过全类名(反射),通过工厂方法(静态工厂方法&实例工厂方法),FactoryBean
IOC容器 BeanFactory &ApplicationContext 概述
依赖注入的方式:属性注入;构造器注入
注入属性值细节
自动转配
bean之间的关系:继承;依赖
bean的作用域:singleton;prototype;WEB 环境作用域
使用外部属性文件
spEL
IOC容器中的Bean的声明周期
Spring 4.x新特性;泛型依赖注入
IOC:(Inversion of Control):其思想时反转资源获取的方向。传统的资源查找方式要求组件向容器发起请求查找资源,作为回应,容器适时的返回资源,而应用了IOC之后,则是容器主动的将资源推送给它所管理的组件,组件所要做的仅是一种合适的方式来接受资源,这种形式被称为查找被动的形式。
DI(Dependency Injection):IOC的另一种表大形式:即组件以一些预先定义好的方式(setter)接受来自如容器资源注入。相对于IOC而言,这种表达更直接。

IOC前生–分离接口与实现

IOC前生–采用工厂设计模式

IOC前生–采用反转控制

---------------------------------Spring–配置bean---------------------------------------

  • 配置形式:基于xml文件的方式 - 配置形式:基于xml文件的方式
  • Bean的配置方式:通过全类名(反射)
  • IOC容器 BeanFactory &ApplicationContext 概述
  • 依赖注入的方式:属性注入;构造器注入
    在spring的IOC容器里面配置Bean
    在xml文件通过bean节点来配置bean
<!--通过全类名的方式来配置bean-->
<!--配置bean class:bean的全类名-->
    <bean id="helloWorld" class="org.example.SpringHelloWorld">
       <property name="name" value="spring"></property>
    </bean>

id:Bean的名称
–在IOC容器中必须是唯一的。
–若id没有制定,Spring自动授权限定性类名作为Bean的名字
–id可以指定多个名字,名字之间可以使用逗号,分号,或者空格分隔。
class:全类名。通过反射的方式在容器中创建bean,所以要求Bean中必须有无参的构造器。

在Spring IOC容器中读取Bean配置创建Bean之前,必须对它进行实例化,只有在容器实例化之后,才可以从IOC容器里获取Bean实例并使用。
Spring 提供了两种类型的的IOC容器实现。
—BeanFactory:IOC容器基本实现。
—ApplicationContext:提供了更多的高级特性,是BeanFactory的子接口。
—BeanFactory 是Spring 框架的基础设施,面向Spring本身;ApplicationContext面向使用Spring框架的开发者,几乎所有的应用场合都直接使用ApplicationContext非底层的BeanFactory
—无论使用何种方式,配置文件时相同。

ApplicationContext:
ApplicationContext的主要实现类:
–ClassPathXmlApplicationContext:从类路径下加载配置文件。
–FileSystemXmlApplicationContext:从文件系统中加载配置文件。


ConfigurableApplicationContext:扩展于ApplicationContext,新增两个主要方法:refresh()和 close(),让ApplicationContext具有启动,刷新,和关闭上下文的能力


ApplicationContext:在初始化上下文时就实例化所有单例的Bean。

WebApplicationContext是专门为WEB应用而准备的,它允许从相对于WEB根目录的路径中完成初始化工作。


在这里插入图片描述
ClassPathXmlApplicationContext 是 ApplicationContext 接口的实现类,该实现类从类路径下来加载配置文件。


属性注入:属性注入即通过setter方法注入Bean的属性值或依赖的对象。

属性注入使用元素,使用name属性指定Bean的属性名称,value属性或value子节点指定属性值。
属性注入是实际应用中最常用的注入方式。

<bean id="helloWorld" class="org.example.SpringHelloWorld">
   <property name="name" value="spring"></property>
</bean>

contructor-arg>中没有name属性。

  <!--通过构造方法来配置bean的属性-->
<!--使用构造器注入属性值可以指定参数的位置,和参数的类型,以区分重载的构造器-->
<bean id="car" class="org.example.Car">
    <constructor-arg value="AUDI" index="0"></constructor-arg>
    <constructor-arg value="Shanghai" index="1"></constructor-arg>
    <constructor-arg value="300000" index="2"></constructor-arg>
</bean>

<bean id="car2" class="org.example.Car">
    <constructor-arg value="my" type="java.lang.String"></constructor-arg>
    <constructor-arg value="beijing" type="java.lang.String"></constructor-arg>
    <constructor-arg value="240" type="int"></constructor-arg>
</bean>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值