Spring @Resource @Autowired @Component常用注解
2017-03-24 11:32
501 查看
package com.test;@Resource 和 @Autowired看案例 抱着疑问往下看 然后再往回从头看:import javax.annotation.Resource;public class Test{ @Resource private Cat cat; @Resource(name="office") private Office office; @Resource(type="Teach.class")private Teach teach;}@Conponent("cat")public class Cat{}...以此类推一般情况下,我们无需使用类似于
@Resource(type=Car.class)的注释方式,因为 Bean 的类型信息可以通过 Java 反射从代码中获取。要让 JSR-250 的注释生效,除了在 Bean 类中标注@Resource等,还需要在 spring 容器中注册一个负责处理注解的配置:<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
CommonAnnotationBeanPostProcessor实现了
BeanPostProcessor接口,它负责扫描使用了 JSR-250 注释的 Bean,并对它们进行相应的操作。@Resource装配顺序如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常如果既没有指定name,又没有指定type,则自动按照byName方式进行装配如果没有匹配,则回退为一个原始类型(如CatDao)进行匹配,如果匹配则自动装配;Spring 不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource、@PostConstruct以及@PreDestroy。 @Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按byName自动注入罢了。@Resource有两个属性是比较重要的,分别是name和type,Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。建议:@Resource(JSR-250标准注解,推荐使用它来代替Spring专有的@Autowired注解),别忘了Spring加上解析注解的配置。
@Component("licenceFacadeImpl")
泛指组件(建议指定其name),相当于配置文件中的<bean id="" class=""/>,当组件不好归类时可以使用该注解; 知识点:@Controller 和 @Service 都可以使用@Component来替代
@Controller
指控制层组件,默认名称是类名(首字母小写)
@Service
指服务层组件,默认名称是类名(首字母小写)
@Transactional
指事务组件,默认名称是类名(首字 aa68 母小写)
<!-- 配置 Annotation 驱动,扫描@Transactional注解的类定义事务 --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
@MyBatisDao
指数据层组件,默认名称是类名(首字母小写)
<!-- 扫描basePackage下所有以@MyBatisDao注解的接口 --> <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <property name="basePackage" value="com.hive"/> <property name="annotationClass" value="com.MyBatisDao"/> </bean>
此处为自定义注解:@MyBatisDao 替代了@Repository数据层(DAO)组件
package com;
@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.TYPE})@Documented@Componentpublic @interface MyBatisDao {String value() default "";}
相关文章推荐
- spring 常用注解关系(@Component,@Controller,@Service,@Repository,@Autowired,@Resource),@Qualifier
- Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier、@scope
- Spring @Autowired@Component@ Resource注解
- Spring @Autowired、@Resource、@Required、@Component、@Repository、@Service、@Controller注解的用法和作用
- spring注解( @Autowired、@Qualifier、@Resource、@PostConstruct、@PreDestroy、 @Component、@Scope)-描述的比较清楚
- Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier 解析[转]
- Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier 解析
- spring注解( @Autowired、@Qualifier、@Resource、@PostConstruct、@PreDestroy、 @Component、@Scope)-描述的比较清楚
- Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier 解析
- spring部分注解@Repository,@Service,@Controller,@Component,@scope,@Resource,@Autowired
- Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier 解析
- 常用注解分析@RequestMapping @AutoWired@Resource @component
- Java框架spring 学习笔记(九):Spring的bean管理(@Required、@Component、@Autowired、@Resource注解)
- Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier 解析
- 终极讲解,看了立马懂 --》Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier 解析
- Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier 解析
- Spring(八):@Resource和@Autowired注解以及其它常用注解
- Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier、@scope
- spring 注解@Resource 和 @Autowired
- Spring中@Autowired注解、@Resource注解的区别