spring学习笔记(一)

来源:互联网 发布:mac dmg镜像下载 编辑:程序博客网 时间:2024/05/17 08:32

Spring 开源框架,
轻量级:(非侵入式)不需要实现接口,不需要继承父类;
HELLOWORLD
1.安装插件(springsource-tool-suite-3.4.0RELEASE-e4.3.1-updatesite.zip)
help–install new soft–add–zip地址
( 带IDE 的项,不勾选联网更新)preference中出现spring(bean,aop等),插件安装成功
2.建lib文件夹,导入jar包:commons-logging-1.1.3;
beans ;conrext;core;expression
3.src下新建applicationcontext.xml(spring bean configuration File)
配置bean

<bean id="用来标识类 helloworld " class="全类名"><property name="set后面的属性名   如name2" value="值">//setname2(){};name="name2";value="spring",标识name设置为spring

4.Main中:

ApplicationContext ctx=new ClasspathXmlApplicationContext("配置文件名 applicationcontext.xml")//创建IOC容器对象HelloWorld helloword=(HelloWorld)ctx.getBean("bean的id  helloworld")//从IOC 容器获取beanhelloworld.hello()//调用HELLO方法

IOC:反转控制(像按需分配),容器主动将资源推送给它所管理的组件,组件做的仅是选择一种合适的方式来接收资源
DI:依赖注入,IOC的另一种说法
配置bean
applicationcontext 代表IOC容器,实际上是一个接口
Spring支持3中依赖注入的方式:属性注入;构造器注入;工厂方法注入
属性注入:setter方法注入
构造器注入:

public class car{ 属性:brand;corp;price;maxSpeed 构造方法:1.只写brand;corp;price         2.只写brand;corp;maxPreed toString方法}//构造器注入属性值可以指定参数的位置和参数的类型以区分重载的构造器<bean id="car" calss="--beans.car">//构造方法配置bean    <constructor-arg value="Audi" index="0">    <constructor-arg value="Shanghai" type="java.lang.String">          <!--  <value><![CDATA[<shangai^>]]></value> -->    <constructor-arg value="30000" type="double"></bean>如果字面值包含特殊字符,用<![CDATA[]]>包裹起来

bean之间的引用用ref;也可以使用内部bean

public class person{属性:name;age;   private Car car; set get方法: toString:}配置person//使用property的ref 属性建立bean之间的引用关系<bean id="person " class="--beans.Person"><property name="name" value="TOM"><property name="age" value="24"><property name="car" ref="car"></bean>main中:{        Person person=(Person)ctx.getBean("person");        system.out.println(person);}

级联属性配置:

<constructor-arg ref="car"></constructor-args><property name="car.maxSpeed" value="250"></property>

集合属性:一个人有许多车
person类中:private List car及对应的set get 方法
car类:
配置:

<bean id="person3 " class="--beans.Person"><property name="name" value="Mike"><property name="age" value="24"><property name="cars" >    <list>        <ref bean="car">        <ref bean="car2">    </list>    </property></bean>main中:{    ctx;    ctx.getbean;    Sysout(person)    }

配置MAP 属性
使用map节点及map的entry子节点配置Map类型的成员变量

<bean id="newperson " class="--beans.NewPerson"><property name="name" value="Rose"><property name="age" value="24"><property name="cars" >    <map>        <entry key="AA" value-ref="car"></entry>        <entry key="BB" value-ref="car2"></entry>    </map>    </property></bean>

使用props和prop子节点来为Properties属性赋值
配置单例的集合bean,以供多个bean进行引用

<util:list id="cars">    <ref bean="car">    <ref bean="car2"></util:list><bean id="person4 " class="--beans.Person"><property name="name" value="Jack"><property name="age" value="24"><property name="cars" ref="cars"></bean>

使用p命名空间
相对于传统配置简洁

<bean id="person5 " class="--beans.Person"p:age="30" p:name="queen" p:cars-ref="cars"></bean>

自动装配:
byType
byName
person类:name;address;Car; set get方法
Address类:city;street;set get方法
car类:brand;price;set get方法
配置文件:

<bean id="address " class="--beans.Address"p:city="beijing" p:street="huilongguan" ></bean><bean id="car " class="--beans.car"p:brand="audi" p:price="30000" ></bean><bean id="person " class="--beans.person"p:name="tom" autowire="byName"></bean>

main:打印person
bean配置的继承:使用bean的parent属性指定继承哪个bean的属性
bean配置的依赖:
配置person时,必须有一个关联的car;就是说person这个bean依赖car这个bean

<bean id="car " class="--beans.car"p:brand="audi" p:price="30000" ></bean><bean id="person " class="--beans.person"p:name="tom" p:address-ref="address2" depends-on="car"></bean>

scope属性配置bean的作用域
bean的作用域:默认 scope=“singleton” ,容器初始时创建bean实例,在整个容器的生命周期内,只创建这一个bean scope=“prototype”:原型的,容器初始化时不创建bean实例,而是在每次请求时都创建一个新的bean
request:
session:
使用外部属性文件:
1.beans-properties.xml:
2.导入c3p0,mysql驱动
3.配置:
普通配置为:

<bean id="dataSource " class="--ComboPooledDataSource" ><property name="user" value="root"></property><property name="password" value="123"></property><property name="driverClass" value="com.mysql.jdbc.Driver"></property><property name="jdbcUrl" value="jdbc:mysql:///test"></property></bean>

配置基本属性,不用再bean中配置,新建一个配置属性文件:db.properties

user=rootpassword=123driverclass=com.mysql.jdbc.Dreiverjdbcurl=jdbc:mysql:///test
//导入属性文件<context:property-placeholder location="classpath:db.properties"><bean id="dataSource " class="--ComboPooledDataSource" >//使用外部化属性文件的属性<property name="user" value="${user}"></property><property name="password" value="${password}"></property><property name="driverClass" value="${driverclass}"></property><property name="jdbcUrl" value="${jdbcurl}"></property></bean>

SpEL

{…}

引用Bean,属性和方法:
1.value=”#{bejing}”
2.使用spel引用类的静态属性 value=“#{T(java.lang.Math).PI*80}”
3.使用spel应用其他的bean value=”#{car}”
4.使用spel应用其他的bean的属性 value=“#{address.city}”
5.在spel中使用运算符 value=”#{car.price>300000?’金领’:‘白领’}”

0 0