1.SpringBean 的生命周期
首先需找所有的bean根据bean定义的信息来实例化bean(默认bean是单例的)-------然后使用依赖注入,spring按照bean定义的信息来配置bean的所有属性--------若bean指定了init-method=“方法名”方法,他将被调用-------若指定了destroy-method=“方法名”定制的销毁方法,就调用来销毁。
2.bean实例化的方式
通过构造器(有参或无参)2.通过静态工厂方法(工厂不会被实例化)3.通过实例工厂方法非静态方法:利用实例化factory方法创建,即将factory方法也作为了业务bean来控制。 4.bean实现Spring提供的FactoryBean接口(提供了工厂方法和返回class是否是单例)
3.advistor
advisor是PointCut和Advice的综合体,完整描述了一个advice将会在pointcut所定义的位置被触发。也就是说,它包含了pointcut和advice两项内容,这两项内容则用于分别给出advice调用所发生的位置和发生的内容。其接口如下:
public interface PointcutAdvisor {
Pointcut getPointcut();
.
Advice getAdvice();
}
应用语法如下:
<!-- 配置advisor -->
<!-- 作用:筛选要拦截的方法 -->
<bean name="advisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<!-- 注入advice -->
<property name="advice" ref="beforeAdvice"></property>
<!-- 注入需要被拦截的目标对象中的方法 -->
<property name="patterns">
<list>
<value>.*delete</value>
<value>.*update</value>
</list>
</property>
</bean>
==========================================================================================
4.spring 自定义属性编辑器PropertyEditor
proEdit
有时候我们需要一个类的多个不同对象,如果在容器中实例化多个bean,显然比较麻烦,Spring中我们可以使用属性编辑器来将特定的字符串转换为对象
String-->object
java.beans.PropertyEditor(JDK中)用于将xml文件中字符串转换为特定的类型
JDK为我们提供一个实现类PropertyEditorSupport
Spring在注入时,如果遇到类型不一致则会去调用相应的属性编辑器进行转换,
调用属性编辑器的setAsText(String str)进行处理,调用其getValue()获取处理后得到的对象
自定义属性编辑器示例
public class AddressEditor extends PropertyEditor{
public void setAsText(String text) throws IllegalArgumentException{
String str = text.split("[,]");
Address addr = new Address(str[0],str[1],Integer.parseInt(str[2]));
//设置到父类中,以便Spring调用getValue可以获得
setValue(addr);
}
}
public class User {
private Address address;
//getter setter
}
public class Address {
private String city;
private String street;
private int code;
//getter setter
}
xml:
customEditors 是CustomEditorConfigurer类的一个属性,这个属性的值是一个Map类型的,Map的KEY 是需要编辑的属性的类型,对应的value值是属性编辑器类
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="com.briup.ioc.Address> //key为目标类
<bean class="com.briup.ioc.AddressEditor" /> //属性编辑器
</entry>
</map>
</property>
</bean>
<bean id="user" class="com.briup.ioc.User">
<property name="address" value="城市,街道,471900" /> //此处串会被转换towired
</bean>
======================================================================================================
5.spring 中的annotation配置(注解)
@Autowired spring 通过BeanPostProcessor对@Autowired进行解析,所以必须声明AutowiredAnnotationBeanPostProcessor Bean
<!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
或使用隐式注册 <context:annotation-config/>
、@Autowired默认按照类型匹配的方式进行注入
、@Autowired注解可以用于成员变量、setter方法、构造器函数等
、使用@Autowired注解须有且仅有一个与之匹配的Bean,当找不到匹配的 Bean 或者存在多个匹配的Bean时,Spring 容器将抛出 异常
、Spring 允许我们通过 @Qualifier 注释指定注入 Bean 的名称。@Autowired 和 @Qualifier 结合使用时,自动注入的策略就从 byType 转变成 byName 了。
public class MovieRecommender {
@Autowired
@Qualifier("mainCatalog")
private MovieCatalog movieCatalog;
private CustomerPreferenceDao customerPreferenceDao;
@Autowired
public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
this.customerPreferenceDao = customerPreferenceDao;
}
-----------------------------------------------------------------------------
@Resource
1)、@Resource 的作用相当于 @Autowired+@Qualifier 需配<beanclass="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
默认byname 找不到beanname当做bytype
------------------------------------------------------------------------------------------------
@Component
1)、使用@Component注解可以直接定义Bean,而无需在xml定义。但是若两种定义同时存在,xml中的定义会覆盖类中注解的Bean定义。
2)、@Component 有一个可选的入参,用于指定 Bean 的名称。
@Component
public class ActionMovieCatalog implements MovieCatalog {
// ...
@Scope("prototype") 单利与非单例。非单例可以保证安全,单例数据共享
@Component
3)、<context:component-scan/> 允许定义过滤器将基包下的某些类纳入或排除。Spring 支持以下 4 种类型的过滤方式:
过滤器类型 表达式范例
annotation org.example.SomeAnnotation
assignable org.example.SomeClass
regex org\.example\.Default.*
aspectj org.example..*Service+
下面这个XML配置会忽略所有的@Repository注解
<beans ...>
<context:component-scan base-package="org.example">
<context:include-filter type="regex" expression=".*Stub.*Repository"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
</beans>
Spring 2.5以后引入了更多典型化注解(stereotype annotations): @Component、@Service和 @Controller。 @Component是所有受Spring管理组件的通用形式; 而@Repository、@Service和 @Controller则是@Component的细化, 用来表示更具体的用例(例如,分别对应了持久化层、服务层和表现层)
6)、要检测这些类并注册相应的bean,需要在XML中包含以下元素,其中'basePackage'是两个类的公共父包 (或者可以用逗号分隔的列表来分别指定包含各个类的包)。
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="org.example"/>
</beans>
====================================================================================================================================