码迷,mamicode.com
首页 > 其他好文 > 详细

今日收获-context:component-scan

时间:2018-07-20 11:13:08      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:VID   pre   ext   res   str   ons   ado   only   reg   

一、要有命名空间 xmlns:context="http://www.springframework.org/schema/context"

二、作用:直接上解释吧!

Scans the classpath for annotated components that will be auto-registered as Spring beans. By default,
the Spring-provided @Component, @Repository, @Service, @Controller, @RestController,
@ControllerAdvice, and @Configuration stereotypes will be detected. Note: This tag implies the effects
of the ‘annotation-config‘ tag, activating @Required, @Autowired, @PostConstruct, @PreDestroy,
@Resource, @PersistenceContext and @PersistenceUnit annotations in the component classes, which is
usually desired for autodetected components (without external configuration). Turn off the ‘annotation-
config‘ attribute to deactivate this default behavior, for example in order to use custom
BeanPostProcessor definitions for handling those annotations. Note: You may use placeholders in
package paths, but only resolved against system properties (analogous to resource paths). A
component scan results in new bean definitions being registered; Spring‘s
PropertySourcesPlaceholderConfigurer will apply to those bean definitions just like to regular bean
definitions, but it won‘t apply to the component scan settings themselves. See javadoc for
org.springframework.context.annotation.ComponentScan for information on code-based alternatives to
bootstrapping component-scanning.

Content Model : (include-filter*, exclude-filter*)

三、实例

<!-- 配置自动扫描的包 -->
<context:component-scan
base-package="com.it.aop"></context:component-scan>

@Component("arithmeticCalculator")
public class ArithmeticCalculatorImpl

ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) ctx.getBean("arithmeticCalculator");

-- 释放资源

((ConfigurableApplicationContext)ctx).close();
 int result = arithmeticCalculator.add(1, 2);System.out.println("result:" + result);

今日收获-context:component-scan

标签:VID   pre   ext   res   str   ons   ado   only   reg   

原文地址:https://www.cnblogs.com/songzhenyi/p/9339485.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!