Spring学习总结

1Spring Ioc
Spring的控制反转:把对象的创建,初始化,销毁等工作交给Spring容器来做,由Spring容器控制对象的周期。
配置applicationContext文件
<bean id="HelloWorld"  class="com.spring.bean.HelloWorld"></bean>






2启动spring容器
ApplicatioContext  context=new ClassPathXmlApplicationContext("applicationContext.xml")
//从Spring容器中吧helloWorld提取出来
HellWorld helloWorld=(HelloWorld) context.getBean("helloWorld")


3别名
在配置容器中添加
 <alias name="helloWorld" alias="hw">


4创建对象的三中方式
  1静态工厂类
在spring中声明
applicationContext中创建---factory-method工厂方法
<bean id="helloworld2" class="类地址" factory-method="getInstance">
写一个静态工厂方法类
public class helloWorldFactory

public static HelloWorld getIntence(){
return new Hellowld();
}
}
调用
ApplicatioContext  context=new ClassPathXmlApplicationContext("applicationContext.xml")
HellWorld helloWorld=(HelloWorld2) context.getBean("helloWorld2")


helloWorld.getHellworld


2使用默认的构造函数


3使用实例工造方法
<bean id="helloworld3" class="工厂类地址" >
<bean id="helloWorldFactory" factory-bean="helloWorld3"  factory-method="getInstance">




4创建对象的时机
1默认情况下载Spring容器启动的时候进行创建
2spring配置文件中<bean id="helloworld2" class="类地址" factory-method="getInstance" lazy-init=“”“”>
如果在spring的配置文件中一个bean的配置中有lazy-init="true" 那么bean在调用bean的时候创建对象




5对象的范围
1默认情况下是单例模式
2在bean中配置一个参数 scope="prototype" 为多实例模式,不敢lazy-init 是什么值 都在getbean中才要创建对象






6初始化init
1在java代码种声明init方法
2在spring的配置文件中进行配置 init-method="init"
3在创建完成该bean的对象时候,spring会自动调用init方法
4可以在该方法中进行初始化




7destory方法
当spring关闭的时候调用的该方法
配置同6
如果该bean中scope=“protrtype”不执行该方法




   
8给属性值赋值
1通过set方法
步骤1写一个bean该bean中配置属性值,并且提供属性的setter方法
步骤2在Spring的配置文件中利用<property name='pid' value="1"></property>给属性值赋值
如果是引用属性值(基本属性值和String行),则赋值value赋值


2通过构造器进行赋值
index 参数下标
type参数类型
value 给一般参数赋值
ref 给引用类型赋值
<constructor-arg index="0" type="" value="" ref=""> </constructor-arg>




9ioc和di的意义
在一个类中引用了一个属性,接口,在java代码端可以不用为该接口进行实现,这个事由spring完成,客户端只需要面向接口编程




10DI spring配置 自定义注解
有三个jdk自带注解@Target(ElementType.Method),@retention(RetentionPolicy.runtime) @document
注解命名空间
在applicationContext中进行配置
<context:annotation-config></context:annotation-config>//启动依赖注入的注解解析器




11注解
步骤 启动Spring容器
1Spring容器为所有的bean创建对象
2 当Spring容器解析到
<context:annotation-config></context:annotation-config>
3启动依赖注入的注解解析器,会在srping管理的bean中寻找所有的属性看属性上是否有@resource
4如果该属性上有注解:
注解的name属性值为“”
让注解所在的属性的名称和spring容器中的id进行匹配,如果匹配成功,则赋值,如果匹配不成功,则按照类型进行匹配,如果匹配成功则赋值
如果匹配失败,则报错
注解name属性值部位null
让name中的属性值与bean中的id值进行匹配,如果不成功则报错
注解:应用类型可以 用注解其他类型不可以进行注解。
@autowored  按照类型匹配 qualifier spring提供的注解


12 abstract 意思为不给bean创建对象 parent 让子类中包含父类的属性


13IOC和DI
1IOC Spring容器 为一个bean创建对象,初始化,销毁
  DI:依赖注入给bean中的属性进行赋值


2DI 
1xml注入:
setter
construct
2注解
@resource


14 代理模式 
aop
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值