spring IOC annotation
来源:互联网 发布:unity3d ngui 实例 编辑:程序博客网 时间:2024/06/06 18:11
@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface Component {/** * The value may indicate a suggestion for a logical component name, * to be turned into a Spring bean in case of an autodetected component. * @return the suggested component name, if any */String value() default "";}
@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Componentpublic @interface Repository {/** * The value may indicate a suggestion for a logical component name, * to be turned into a Spring bean in case of an autodetected component. * @return the suggested component name, if any */String value() default "";}
@Component("userDao")注解在类上,表示定义了一个bean,id为userDao,和此注解类似的还有:@Repository,@Service,@Controller用来注解dao层,业务逻辑层和控制层,他们都会被解析成bean,只是后面几个spring会赋予一些特殊功能,所以写的时候根据类的作用写不同的注解
要在定义bean的时候,给bean定义scope那么就再添加一个注解@Scope
@Target({ElementType.TYPE, ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface Scope {/** * Specifies the scope to use for the annotated component/bean. * @return the specified scope */String value() default BeanDefinition.SCOPE_SINGLETON;/** * Specifies whether a component should be configured as a scoped proxy * and if so, whether the proxy should be interface-based or subclass-based. * <p>Defaults to {@link ScopedProxyMode#NO}, indicating that no scoped * proxy should be created. * <p>Analogous to {@literal <aop:scoped-proxy/>} support in Spring XML. */ScopedProxyMode proxyMode() default ScopedProxyMode.DEFAULT;}
要实现bean的装配,那么使用@Autowired,@Qualifier,@Resource
@Autowired 默认按类型进行装配,如果找不到就出异常,required属性定义为false就不会出异常了
@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD})public @interface Autowired {/** * Declares whether the annotated dependency is required. * <p>Defaults to <code>true</code>. */boolean required() default true;}
如果容器中有一个以上的bean匹配,那么使用@Qualifier限定Bean的名称,和@Autowired配合使用
@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE})@Inherited@Documentedpublic @interface Qualifier {String value() default "";}
@Resource和@Autowired类似,不过@Resource是javax.annotation包下的
@Target({TYPE, FIELD, METHOD})@Retention(RUNTIME)public @interface Resource {
@Resource("car") 找id为car的bean注入,这个注解要求提供一个bean名称的属性,如果没有指定,那么默认采用标注处的变量名或方法名作为bean的名称。与@Resource雷同的还有@Inject,很少使用,建议用@Autowired
@Resource装配顺序
如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常
如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常
如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常
如果既没有指定name,又没有指定type,则自动按照byName方式进行装配(见2);如果没有匹配,则回退为一个原始类型(UserDao)进行匹配,如果匹配则自动装配;
在类中定义了这些注解后,要在配置文件中使用context命名空间中的component-scan的base-package属性指定扫描的路径,那么该包以及下面所有的子包中的类都会被扫描
<context:component-scan base-package="com.baobaotao.anno"/>
这个元素里可以配置扫描哪些类,排除哪些类
<context:component-scan base-package="com.auscend"><!-- 安全性配置,如果启用安全性,请注释掉 --><context:exclude-filter type="regex" expression="com.auscend.secure.*"/><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan>
<context:component-scan base-package="com.baobaotao"> <context:include-filter type="regex" expression="com\.baobaotao\.anno.*Dao"/> <context:include-filter type="regex" expression="com\.baobaotao\.anno.*Service"/> <context:exclude-filter type="aspectj" expression="com.baobaotao..*Controller+"/></context:component-scan>
<context:component-scan base-package="com.baobaotao" resource-pattern="anno/*.class"/>
支持的过滤器类型:
org.example.SomeAnnotation
An annotation to be present at the type level in target components.assignableorg.example.SomeClass
A class (or interface) that the target components are assignable to (extend/implement).aspectjorg.example..*Service+
An AspectJ type expression to be matched by the target components.regexorg\.example\.Default.*
A regex expression to be matched by the target components class names.customorg.example.MyTypeFilter
A custom implementation of the org.springframework.core.type .TypeFilter
interface.练习时请使用ApplicationContext,因为有些BeanFacory不支持自动依赖注入。
- spring ioc annotation 理解
- spring IOC annotation
- spring IOC annotation
- spring IOC annotation
- Spring IOC XML Annotation
- Spring Annotation详解 --- IOC篇
- spring的IOC基于annotation
- spring的IOC基于annotation
- Spring IOC 的 Annotation 简析
- 关于 Spring Ioc annotation 的心得体会
- Spring AOP IOC 的 Annotation 实现方式
- Spring IoC Annotation 的简单使用
- Spring Annotation 笔记——IOC篇
- Spring Annotation 笔记——IOC篇
- Spring中IOC的Annotation实现
- 关于Spring IOC的注解Annotation
- Spring Annotation 笔记——IOC篇
- Spring IOC Annotation 注入 学习实例
- 一位年轻女董事长的37条忠告
- 手机在于折腾,探索未知世界!
- LR有的JMeter也有之一“参数化”
- *.sct: error: No section matches selector - no section to be FIRST/LAST.
- 基于java的俄罗斯小方块(二),code,code...
- spring IOC annotation
- jQuery中的DOM操作
- 数字图像噪声的分类
- Signing JAR Files
- LR有的JMeter也有之二“检查点”
- 解决多登陆的问题
- go learn
- visual c++2010密钥
- html中有一段公用的代码。在多个页面用到。是否可以将他做成公用代码?怎么调用?