• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

爱河

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

Spring基础

Spring

一、Spring的特点及概念

1. Spring:和一个开源框架

2. Spring为简化企业级开发而生,使用Spring就可以使简单的JavaBean实现以前只有EJB才能实现的功能。

3. Spring是一个IOC(DI)和AOP容器框架

4. Spring是一个容器,控制应用对象的生命周期

二、Spring架包

1.Spring-beans、Spring-context、Spring-core、

Spring-excression

三、案例HelloWord

                1.创建HelloWord类,并却添加hello()方法,必须要有无参构造器

                2.创建Main测试类

             3.配置HelloWord类的bean

                4.创建Spring的IOC容器对象

//创建Spring的IOC对象

ApplicationContext ctx=new ClassPathXmlApplication(“application.xml”);

//从IOC中获取Bean实例

HelloWord hello=(HelloWord)ctx.getBean(“helloword”, HelloWord.class);

//调取该类中的方法

hello. hello();

四、SpringIOC

概念:

SpringIOC(控制反转)对象不需要从容器中查询或者创建所需要的bean对象,容器会主动提供所需要的bean对象。

DI(依赖注入):依赖容器把资源注入给Spring bean对象。组件以及预先定义好的方式,接受来自容器的注入。

 

ApplicationContext主要实现类:

    ClassPathXmlApplicationContext:从类路径下加载xml文件配置

    FIleSystemXmlAplicationContext:从文件系统中加载配置文

ConfigurableApplicationContext

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

类关系图

 

 

 

通过ID获取bean对象

a)  <bean id=”aqw1” class=”hellow”></bean>

 

有多个bean对象

b)  用类型返回IOC容器中的bean,要求是容器中只能有一个该类型的bean

c)  从IOC容器中利用id获取容器中的bean (多个bean对象)

属性注入:通过set方法注入Bean的属性值或者依赖的对象

d)  属性注入使用<property>元素使用name属性指定Bean的属性名称,value属性或者<value>子节点指定的属性值  

e)  属性注入是实际应用中常用的注入方式

 

构造方法来配置Bean的属性

f)  按照参数顺序:

<constructor-arg value=”123” index=”1”></constructor-arg>

<constructor-arg value=”232” index=”2”></constructor-arg>

 

g)  区分参数类型(指定参数的位置和类型区分重载的构造器)

<constructor-arg value=”阿斯顿” type=””></constructor-arg>

<constructor-arg value=”大大啊” type=””></constructor-arg>

Bean之间的关系(配置上 “继承”)  

h)  <bean id=”aqw1” class=”hellow”></bean>

<bean id=”aqw2” parent=” aqw1”></bean>

i)  将bean作为模板:

j)   <bean id=”aqw1” class=”hellow” abstract=”true”></bean>

抽象bean不能被实例化

k)  若一个bean的class没有被指定,则该bean是一个abstract  bean

 

Bean之间的依赖关系

l)   < property name=”取得名字” ref=”依赖的bean”/>

Bean的作用域(的值有关)

m)  scope=“prototype”,不是单例的,每次请求时,重新创建bean

n)  scope=“singleton”,是单例的,要重新创建bean,容器的默认scope=“singleton”,整个容器的生命周期中之创建一个。

 

 

Spring Bean外部属性文件(datasource)
  • o)  Spring bean文件配置

                         i.      c3po配置(连接数据库)

<!—导入数文件-->

    <context:property-placeholder location=”classpath:db.properties”>

    <bean id=”dataSource” class=”com.mchange.v2.c3p0.ComboPooledDataSource”>

      <property name=”user” value=”${user}”></property>

      <property name=”password” value=”${password}”></property>

      <property name=”driverClass” value=”${ driverClass }”></property>

      <property name=”url” value=”${ jdbcurl }”></property>

    </bean>

 

                       ii.      数据库资源库配置(db.properties)文件

user=root

password=1230

driverClass=com.mysql.jdbc.Driver

jdbcurl=jdbc:mysql:///test

               iii.      Spring Bean导入文件db.properties

posted on 2018-08-11 00:03  爱河  阅读(122)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3