Spring基础总结

DI基本操作
setter注入
基本注解
通知器的xml配置
通知器注解的配置

			**************DI的基本操作**************

启动Spring容器,加载配置文件
ClassPathXmlApplicationContext cp=new ClassPathXmlApplicationContext(“xml文件路径”);
从spring容器中获得对象
getBean(“bean的name”);

			**************setter注入**************

给对象起别名
1.在bean中加入name标签,可写多个
2.在bean中的id中,写,但只能写一个
3.在外面配置标签<alias name="" alias="">

给构建的对象赋值
1.在<property>标签中配置name、value属性赋值(限基本数据类型/String)
2.在<property>标签中配置name、ref,ref指向其他bean标签(可通过bean标签中的构造器传参)
3.不配置values,在标签里面写
<ref bean="" /> 或
<bean class="">或
<ref local="" />有的版本支持

集合装配进对象内
在<property name=“去掉set名”>配置
1.array---------<array><value> 或<list><value>
2.list----------<array><value> 或 <list><value>
3.set-----------<set><value>
4.map-----------<map><entry key="" value="">
5.properties----<prop key="">

构造器装配
不写value时可用ref或在里面再嵌套<bean>
1.基于角标:<constructor-arg index="" value=""></constructor-arg>
2.基于类型:<constructor-arg type="" value=""></constructor-arg>
3.基于构造器变量名:<constructor-arg name="" value=""></constructor-arg>
4.上述三种结合使用

自动装配
autowire=""
1.byType:相同类型仅出现一次,set方式的参数类型
2.byName:全局变量的set方法去掉set 首字母小写去bean找
3.default:默认,跟一级标签beans中deafult-autowire属性结合使用
4.no:不匹配,一级标签beans不起作用
5.construct:基于构造器,先类型再参数名字

继承装配
在父类模板中bean中加abstract="true"通知spring不构建对象,子标签中parent=“父标签name”

自定义属性编辑器
eg. 字符串->自定义类
1.实现PropertiesEditor接口重写setAsText方法
2.通知spring,在xml中加入特殊bean,spring自动搜索装配(editor.xml)
org.springframework.beans.factory.config.CustomEditorConfigurer

创建bean实例的方式
1.通过构造器(有参/无参):bean方式
2.通过静态工厂:spring代建工厂实例,bean标签中加入factory-methond=“静态方法全名”
spring见该属性自动类名.方法
3.通过实例工厂:<bean name="" factory-bean=“工厂bean中的name” factory-method=“工厂中方法”>
4.bean实现spring提供FactoryBean接口,是则调用getObject方法返回对象
(spring提供了俩读取properties文件的类,加入后可直接${key}取 springFac.xml)
org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer
org.springframework.beans.factory.config.PropertyPlaceholderConfigurer

Spring的生命周期
bean中写入class所对应类中的初始化init和销毁destory方法即init-method="“和destory-method=”"

			   **************基本注解**************

注解构建
1.正常情况:<property name="" ref="">
2.自动装配:<bean autowire="">
3.给类的自定义属性的set方法上加 @Autowire注解,再告诉spring<bean class=“特殊类全限定名”>去扫描
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
4.先加context约束
<context:annotation-config></context:annotation-config>相当于写了上述共4类bean标签,扫描当前xml中class中的类
他会检查 @Autowired @Resource @PostConstruct @PreDestory @Required等注解

注解-语义(查找顺序)-可标记位置-参数
@AutoWire-自动装配(先基于类型再基于参数名)-set方法/全局变量/构造器-(required=true/false)
@Qualifier-指定注入bean的名字-set方法上或参数前,参数名/构造器参数类型前/全局变量前或上-(name="" type="")
	生效顺序:set->全局变量->构造器
@Resource-资源(默认先基于名字set方法去set,找不到再类型)-set方法上/全局变量上-(name="" type="镜像")  以上是spring提供注解,该注解由jdk提供

此时的<context:annotation-config>标签无用,代替的是<context:component-scan base-package="com.briup.bean"> 
@Component-组件(自动构建对象,无需配bean)-类上面-value="bean名字"
	区分web项目层次用的
	@Controller   web
	@Service      service
	@Respository  dao层
@Value("赋值")-基本数据类型,String赋值-set方法/全局变量
@Scope-构建单例-类上面-"singleton/prototype"
@PostConstruct-初始化方法上-构建对象调用的方法
@PreDestory-销毁的方法-从spring移除时调用的(单例可销毁)

