zoukankan      html  css  js  c++  java
  • Spring常用注解

    Spring常用注解

    使用注解之前要开启自动扫描功能,其中base-package为需要扫描的包(含子包),开启注解支持

    <?xml version="1.0" encoding="UTF-8"?>
    <!--suppress ALL -->
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
        
        <context:component-scan base-package="com.xia"></context:component-scan>
        <context:annotation-config/>
    
    </beans>
    
    • @Autowired:顾名思义,就是自动装配,其作用是为了消除代码Java代码里面的getter/setter与bean属性中的property。当然,getter看个人需求,如果私有属性需要对外提供的话,应当予以保留。

    • @Resource:与@Autowired类似,但更强大。@autowire通过byType实现,而且必须要求这个对象存在,@resource默认通过byName实现,如果找不到,通过byType实现

    • Qualifier(指定注入Bean的名称:如果容器中有一个以上匹配的Bean,则可以通过@Qualifier注解限定Bean的名称

    • @Component:等价于 组件,放在类上,说明此类被ico容器管理

    • @Service用于标注业务层组件

    • @Controller用于标注控制层组件(如struts中的action)

    • @Repository用于标注数据访问组件,即DAO组件。

    • @Value 用于类成员的初始化

    • @Scope("prototype") Bean的作用域定义

    xml与注解

    • xml更加万能,维护简单
    • 注解:不是自己的类,使用不了,维护复杂

    最佳实践:

    • xml用来管理bean
    • 注解只用来完成属性的注入
  • 相关阅读:
    nyoj 202红黑树 (搜索)
    POJ 3281 Dining(最大流)
    nyoj-488 素数环 +nyoj -32 组合数 (搜索)
    LeetCode100:Same Tree
    LeetCode283:Move Zeros
    Leetcode226:Invert Binary Tree
    LeetCode258:Add Digits
    Leetcode237:Delete Node in a Linked List
    LeetCode7:Reverse Integer
    LeetCode292:Nim Game
  • 原文地址:https://www.cnblogs.com/xiaxiaopi/p/14459882.html
Copyright © 2011-2022 走看看