闲来无事,看了一下jar包的结构,今天来学习一下
就用这个spring-aop的jar包来举例子:
首先是META-INF文件夹下的文件:INDEX.LIST
INDEX.LIST:顾名思义,索引列表。这个应该就是描述了当前jar包下所有的资源文件的路径,类似一个索引,可能是为了方便查找各个类文件
网上是这样解释INDEX.LIST的: 这个文件由 jar 工具的新选项 -i 生成,它包含在应用程序或者扩展中定义的包的位置信息。它是 JarIndex 实现的一部分,并由类装载器用于加速类装载过程。
MANIFEST.MF:这个 manifest 文件定义了与扩展和包相关的数据。
其实,INDEX.LIST和MANIFEST.MF应该是jar包生成工具在压缩jar包时自动生成的2个文件
spring.handlers:spring.handlers是aopNamespaceHandler和XSD文件的连接器(aopNamespaceHandler通过spring.handlers配置文件找到XSD文件)。指明命名空间需要哪个类来处理,这里是aopNamespaceHandler这个类来处理命名空间
spring.schemas:指明了schema文件的位置,spring会使用这里制定的xsd文件来验证配置的正确性。
spring容器在启动的时候会根据spring.handlers和spring.schemas的配置加载文件内容,然后去解析文件中的配置信息。
AopNamespaceHandler类:
上面实例主要说明config这个自定义标签通过new ConfigBeanDefinitionParser()通过这个parser进行解析。public class AopNamespaceHandler extends NamespaceHandlerSupport { public AopNamespaceHandler() { } public void init() { this.registerBeanDefinitionParser("config", new ConfigBeanDefinitionParser()); this.registerBeanDefinitionParser("aspectj-autoproxy", new AspectJAutoProxyBeanDefinitionParser()); this.registerBeanDefinitionDecorator("scoped-proxy", new ScopedProxyBeanDefinitionDecorator()); this.registerBeanDefinitionParser("spring-configured", new SpringConfiguredBeanDefinitionParser()); } }
spring.schemas中的配置:
http\://www.springframework.org/schema/aop/spring-aop-2.0.xsd=org/springframework/aop/config/spring-aop-2.0.xsd
http\://www.springframework.org/schema/aop/spring-aop-2.5.xsd=org/springframework/aop/config/spring-aop-2.5.xsd
http\://www.springframework.org/schema/aop/spring-aop-3.0.xsd=org/springframework/aop/config/spring-aop-3.0.xsd
http\://www.springframework.org/schema/aop/spring-aop.xsd=org/springframework/aop/config/spring-aop-3.0.xsd
既然是通过xsd文件来做到扩展的,那么我们看一下spring-aop.xsd文件,随便看一下
<xsd:element name="aspectj-autoproxy">
<xsd:annotation>
<xsd:documentation source="java:org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"><![CDATA[
Enables the use of the @AspectJ style of Spring AOP.
]]></xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:sequence>
<xsd:element name="include" type="includeType" minOccurs="0" maxOccurs="unbounded">
<xsd:annotation>
<xsd:documentation><![CDATA[
Indicates that only @AspectJ beans with names matched by the (regex)
pattern will be considered as defining aspects to use for Spring autoproxying.
]]></xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="proxy-target-class" type="xsd:boolean" default="false">
<xsd:annotation>
<xsd:documentation><![CDATA[
Are class-based (CGLIB) proxies to be created? By default, standard
Java interface-based proxies are created.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="expose-proxy" type="xsd:boolean" default="false">
<xsd:annotation>
<xsd:documentation><![CDATA[
Indicate that the proxy should be exposed by the AOP framework as a
ThreadLocal for retrieval via the AopContext class. Off by default,
i.e. no guarantees that AopContext access will work.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
我们看到了一个名叫:org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator的类
这个类就是spring-aop的主要类,jar包中的其他类也是通过这样的方式被加载的。
因为主题本来是想介绍jar包的结构,但是觉得只说jar包的结构可能没多少东西
那么就来说一下服务器启动到java项目的加载的过程吧:
服务器启动的时候,会在服务端发布3个文件夹:libs,classes,web.xml
libs里面就是我们的jar包,web.xml会通过配置监听来将spring容器的信息加载,这些信息一般放在一个叫:applicationContext.xml的文件里,最后,在这个文件中通过bean的方式来使用aop或者spring的其他技术。
web.xml配置:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
applicationContext.xml:
<bean id="LogAopFormat" class="com.assa.asas.log.tool.LogAopFormat"/>
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.aaa.bbb.service.impl.sssServiceImpl.*(..))"/>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.aaa.bbb.service.impl.sassServiceImpl.*(..))"/>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.aaa.bbb.service.impl.EndorseServiceImpl.*(..))"/>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.aaa.bbb.service.impl.ascasServiceImpl.*(..))"/>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.aaa.bbb.service.impl.ascsaServiceImpl.*(..))"/>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.aaa.bbb.service.impl.ascsaServiceImpl.*(..))"/>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.aaa.bbb.service.impl.ascasServiceImpl.*(..))"/>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.aaa.bbb.service.impl.ascasServiceImpl.*(..))"/>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.aaa.bbb.service.impl.acascServiceImpl.*(..))"/>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.ccsee.ecargo.service.impl.QuotaInsureServiceImpl.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.ccsee.ecargo.service.impl.BillServiceImpl.*(..))"/>
<aop:aspect id="LogAopFormatAspect" ref="LogAopFormat">
<aop:pointcut id="log"
expression="execution(* com.ccsee.ecargo..service..*Service*Impl.*(..))||execution(* com.ccsee.ecargo..web.*.*(..))"/>
<aop:before method="beforeMethod" pointcut-ref="log" />
<aop:after method="afterMethod" pointcut-ref="log"/>
</aop:aspect>
</aop:config>
这里是用aop来做日志。