静态代理:
代理跟目标方法实现相同接口,在代理方法中加入操作 再 调用目标方法,
jdk代理:
前提是目标类必须实现接口
Proxy.newProxyInstance(类加载器,接口镜像,处理器)
方法一:不构建工厂:处理器:实现InvocationHandler接口,核心方法invoke(代理对象,方法镜像,方法参数)
方法二:spring帮我们管理jdk工厂:实现FactoryBean接口,核心方法getObject()
cglib代理:
解决了没有接口的目标对象
工厂实现了MethodInterceptor接口
方法一:CglibProxyTest cp=new CglibproxyTest(); cp.getProxy(目标对象的镜像);
方法二:CglibProxyTest cp=new CglibProxyTest(); cp.setTarget(目标对象);
方法三:将方法二交由spring管理,spring管理工厂无法传参,故用方法二
<bean name="" factory-bean=“工厂对象” factory-method=“工厂中调用的方法”></bean>

			  **************通知器的xml配置**************

编写xml的通知

前置通知:
接口MethodBeforeAdvice
<bean class=“org.springframework.aop.framework.ProxyFactoryBean”>
<property name=“target” ref=“目标对象”>
<property name=“interfaces”><array><value>目标对象实现的所有接口</value></array> 无接口该步可忽略,cglib代构建
<property name=“interceptorNames”><array><value>通知器/增强器的目标bean</value></array>

后置通知:
接口AfterReturningAdvice
配法跟以前一样

环绕通知:
接口MethodInterceptor,调用参数的proceed()方法
配法跟以前一样

异常通知:
接口ThrowsAdvice
配法跟以前一样

增强器(切面+通知):
增强器配置:<bean class=“org.springframework.aop.support.RegexpMethodPointcutAdvisor”>
<property name=“advice” ref="">
<property name=“patterns”><array><value>.save.</value>
工厂:<bean name=“proxy” class=“org.springframework.aop.framework.ProxyFactoryBean”>
<property name=“target” ref=“目标对象”>
<property name=“interfaces”><array><value>目标对象实现的所有接口</value></array> 无接口该步可忽略,cglib代构建
<property name=“interceptorNames”><array><value>通知器/增强器的目标bean</value></array>

自动增强器:
<bean class=“org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator”></bean>
会自动基于bean标签的class扫描添加增强器(advisor>=1)

多代理:
<bean class=“org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator”>
<property name=“beanNames”><array><value>目标对象bean的名字
<property name=“interceptorNames”><array><value>增强器/代理器bean名字
基于beanNames去添加拦截器

aop专用标签:
<aop:config proxy-target-class=“false/true”> jdk代理-false / cglib代理-true
切面配置:<aop:pointcut expression=“execution(* com.briup.IOC.pojo..save(…))” id=“mypointcut”/>
增强器配置:<aop:advisor advice-ref=“around” pointcut-ref=“mypointcut”/> 也可以用pointcut=“execution(* com.briup.IOC.pojo..save(…))”

自定义aop标签:
<aop:config>
指定切面:<aop:pointcut expression=“execution(* com.briup.IOC.pojo..save(…))” id=“mypointcut”/>
<aop:aspect ref=“自定义通知类bean名字”>
前置通知:<aop:before method=“自定义通知类的方法名” pointcut-ref=“切面”/>
后置通知:<aop:after method=“自定义通知类的方法名” pointcut-ref=“切面”/> 无论如何都通知
<aop:after-returning method=“自定义通知类的方法名” pointcut-ref=“切面”/> 正常返回后的通知
环绕通知:<aop:around method=“自定义通知类的方法名” pointcut-ref=“切面”/>
异常通知:<aop:after-throwing method=“自定义通知类的方法名” pointcut-ref=“切面” throwing=“方法参数异常的参数名”/>
自定义发放中:前后置通知参数JoinPoint; 环绕通知ProceedingJoinPoint; 异常通知:JoinPoint和Exception

			   **************通知器注解的配置**************

注解配置通知
类上: @Component 让spring构建对象无需配置bean标签
@Aspect 该类中的所有方法均作用于切面

自定义空方法: @Ponitcut("executu(切面)")  该方法解释为了让该注解生效
前置通知: @Before(value="切面所指向空方法的名字")
后置通知: @After(value="切面所指向空方法的名字")
后置返回通知: @AfterReturning(value="切面所指向空方法的名字")
环绕通知: @Around(value="切面所指向空方法的名字")
异常通知: @AfterThrowing(value="切面所指向空方法的名字",throwing="异常参数名")

大致总结,如有不足之处,望留言指正。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值