二. Bean Scope :As a rule, use the prototype scope for all stateful beans and the singleton scope for stateless beans
作为默认规则,用原型范围来表示有状态的beans,用单例范围来表示无状态的beans
三.当单例的beanA要装配非单例的beanB:容器不能够随时提供最新的beanB给beanA.这时,可以让beanA通过实现接口ApplicationContextAware 来让beanA知道容器的变化
四.bean 的init-method 和destroy-method 对应注解@PostConstruct和@PreDestroy
五.应用上下文ApplicationContext ac = new ClassPathXmlApplicationContext();
ApplictionContext为顶级接口,ClassPathXmlApplicationContext为其实现类,向上转型的意义就在于:利于扩展,
假如说有一个方法 public void do(ApplicationContext ac){...}和public void do(ClassPathXmlApplicationContect c){...},如果是前面一种方式,他可以根据实现类的不同而做出不同的动作;而用后一种,就只能对应于其实现类的动作。
六.fine-grained control and wider applicability 细粒度的控制和更宽广的应用性
七.@Required:setter方法上的注解,防止Null指针异常
八.@Bean,Indicates that a method produces a bean to be managed by the Spring container. 一旦某个配置类的某个方法被@Bean注解了,该方法返回一个bean对象;
可以用ApplicationContext ctx = new AnnotationConfigApplicationContext(HelloWorldConfig.class) 来new出ApplicationContext;
九.@Bean(initMethod = "init",destroyMethod = "destroy")通过在Bean对象的对应类中定义的init()方法和destroy方法来初始化或销毁bean对象,达到对bean对象的生命周期的管理。
十.@Bean加@Scope("prototype")来重定义bean的范围
十一.AOP
注:强制使用CGLIB代理
前面说过Spring使用动态代理或是CGLIB生成代理是有规则的,高版本的Spring会自动选择是使用动态代理还是CGLIB生成代理内容,当然我们也可以强制使用CGLIB生成代理,那就是<aop:config>里面有一个"proxy-target-class"属性,这个属性值如果被设置为true,那么基于类的代理将起作用,如果"proxy-target-class"省略(或者写为false)则默认基于接口的代理。
a)AOP核心概念
1、横切关注点
对哪些方法进行拦截,拦截后怎么处理,这些关注点称之为横切关注点
2、切面(aspect)
类是对物体特征的抽象,切面就是对横切关注点的抽象
3、连接点(joinpoint)
被拦截到的点,因为Spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法,实际上连接点还可以是字段或者构造器
4、切入点(pointcut)
对连接点进行拦截的定义
5、通知(advice)
所谓通知指的就是指拦截到连接点之后要执行的代码,通知分为前置(before)、后置(after)、异常(after-throwing)、最终(after-returning)、环绕通知(around)五类
6、目标对象
代理的目标对象
7、织入(weave)
将切面应用到目标对象并导致代理对象创建的过程
8、引入(introduction)
在不修改代码的前提下,引入可以在运行期为类动态地添加一些方法或字段
b)基础xml配置的AOP:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<aop:config><!--申明aop -->
<aop:aspect id="log" ref="logging"> <!--申明切面 -->
<aop:pointcut id="selectAll" <!--定义的切点名字 -->
expression="execution(* com.tutorialspoint.*.*(..))"/>
<!--定义的连接点(join point)-->
<aop:before pointcut-ref="selectAll" method="beforeAdvice"/>
<aop:after pointcut-ref="selectAll" method="afterAdvice"/>
<aop:after-returning pointcut-ref="selectAll"
returning="retVal"
method="afterReturningAdvice"/>
<aop:after-throwing pointcut-ref="selectAll"
throwing="ex"
method="AfterThrowingAdvice"/>
</aop:aspect>
</aop:config>
<!-- Definition for student bean -->
<bean id="student" class="com.tutorialspoint.Student">
<property name="name" value="Zara" />
<property name="age" value="11"/>
</bean>
<!-- Definition for logging aspect -->
<bean id="logging" class="com.tutorialspoint.Logging"/>
</beans>
c)基于@AspectJ
但在beans.xml中仍然要加一句 <aop:aspectj-autoproxy/>
参考http://wiki.jikexueyuan.com/project/spring/aop-with-spring-framenwork/aspectj-based-aop-with-spring.html