<(|求北京gt赛车5注册方法谢谢|)>

我们在 SpringMVC 开发项目中,有的用注解和 XML 配置 Bean, 这两种都各有自己的优势,数据源配置比较经常用 XML 配置,控制层依赖的 service 比较经常用注解等(在部署时比较不会改变的),我们经常比较常用的注解有 @Component 是通用标注, @Controller 标注 web 控制器, @Service 标注 Servicec 层的服务, @Respository 标注 DAO 层的数据访问。 SpringMVC 启动时怎么被自动扫描然后解析并注册到 Bean 工厂中去(放到 DefaultListableBeanFactory 中的 Map&String,&BeanDefinition&&beanDefinitionMap 中&以 BeanName 为 key )?我们今天带着这些问题来了解分析这实现的过程,我们在分析之前先了解一下这些注解。
@Controller 标注 web 控制器, @Service 标注 Service 层的服务, @Respository 标注 DAO 层的数据访问。 @Component 是通用标注,只是定义为一个类为 Bean , SpringMVC 会把所有添加 @Component 注解的类作为使用自动扫描注入配置路径下的备选对象。 @Controller 、 @Service/@Respository 只是更加的细化,都是被 @Component 标注,所以我们比较不推荐使用 @Component 。源代码如下:
@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Componentpublic @interface Service { String value() default &&;}@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Componentpublic @interface Controller { String value() default &&;}@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Componentpublic @interface Repository { String value() default &&;}都是有标示 @Component 我们在配置文件中,标示配置需要扫描哪些包下,也可以配置对某个包下不扫描,代码如下:&context:component-scan base-package=&cn.test&&
&context:exclude-filter type=&regex& expression=&cn.test.*.*.controller&/&
&context:exclude-filter type=&regex& expression=&cn.test.*.*.controller2&/&&/context:component-scan&说明:&context:exclude-filter& 指定的不扫描包, &context:exclude-filter& 指定的扫描包 SpringMVC 先读取配置文件,然后根据 context:component-scan 中属性 base-package 去扫描指定包下的 class 和 jar 文件,把标示 @Controller 标注 web 控制器, @Service 标注 Servicec 层的服务, @Respository 标注 DAO 层的数据访问等注解的都获取,并注册为 Bean 类放到 Bean 工厂,我们接下来要分析的这个过程。我们平时项目开发都是这样的注解,实现 MVC 模式,代码如下: 例如://控制层@Controller@RequestMapping(value=&/test&)public class TestController2 { @Autowired private TestService testS @RequestMapping(value=&/index&) public String getIndex(Model model){
return &&; }}//服务层@Service(&testService&)public class TestServiceImpl implementsTestService{}我们今天的入口点就在这,因为解析注解的到注册,也是先读取配置文件并解析,在解析时扫描对应包下的 JAVA 类,里面有 DefaultBeanDefinitionDocumentReader 这个类, doRegisterBeanDefinitions 这个方法实现解析配置文件的 Bean ,这边已经读取进来形成 Document& 形式存储,然后开始解析 Bean, 是由 BeanDefinitionParserDelegate 类实现的, BeanDefinitionParserDelegate 完成具体 Bean 的解析(例如: bean 标签、 import 标签等)这个在 上一篇SpringMVC 源代码深度解析 IOC容器(Bean 解析、注册)
里有解析,今天注解属于扩展的标签,是由 NamespaceHandler 和 BeanDefinitionParser 来解析。源代码如下: public BeanDefinition parseCustomElement(Element ele, BeanDefinition containingBd) {
String namespaceUri = getNamespaceURI(ele);
NamespaceHandler handler = this.readerContext.getNamespaceHandlerResolver().resolve(namespaceUri);
if (handler == null) {
error(&Unable to locate Spring NamespaceHandler for XML schema namespace [& + namespaceUri + &]&, ele);
return handler.parse(ele, new ParserContext(this.readerContext, this, containingBd)); }NamespaceHandler 这边这边起到了什么作用,根据不同的 Namespace 获取不同的 NamespaceHandler ,因为我们在 Beans 标签配置了命名空间,然后就可以配置对应的标签,解析标签时,比较有自己的所实现的 NamespaceHandler 来解析,如图所示: NamespaceHandler 中的 parse 方法是它的子类类 NamespaceHandlerSupport 实现的,获取通过 findParserForElement 方法获取 BeanDefinitionParser& 对象,这个对象在工程初始化时就直接实例化放在缓存中 Map&String,&BeanDefinitionParser& ,然后通过 localName 获取,源代码如下: public BeanDefinition parse(Element element, ParserContext parserContext) {
return findParserForElement(element, parserContext).parse(element, parserContext); } private BeanDefinitionParser findParserForElement(Element element, ParserContext parserContext) {
String localName = parserContext.getDelegate().getLocalName(element);
BeanDefinitionParser parser = this.parsers.get(localName);
if (parser == null) {
parserContext.getReaderContext().fatal(
&Cannot locate BeanDefinitionParser for element [& + localName + &]&, element);
}为什么要获取 BeanDefinitionParser& ,因为 BeanDefinitionParser& 类是解析配置文件中的 &context:component-scan&,&aop:config& 等标签,但是不同的标签是由不同的 BeanDefinitionParser 来进行解析的,如图所示:
接下来我们开始解析这个标签, &context:component-scan& 标签的解析是由 ComponentScanBeanDefinitionParser 类解析的,接下来我们要分析它怎么解析注解的 Bean ,并把 Bean 注册到 Bean 工厂,源代码如下: public BeanDefinition parse(Element element, ParserContext parserContext) { //获取context:component-scan 配置的属性base-package的值
String[] basePackages = StringUtils.tokenizeToStringArray(element.getAttribute(BASE_PACKAGE_ATTRIBUTE),
ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS); //创建扫描对应包下的class文件的对象
ClassPathBeanDefinitionScanner scanner = configureScanner(parserContext, element); //扫描对应包下的class文件并有注解的Bean包装成BeanDefinition
Set&BeanDefinitionHolder& beanDefinitions = scanner.doScan(basePackages);
registerComponents(parserContext.getReaderContext(), beanDefinitions, element);
}说明:( 1 )获取 context:component-scan& 配置的属性 base-package 的值,然后放到数组。 ( 2 )创建扫描对应包下的 class 和 jar 文件的对象 ClassPathBeanDefinitionScanner& ,由这个类来实现扫描包下的 class 和 jar 文件并把注解的 Bean 包装成 BeanDefinition 。 ( 3 ) BeanDefinition 注册到 Bean 工厂。
第一:扫描是由 ComponentScanBeanDefinitionParser 的 doScan 方法来实现的,源代码如下:
protected Set&BeanDefinitionHolder& doScan(String... basePackages) {//新建队列来保存BeanDefinitionHolder
Set&BeanDefinitionHolder& beanDefinitions = new LinkedHashSet&BeanDefinitionHolder&();//循环需要扫描的包
for (String basePackage : basePackages) { //进行扫描注解并包装成BeanDefinition
Set&BeanDefinition& candidates = findCandidateComponents(basePackage);
for (BeanDefinition candidate : candidates) {
ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(candidate);
candidate.setScope(scopeMetadata.getScopeName());
String beanName = this.beanNameGenerator.generateBeanName(candidate, this.registry);
if (candidate instanceof AbstractBeanDefinition) {
postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName);
if (candidate instanceof AnnotatedBeanDefinition) {
AnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate);
if (checkCandidate(beanName, candidate)) {
BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, beanName);
definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
beanDefinitions.add(definitionHolder); //对BeanDefinition进行注册
registerBeanDefinition(definitionHolder, this.registry);
return beanD }进行扫描注解并包装成 BeanDefinition 是 ComponentScanBeanDefinitionParser 由父类 ClassPathScanningCandidateComponentProvider 的方法 findCandidateComponents 实现的,源代码如下: public Set&BeanDefinition& findCandidateComponents(String basePackage) {
Set&BeanDefinition& candidates = new LinkedHashSet&BeanDefinition&();
try { //base-package中的值替换为classpath*:cn/test/**/*.class
String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
resolveBasePackage(basePackage) + &/& + this.resourceP//获取所以base-package下的资源
Resource[] resources = this.resourcePatternResolver.getResources(packageSearchPath);boolean traceEnabled = logger.isTraceEnabled();
boolean debugEnabled = logger.isDebugEnabled();
for (Resource resource : resources) {
if (traceEnabled) {
logger.trace(&Scanning & + resource);
if (resource.isReadable()) {
MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(resource); //对context:exclude-filter进行过滤
if (isCandidateComponent(metadataReader)) { //包装BeanDefinition
ScannedGenericBeanDefinition sbd = new ScannedGenericBeanDefinition(metadataReader);
sbd.setResource(resource);
sbd.setSource(resource);
if (isCandidateComponent(sbd)) {
if (debugEnabled) {
logger.debug(&Identified candidate component class: & + resource);
candidates.add(sbd);
if (debugEnabled) {
logger.debug(&Ignored because not a concrete top-level class: & + resource);
if (traceEnabled) {
logger.trace(&Ignored because not matching any filter: & + resource);
catch (Throwable ex) {
throw new BeanDefinitionStoreException(
&Failed to read candidate component class: & + resource, ex);
if (traceEnabled) {
logger.trace(&Ignored because not readable: & + resource);
catch (IOException ex) {
throw new BeanDefinitionStoreException(&I/O failure during classpath scanning&, ex);
}说明:(1) 先根据 context:component-scan& 中属性的 base-package=&cn.test& 配置转换为 classpath*:cn/test/**/*.class ,并扫描对应下的 class 和 jar 文件并获取类对应的路径,返回 Resources (2) 根据 &context:exclude-filter& 指定的不扫描包, &context:exclude-filter& 指定的扫描包配置进行过滤不包含的包对应下的 class 和 jar。 ( 3 )封装成 BeanDefinition 放到队列里。
1 )怎么根据 packageSearchPath 获取包对应下的 class 路径,是通过 PathMatchingResourcePatternResolver 类, findAllClassPathResources(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length())); 获取配置包下的 class 路径并封装成 Resource ,实现也是 getClassLoader().getResources(path); 实现的。源代码如下:
&span style=&font-size:18&&public Resource[] getResources(String locationPattern) throws IOException {
Assert.notNull(locationPattern, &Location pattern must not be null&);
if (locationPattern.startsWith(CLASSPATH_ALL_URL_PREFIX)) {
// a class path resource (multiple resources for same name possible)
if (getPathMatcher().isPattern(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()))) {
// a class path resource pattern
return findPathMatchingResources(locationPattern);
// all class path resources with the given name
return findAllClassPathResources(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()));
// Only look for a pattern after a prefix here
// (to not get fooled by a pattern symbol in a strange prefix).
int prefixEnd = locationPattern.indexOf(&:&) + 1;
if (getPathMatcher().isPattern(locationPattern.substring(prefixEnd))) {
// a file pattern
return findPathMatchingResources(locationPattern);
// a single resource with the given name
return new Resource[] {getResourceLoader().getResource(locationPattern)};
} }protected Resource[] findAllClassPathResources(String location) throws IOException {
String path =
if (path.startsWith(&/&)) {
path = path.substring(1);
Enumeration&URL& resourceUrls = getClassLoader().getResources(path);
Set&Resource& result = new LinkedHashSet&Resource&(16);
while (resourceUrls.hasMoreElements()) {
URL url = resourceUrls.nextElement();
result.add(convertClassLoaderURL(url));
return result.toArray(new Resource[result.size()]); }&/span& 说明: getClassLoader().getResources 获取 classpath*:cn/test/**/*.class 下的 cn/test 包下的 class 的路径信息。并返回了 URL 。这里能把对应 class 路径获取到了,就能获取里面的信息。
2 ) isCandidateComponent 实现的标签是里配置的 &context:exclude-filter& 指定的不扫描包, &context:exclude-filter& 指定的扫描包的过滤,源代码如下:
&span style=&font-size:18&&protected boolean isCandidateComponent(MetadataReader metadataReader) throws IOException {
for (TypeFilter tf : this.excludeFilters) {
if (tf.match(metadataReader, this.metadataReaderFactory)) {
for (TypeFilter tf : this.includeFilters) {
if (tf.match(metadataReader, this.metadataReaderFactory)) {
AnnotationMetadata metadata = metadataReader.getAnnotationMetadata();
if (!metadata.isAnnotated(Profile.class.getName())) {
AnnotationAttributes profile = MetadataUtils.attributesFor(metadata, Profile.class);
return this.environment.acceptsProfiles(profile.getStringArray(&value&));
}&/span& 说明:& this.excludeFilters 有 pattern 属性,值是就是 &context:exclude-filter&type=&regex&&expression=&cn.test.*.*.controller&/& 的 cn.test.*.*.controller 值 this.pattern.matcher(metadata.getClassName()).matches(); 通过这个去匹配,如果是就返回 false 。如图所示:
我们到这边已经把对应的通过在 XML 配置把注解扫描解析并封装成 BeanDefinition 。 接下来我们来分析一下注册到 Bean 工厂,大家还记得 ComponentScanBeanDefinitionParser 的 doScan 方法,然后到工厂的是由 registerBeanDefinition(definitionHolder,&this.registry); 实现的,源代码如下: protected void registerBeanDefinition(BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry) {
BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, registry); }public static void registerBeanDefinition(
BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry)
throws BeanDefinitionStoreException {
// Register bean definition under primary name.
String beanName = definitionHolder.getBeanName();
registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition());
// Register aliases for bean name, if any.
String[] aliases = definitionHolder.getAliases();
if (aliases != null) {
for (String aliase : aliases) {
registry.registerAlias(beanName, aliase);
} } public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
throws BeanDefinitionStoreException {
Assert.hasText(beanName, &Bean name must not be empty&);
Assert.notNull(beanDefinition, &BeanDefinition must not be null&);
if (beanDefinition instanceof AbstractBeanDefinition) {
((AbstractBeanDefinition) beanDefinition).validate();
catch (BeanDefinitionValidationException ex) {
throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,
&Validation of bean definition failed&, ex);
synchronized (this.beanDefinitionMap) {
Object oldBeanDefinition = this.beanDefinitionMap.get(beanName);
if (oldBeanDefinition != null) {
if (!this.allowBeanDefinitionOverriding) {
throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,
&Cannot register bean definition [& + beanDefinition + &] for bean '& + beanName +
&': There is already [& + oldBeanDefinition + &] bound.&);
if (this.logger.isInfoEnabled()) {
(&Overriding bean definition for bean '& + beanName +
&': replacing [& + oldBeanDefinition + &] with [& + beanDefinition + &]&);
this.beanDefinitionNames.add(beanName);
this.frozenBeanDefinitionNames =
this.beanDefinitionMap.put(beanName, beanDefinition);
resetBeanDefinition(beanName); }说明: DefaultListableBeanFactory 要实现的保存到 Map&String,&BeanDefinition&&beanDefinitionMap 中&以 BeanName 为 key ,如果有,就不用保存了。 DefaultListableBeanFactory 我们在 上一篇SpringMVC 源代码深度解析 IOC容器(Bean 解析、注册) 有介绍过了, DefaultListableBeanFactory继承了BeanFactory。
(1) 因为解析注解的到注册,也是先读取配置文件并解析,在解析时扫描对应包下的 JAVA 类,里面有 DefaultBeanDefinitionDocumentReader 这个类, doRegisterBeanDefinitions 这个方法实现解析配置文件的 Bean ,这边已经读取进来形成 Document& 形式存储。然后 注解属于扩展的标签,是由 NamespaceHandler 和 BeanDefinitionParser 来解析。
& &(2) 根据 context:component-scan 中属性 base-package 去扫描指定包下的 class 和 jar 文件,获取对应的路径信息,然后根据配置 &context:exclude-filter& 指定的扫描包配置进行过滤不包含的包对应下的 class 和 jar路径的 Resources。
& & &(3) 把标示 @Controller 标注 web 控制器, @Service 标注 Servicec 层的服务, @Respository 标注 DAO 层的数据访问等注解路径都获取包装成 BeanDefinition ,并注册为
Bean 类放到 Bean 工厂,也就是 DefaultListableBeanFactory Map&String,&BeanDefinition&&beanDefinitionMap 中 以 BeanName 为 key。在使用Jquery的html(data)方法执行写数据到Dom元素时遇到一个问题:在data参数中包含script脚本块的时候,html(data)方法的执行结果与预期不符
“今天才注意到jQuery(...).html()方法返回的HTML会过滤掉所有的&script&块,但有时候,特别是在AJAX应用中,常常是需要保留HTML中的&script&块的,比如局部更新含脚本的内容,此时切忌不要用html()来读写HTML,还是要老老实实用elem.innerHTML。今天因为这个问题浪费了1个小时,特此给大家提个醒。”   经过测试发现,新版本(1.3.2之后的,之前版本未考证过)的jQuery(...).html()方法已经不存在该问题(html()方法是一个读Dom元素数据的操作),但是当用html(data)方法写数据到Dom元素并且data参数中包含script脚本时,又出现了异常现象:在Firefox(我用的3.6)浏览器下执行上述操作,data参数里面的script脚本会自动运行,引起页面破相、异常等错误,比如我遇到的问题就是:在script中存在document.write方法时,在执行完html(data)操作后整个页面的原有的dom元素都消失了,只剩下document.write()方法的执行结果,引起了页面破相;具体原因待讨论,最后还是用elem.innerHTML=data这种方式解决了这个问题,特此标记。U9论坛欢迎您,有任何疑问均可以发帖咨询!
看一看U9会员都在讨论什么~
欢迎关注游久网电竞专区。
查看: 3496|回复: 9
你猜我是谁&
必杀系点放的
不会用绝招&&请赐教
触手+电钻变态怪叔叔.
节操5 绿叶0 注册时间在线时间828 小时阅读权限40精华39主题听众数最后登录KPI值0
Lv.10 热心玩家[初], 积分 4568, 距离下一级还需 932 积分
UID2906867帖子U9币45 魅力0 声誉0 U菜花0
啊?那是什么游戏?给些消息 1,不然谁知道是什么游戏?
screen.width*0.7) {this.resized= this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized= this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {} else {window.open('/albums/k211/K9999MARIE13/gal-002.jpg?t=');}" onmousewheel="return imgzoom(this);" alt="" />
如果知道自己每次轮回都死,但又每次都逃不掉,那太恐怖了.
节操3 绿叶0 注册时间在线时间1204 小时阅读权限50精华8主题听众数最后登录KPI值0
Lv.13 社区元老[初], 积分 9572, 距离下一级还需 1928 积分
UID2907355帖子U9币11142 魅力12 声誉0 U菜花0
楼主可以去各类格斗电子出招表里看看。
暗魔院(暗黑魔法学院)3年生
节操0 绿叶0 注册时间在线时间364 小时阅读权限20精华1主题听众数最后登录KPI值0
正式会员[中], 积分 1037, 距离下一级还需 463 积分
UID2908575帖子U9币1547 魅力0 声誉0 U菜花0
异种角斗士没有出招表,抱住对手按方向键和A(打)或B(蹦)都行,要看谁按的快,方向键的不同攻击对手的方法也就不同。大招是满气抱住对手按上和B(蹦)。
本帖子中包含更多资源
才可以下载或查看,没有帐号?
screen.width*0.7) {this.resized= this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized= this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {} else {window.open('/albums/userpics/datab/92/52/C89e11f8cefa.gif');}" onmousewheel="return imgzoom(this);" alt="" />世纪公爵
uu9_46whf 该用户已被删除
提示: 作者被禁止或删除 内容自动屏蔽
触手+电钻变态怪叔叔.
节操5 绿叶0 注册时间在线时间828 小时阅读权限40精华39主题听众数最后登录KPI值0
Lv.10 热心玩家[初], 积分 4568, 距离下一级还需 932 积分
UID2906867帖子U9币45 魅力0 声誉0 U菜花0
我知道了,确实是我想到的那游戏,那游戏气满一直上按拳就可以出必杀了。
screen.width*0.7) {this.resized= this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized= this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {} else {window.open('/albums/k211/K9999MARIE13/gal-002.jpg?t=');}" onmousewheel="return imgzoom(this);" alt="" />
如果知道自己每次轮回都死,但又每次都逃不掉,那太恐怖了.
uu9_46whf 该用户已被删除
提示: 作者被禁止或删除 内容自动屏蔽
MAX黑势力社团老大
节操1 绿叶0 注册时间在线时间372 小时阅读权限20精华5主题听众数最后登录KPI值0
正式会员[高], 积分 1805, 距离下一级还需 195 积分
UID2906253帖子U9币1626 魅力0 声誉0 U菜花0
这个游戏感觉还可以....当年在机厅里此机器和那个摔交是最容易坏的2台...
&&至今还记得,那个牛头发超必杀的时候,喊的是&啊哦一温撒波&``````````````!
凄 美 的 歌 特 民 謠
screen.width*0.7) {this.resized= this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized= this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {} else {window.open('/attachments/month_0707/x6nD+w==_Vv0Rl2YNhzaX.jpg');}" onmousewheel="return imgzoom(this);" alt="" />
节操0 绿叶0 注册时间在线时间546 小时阅读权限30精华0主题听众数最后登录KPI值0
常驻会员[初], 积分 1860, 距离下一级还需 640 积分
UID2910479帖子U9币1623 魅力0 声誉0 U菜花0
这游戏很有纪念意义的。。。前几个月刚找到
uu9_46whf 该用户已被删除
提示: 作者被禁止或删除 内容自动屏蔽
) 管理员(非地图作者)QQ:
Powered by}

我要回帖

更多关于 amp gt . amp lt 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信