spring IOC注解:
@Autowired:表示自动注入,要让 @Autowired 起作用必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。
<!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
或者:
使用隐式注册(隐式注册 post-processors 包括了 AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor
<?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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
</beans>
@Autowired默认按照类型匹配的方式进行注入
@Autowired注解可以用于成员变量、setter方法、构造器函数等
使用@Autowired注解须有且仅有一个与之匹配的Bean,当有多个匹配的bean或者没有找到匹配的bean,spring会抛异常
@Resource:
@Resource 的作用相当于 @Autowired,只不过 @Autowired 默认按byType 自动注入,而@Resource 默认按 byName自动注入罢了。
@Resource 有两个属性是比较重要的:name和type,指定了name,则按byName自动注入,指定了type,则按byType自动注入,两者都不指定,则按默认的byName自动注入
@PostConstruct 和 @PreDestroy:
@PostConstruct:
被@PostConstruct标注的方法会在该类实例化后被自动调用,
@PreDestroy:
被@PreDestroy标注的方法会在该类销毁后被自动调用
@Component:
被@Component标注的bean表示需要spring依赖注入,若xml里也定义了,xml中的定义会覆盖类中注解的Bean定义。
@Component 有一个可选的入参,用于指定 Bean 的名称。
示例:
@Component
public class ActionMovieCatalog implements MovieCatalog {
// ...
}
@Component 定义的 Bean默认都是singleton单实例的,@Scope注解可以指定是单例还是非单例
示例:
@Scope("prototype")
@Repository
public class MovieFinderImpl implements MovieFinder {
// ...
}
Spring 2.5引入了更多典型化注解(stereotype annotations):
@Component、@Repository、@Service和 @Controller。
@Component是所有受Spring管理组件的通用形式;
而@Repository、@Service和 @Controller则是@Component的细化
@Repository:用于Dao层
@Service:用于service层bean的定义
@Controller:用于控制层bean的定义
<context:component-scan/>:定义过滤器将基包下的某些类纳入或者排除,spring提供了如下4种过滤方式
示例:【base-package】多个包名之间可以用逗号隔开
<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>
<ref local=""> 在当前XML中根据id查找bean
<ref bean="">可以不在同一XML中,首先根据id查找,没找到再根据bean的name查找
bean自动注入的几种模式即autowire:
byName 按名称
byType 按类型
constructor 按构造函数
autodetect 自动判断使用哪种注入方式,优先使用constructor,没有提供constructor,则使用byType
no 不使用自动装配,需要使用<property>来配置bean之间的依赖关系
dependency-check:bean自动装配的依赖检查,检查bean之间的依赖是否都按照预设那样装配完成:
simple: 只对简单基本属性进行bean依赖装配是否完成检查
object: 只对对象类型进行依赖检查
all: simple和object的综合体
none: 就是不进行依赖检查
依赖的作用就是在应用程序启动时就能帮你检查bean自动装配是否成功完成,而不用把问题延迟到运行时出现错误
List装配:
<list>
<value></value>
<value></value>
......
</list>
set装配
<set>
<value></value>
<value></value>
......
</set>
map装配
<map>
<entry key="">
<value></value>
</entry>
<entry key="">
<value></value>
</entry>
......
</map>
properties装配:
<props>
<prop key="1">aaa</prop>
<prop key="2">bbb</prop>
......
</props>
BeanWrapper接口:
把普通的java bean转换成BeanWrapper的实现类BeanWrapperImpl,然后我们的普通java bean就受Spring IOC容器管理了,可以通过
BeanWrapper接口像操作bean本身一样读写其对象属性。但是BeanWrapper一次只能添加一个bean到IOC容器,这种单一的硬编码方式不可取。
BeanFactory接口:
顾名思义,就是bean的工厂类,必须首先在spring的xml中注册bean,然后加载xml根据bean的注册id通过工厂类获取bean
ApplicationContext接口:
ApplicationContext接口相当于BeanFactory接口的升级版,BeanFactory接口的功能ApplicationContext接口全都有,ApplicationContext接口还
支持国际化,支持在web上下文中获取资源,支持事件传递等等。
Spring国际化:
applicationContext.getMessage(key, Object[], Locale);
第一个参数:对应properties资源文件里的key,即根据key获取value
第二个参数:Object[]即传递给properties资源文件的参数,资源文件的参数用{index}表示,index从0开始,且需要与数组里
元素一一对应
第三个参数:Locale即本地语言环境,如Local.CHINA/Local.US
jsp页面上可以通过jstl标签获取资源文件的value,如<fmt:message key=""/>
当然spring也提供了一套标签来获取资源文件的值,如<spring:message code=""/>(这里的code对应资源文件里的key)
如果需要传参,则这样使用:<spring:message code=""?arguments="参数值1,参数值2"/>多个参数值用逗号分割开。
Spring对于资源文件的访问:(这里的文件可以是properties/xml/txt等等任何文件)
applicationContext.getResource(filepath); 方法返回Resource对象,通过对象可以getFile()得到文件对象,
通过getInputStream()可以得到文件输入字节流。
参数filepath有3种方式给定:
1.根据类路径方式:加classpath:前缀,举例说明:
classpath:com/yida/config/applicationcontext.xml
2.硬盘上文件系统的绝对路径,就是在哪个盘哪个文件夹下,需要加前缀file:,举例说明:
file:D:\\demo\a.txt
3.相对web应用中的WEB-INF存放,如 WEB-INF/config/a.xml
spring事件传递:
首先自定义事件event,需要继承ApplicationEvent抽象类,
然后定义事件发布类,需实现ApplicationContextAware接口,通过applicationContext上下文将事件发布到spring容器,然后通知listener,
然后创建事件监听器,需实现ApplicationListener接口,重写onApplicationEvent方法,即事件触发时要做的操作。
最后在spring配置文件里注册该listener,实际应用中只需获取事件发布实现类对象,发布事件即可,事件触发和触发后所做操作,完全由listener控制。
Java代理的几种实现方式:
1.继承方式:不可取,由于java类的单一继承性,故没有实际价值,比较流行的一句话是:多用依赖或接口而不是继承
2.依赖方式:就是在一个类内部持有另一个类对象的引用,这样就可以根据另一个对象的引用调用其方法,再其方法开头结尾添加自定义实现,从而实现方法扩展。
但是依赖也有弊端,过于依赖使得系统维护成本和难度加大。
3.实现jDK的InvocationHandler接口方式:
虽然可以实现任意接口的代理,但是客户端还是必须关心接口的具体实现类,而最好是只暴露接口给客户端,降低客户端使用难度才是最好的方式。
spring 代理配置:
<bean id="logAround" class="test.aop.interceptor.LogInterceptor"></bean>
<bean id="timebook" class="test.aop.interceptor.TimeBook"></bean>
<bean id="logProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>test.aop.interceptor.ITimeBook</value>
</property>
<property name="interceptorNames">
<list>
<value>logAround</value>
<value>timebook</value>
</list>
</property>
</bean>
proxyInterfaces: 代理类需要实现的接口
interceptorNames:配置代理类的拦截处理类,引用bean id,该bean需要实现MethodInterceptor接口,若不
为代理类指定拦截方法,则默认会拦截目标所有方法,且前后增强
spring前后增强配置:
<bean id="logAround" class="test.aop.interceptor.LogInterceptor"></bean>
<bean id="timebook" class="test.aop.interceptor.TimeBook"></bean>
<bean id="advisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref bean="logAround"/>
</property>
<property name="patterns">
<list>
<value>.*sayHello1.*</value>
<value>.*sayHello2.*</value>
</list>
</property>
</bean>
<bean id="logProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>test.aop.interceptor.ITimeBook</value>
</property>
<property name="interceptorNames">
<list>
<value>advisor</value>
<value>timebook</value>
</list>
</property>
</bean>
spring前增强配置:
实现MethodBeforeAdvice接口,其他跟前后增强配置类似
spring后增强配置:
实现AfterReturningAdvice接口,其他跟前后增强配置类似
spring异常通知:
实现ThrowsAdvice接口,spring2.x需要强制实现afterThrowing方法,
spring3.0也需要定义这样一个方法,但是方法前3个参数可选
public void afterThrowing(Throwable throwable) throws Throwable{
System.out.println("方法出现异常");
}
转载于:https://blog.51cto.com/sun316737332/1253562