@Controller,@Service,@Repository,@Component详解。
来源:互联网 发布:淘宝标题关键词测试 编辑:程序博客网 时间:2024/06/15 18:28
@Controller
用来表示一个web控制层bean,如SpringMvc中的控制器。
@Service
用来表示一个业务层bean。
@Repository
用来表示一个持久层bean,即数据访问层DAO组件。
@Component
用来表示一个平常的普通组件,当一个类不合适用以上的注解定义时用这个组件修饰。
需要注意的是@Controller,@Service,@Repository都有带@Component父注解,说明它们除了基本组件的属性外还有其他的的场景应用,即如果不用SpringMVC其实它们就是一个普通的组件,但普通组件建议最好还是用@Component修饰。
为了让Spring自动扫描注册这些组件,需要在配置文件中加上扫描的配置,如扫描com.test包下的注解。
些扫描配置默认use-default-filters="true",默认扫描@Component注解及子注解,可以配置过滤只扫描哪些注解不扫描哪些注解。
要过滤扫描注解,需要相应的带上下面的子标签,可以有多个。
如只扫描com.test包下的@Controller和@Service注解的组件。
关于type的定义
Filter TypeExamples ExpressionDescription
annotationorg.example.SomeAnnotation符合SomeAnnoation的target class
assignableorg.example.SomeClass指定class或interface的全名
aspectjorg.example..*Service+AspectJ语法
regexorg\.example\.Default.*Regelar Expression
customorg.example.MyTypeFilter自定义Type,实现接口org.springframework.core.type.TypeFilter
另外,配置可以有多个。
- @Controller,@Service,@Repository,@Component详解。
- @Component @Service @Controller @Repository
- @Component、@Repository @Service、@Controller
- @Component @Service @Controller @Repository
- @Component,@Service,@Controller,@Repository
- @Repository @Service @Controller @Component
- @Repository、@Service、@Controller、@Component
- @Component、@Repository、@Service、@Controller
- 解释@Component @Controller @Service @Repository
- 注解@Component @Controller @Service @Repository
- @Repository、@Service、@Controller 和 @Component
- 解释@Component @Controller @Service @Repository
- 解释@Component @Controller @Service @Repository
- 解释@Component @Controller @Service @Repository
- @Repository、@Service、@Controller 和 @Component
- @Component 、@Repository、@Service 和 @Controller
- @Repository、@Service、@Controller 和 @Component
- @Repository、@Service、@Controller 和 @Component
- 设计模式之单例模式实践
- 获取Spring的ApplicationContext的几种方式
- Spring AOP注解为什么失效?90%Java程序员不知道
- @Resource,@Autowired,@Inject3种注入方式详解
- codeforces Div.2 #840D Leha and another game about graph 顶点权值为度数对应奇偶
- @Controller,@Service,@Repository,@Component详解。
- Spring零配置之@Configuration注解详解。
- 一张图搞懂Spring bean的完整生命周期。
- JAVA元注解@interface详解(@Target,@Documented,@Retention,@Inherited)。
- 自定义View (五) 阴影、渐变
- IntegerCache的妙用和陷阱!
- 屌炸天,JDK8的排序大法!!
- 为什么byte取值-128~127??
- 高级JAVA码农必须搞清楚它们的区别:instanceof、isInstance、isAssignableFrom