spring_test

<?xml version="1.0" encoding="utf-8"?> spring<sub>test</sub> <style type="text/css"> </style> <body>

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}">

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念