zoukankan      html  css  js  c++  java
  • spring之注解详解

    一、类级别注解,效果相同

    • 通用:@Component(value="name")
    • Controller层:@Controller(value="name")
    • Service层:@Service(value="name")
    • Dao层:@Repository(value="name")

    二、属性注入注解

    • 普通属性注入2种方式: 

       1.在属性上注入,底层使用反射给该字段赋值,破坏了封闭原则,但简易明了。

        @value(“xxx”)

        private String name;

       2.在set方法上注入

        @value("xxx")

        public void setName(String name){

          this.name = name;

        }

    • 引用属性注入

    1. @Autowired和@Qualifier搭配使用

        都是spring提供的注解,Autowired自动装配,Qualifier指明具体注入id或者name为哪个的bean对象

        例如:

        @Autowired
        @Qualifier("plan2")

        private Plan plan;

      2.@Resource

        javax中的注解,可以带参数,直接指明id或者name为哪个的bean对象

    三、生命周期方法注解

             初始化:@PostConstruct

             销毁:@PreDestroy

        均来自于jdk自带注解。

    四、作用域

             @Scope("prototype") 多例,默认是单例如,一般springmvc采用单例(有全局变量例外),struts2采用多例

    前提1:

    前提2:

       添加扫描

    <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.itheima.g_annotation.a_ioc"></context:component-scan>
    </beans>
    Best Regards
  • 相关阅读:
    Leetcode Plus One
    Leetcode Swap Nodes in Pairs
    Leetcode Remove Nth Node From End of List
    leetcode Remove Duplicates from Sorted Array
    leetcode Remove Element
    leetcode Container With Most Water
    leetcode String to Integer (atoi)
    leetcode Palindrome Number
    leetcode Roman to Integer
    leetcode ZigZag Conversion
  • 原文地址:https://www.cnblogs.com/pecool/p/8259335.html
Copyright © 2011-2022 走看看