spring_test

springtest

1 spring的简单使用

  1. 包导入
  2. 新建类,并给出get,set方法,无参构造函数
  3. 配置xml中的bean class: bean的全类名,反射的方式实现 id: 标识容器中的bean 框架式配置bean
  4. 创建spring中的ioc容器对象,并获取bean实例 ApplicationContext ctx = new ClassPathApplicationContext("xxx.xml"); //Classpathapplicationcontext是 Applicationcontext的一个实现类,表示从类路径加载 XXXXXXX xxxxx = (XXXXXXX)ctx.getBean("xxx.xml"); //getBean有几种构造方法,例如"xxx.class",不一定是说beanID
  5. 依赖注入
    1. 属性注入: 通过setter方法 , 标签是 <property name="get方法名" value="hahah"> 这个name并不是指变量名,而是变量的set方法的名字,虽然结果是一样,但是实现的的方式不同
    2. 构造器注入: 通过的是类的构造方法,标签是<constructor-arg value="" [type=] [index=]> ,有几个可以写几个,默认可以按顺序来,也可以混序,加index就行了 , 当构造器重载时,要指定参数的位置和参数,type和index混用
    3. 如果有特殊字符,就不能属性的方式赋值了,要用子标签 <value><![CDATA[<SHHS^]]>
    4. 引用类型在注入时用ref而不用value,这个是专指外部bean,当然也可以使用内部bean(也叫做私有匿名bean,不能给外部使用)
    5. 注入null必须要用标签 <null/>
    6. 为级联属性赋值 name="类名.属性名" , 先初始化,后级联
    7. 集合属性: 利用标签<list[set][map]> , <map>就要用 <entry key="" value-ref="">
    8. 配置properties <property name="propertes"> <props> <prop key="user">root<prop> <props> <property>
    9. 对多个bean进行引用, 要使用util命名空间 <util:list id="xx"> <ref bean="xx1"> <ref bean="xx2"> </util:list>
  1. p命名空间

2 自动装配

两个指令: autowire:byName , autowire:byType

3 bean配置的继承

一般是为了省事, 比如 <bean id="xx" class="aaa.bbb.ccc" p:city="beijin" p:street="wudaokou"> </bean> <bean id="xx" class="aaa.bbb.ccc" p:city="beijin" p:street="dazhongsi"> </bean> 前面的几个参数都是一样的,那么就最后一个不一,所以用一个继承指令 , parent , 然后在id上加"^"标识 <bean id="xx^" class="aaa.bbb.ccc" p:city="beijin" p:street="wudaokou"> </bean> <bean id="xx^" parent="adderss" p:street="dazhongsi"> </bean>

有抽象bean的用法 : abstract="true"

bean的依赖关系,就是bean的强行绑定,在定义一个bean时,必须要有其定义过的bean

4 bean的作用域

默认是单例,通过 scope属性来设置

5 使用外部属性文件(jar包是 c3p0 , mysql-con)

日常: <bean id="xxx" class="xxx.xxx.xxx.c3p0.cccccc"> <property name="user" value="root"></property> <property name="password" value="root"></property> <property name="driverClass" value="root"></property> <property name="jdbcUrl" value="root"></property> </bean>

一般都是为了方便 ,放在外面的文件中 例如:db.properties user=root password=123 driverclass=com.mysql jdbcurl=jdbc:…

引用context命名空间 导入 <context:property-placeholder location="classpath:db.properties"> <bean id="xxx" class="xxx.xxx.xxx.c3p0.cccccc"> <property name="user" value="\({user}">

6 SPEL #{}

例子:

  1. 引用其他对象 <property name="car" value="#{car}"></property> //以前要有ref
  2. 引用其他对象的属性 <property name="ca r2.speed" value="#{car.speed}"></property> //这个不用这个是做不到的

7 bean的生命周期

了解了一下,就在类中定义相应的函数 , init ,destory 之类的 然后在相应的bean中配置 init-method , destory-method , 顺序是: 构造函数 -> set方法 -> init -> 返回bean -> destory

更加细致的配置,要用到后置处理器 要实现的接口: BeanPostProcessor 在xml中配置好这个bean就行了

8 框架式配置bean #

  1. 工厂配置方法
    1. 静态: 直接调用某一个类的静态方法就可以返回bean的实例
    2. 实例:
  2. FactoryBean配置
    1. 实现类
    2. 配置bean
  3. 基于注解的方式
    1. 先要了解什么是组件扫描
    2. @Comlponent @Respository @Service @Controller
    3. <context: component-can>

Date: 2019-09-11 18:34

Created: 2019-09-11 周三 18:36

Validate

猜你喜欢

转载自www.cnblogs.com/instinct-em/p/11507989.html