当前位置: 首页 > news >正文

使用注解开发

(1)在spring4之后,要使用注解开发,必须要保证aop的包导入了

 

使用注解需要导入context约束,增加注解的支持!

精简版:

<?xml version="1.0" encoding="UTF-8"?>
<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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>
    
    

</beans>

扩展版: 

<?xml version="1.0" encoding="UTF-8"?>
<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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

    <!--
      指定要扫描的包,这个包下的注解就会生效
      -->
    <context:component-scan base-package="com.gt.pojo"/>


</beans>

(2)bean

 绿色的叶子表示放入组件当中!

 

 User

package com.gt.pojo;

import org.springframework.stereotype.Component;

//@Component 等价于<bean id="user" class="com.gt.pojo.User"
//@Component 组件

@Component
public class User {

    public String name = "山姆";
}

MyTest

import com.gt.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //getBean括号里面的东西就是默认类的小写
        User user = context.getBean("user",User.class);
        System.out.println(user.name);
    }
}

 

(3)属性如何注入

  • @Component 等价于<bean id="user" class="com.gt.pojo.User"
  • @Component 就是组件
  • @Value("shanmu2") 相当于在beans.xml中<property name="name" value="shanmu2"/>
     

User

package com.gt.pojo;

        import org.springframework.beans.factory.annotation.Value;
        import org.springframework.stereotype.Component;

//@Component 等价于<bean id="user" class="com.gt.pojo.User"
//@Component 组件

@Component
public class User {

    //@Value("shanmu2") 相当于在beans.xml中<property name="name" value="shanmu2"/>

    public String name;
    
    @Value("shanmu2")
    public void setName(String name) {
        this.name = name;
    }
}

 

 扩展(Web):

1、衍生的注解

@Component有几个衍生注解,我们在web开发中,会按照mvc三层架构分层

  • dao【@Repository】

  • service【@Service】

  • controller【@Controller】

    这四个注解功能都是一样的,都是代表某个类注册到spring,装配bean

 

2、自动装配

  • @Autowired:自动装配通过类型。名字
  • @Qualifier:如果Autowired不能唯一自动装配上属性,则需要通过@Qualifier(value='xxx')
  • @Resource:自动装配通过名字。类型
  • @Component 等价于<bean id="user" class="com.gt.pojo.User"/>
  • @Scope("prototype") 这个等价于作用域
  • @Value("山姆02")  * 相当于 <bean id="user" class="com.gt.pojo.User"><property name="name" value="山姆02"/> </bean>

3、作用域

/**
 * @Component 等价于<bean id="user" class="com.gt.pojo.User"/>
 *
 */
@Component
// @Scope("prototype") 这个等价于作用域
@Scope("prototype") 
public class User {
    /**
     * 相当于
     *     <bean id="user" class="com.gt.pojo.User">
     *         <property name="name" value="山姆02"/>
     *     </bean>
     */
//    @Value("山姆02")
    public String name;


    @Value("shanmu02")
    public void setName(String name) {
        this.name = name;
    }
}

小结

xml与注解:

  • xml更加万能,适用于任何场合,维护简单方便

  • 注解不是自己的类使用不了,维护相对复杂

xml与注解最佳实践:

  • xml用来管理bean

  • 注解只负责完成属性的注入

  • 我们在使用的过程中,需要注意一个问题:必须让注解生效,就需要开启注解的支持

    <context:annotation-config/>

    <!--
      指定要扫描的包,这个包下的注解就会生效
      -->
    <context:component-scan base-package="com.gt"/>

相关文章:

  • 秒杀系统高并发优化
  • 神经网络是线性分类器吗,神经网络是线性的吗
  • socket编程
  • 多传感器融合定位技术
  • 2022第十一届PMO大会(线上会议)成功召开
  • 公众号如何运营?教你几招超实用的公众号运营方法
  • C# 第六章『交互式图形界面』◆第5节:FolderBrowserDialog类、DialogResult枚举
  • 氨基化/环氧化/胺化/羧基化/巯基改性/笼空状磺化聚苯乙烯微球相关制备
  • docker-compose安装mysql主流版本及差异
  • 牛客多校3 - Journey(建图,最短路)
  • python如何实现数据可视化,如何用python做可视化
  • 开源治理的基本实践与指导原则
  • 【APP测试】怎么对App进行功能测试
  • Mybatis-Plus复习
  • 8、JAVA入门——switch选择结构
  • 4个实用的微服务测试策略
  • JavaScript设计模式与开发实践系列之策略模式
  • oldjun 检测网站的经验
  • sublime配置文件
  • Vue UI框架库开发介绍
  • Vue2.x学习三:事件处理生命周期钩子
  • 解析带emoji和链接的聊天系统消息
  • 力扣(LeetCode)22
  • 嵌入式文件系统
  • 如何正确配置 Ubuntu 14.04 服务器?
  • 小程序开发之路(一)
  • 中文输入法与React文本输入框的问题与解决方案
  • MyCAT水平分库
  • ​软考-高级-系统架构设计师教程(清华第2版)【第15章 面向服务架构设计理论与实践(P527~554)-思维导图】​
  • # Swust 12th acm 邀请赛# [ E ] 01 String [题解]
  • # 手柄编程_北通阿修罗3动手评:一款兼具功能、操控性的电竞手柄
  • $.extend({},旧的,新的);合并对象,后面的覆盖前面的
  • (1/2)敏捷实践指南 Agile Practice Guide ([美] Project Management institute 著)
  • (173)FPGA约束:单周期时序分析或默认时序分析
  • (3)选择元素——(17)练习(Exercises)
  • (PWM呼吸灯)合泰开发板HT66F2390-----点灯大师
  • (过滤器)Filter和(监听器)listener
  • (每日持续更新)信息系统项目管理(第四版)(高级项目管理)考试重点整理 第13章 项目资源管理(七)
  • (四)Tiki-taka算法(TTA)求解无人机三维路径规划研究(MATLAB)
  • (原創) 如何安裝Linux版本的Quartus II? (SOC) (Quartus II) (Linux) (RedHat) (VirtualBox)
  • (转)setTimeout 和 setInterval 的区别
  • (转)用.Net的File控件上传文件的解决方案
  • .apk文件,IIS不支持下载解决
  • .NET 的静态构造函数是否线程安全?答案是肯定的!
  • .NET/C# 推荐一个我设计的缓存类型(适合缓存反射等耗性能的操作,附用法)
  • .Net6 Api Swagger配置
  • .net6Api后台+uniapp导出Excel
  • .NET开源全面方便的第三方登录组件集合 - MrHuo.OAuth
  • /usr/lib/mysql/plugin权限_给数据库增加密码策略遇到的权限问题
  • @Autowired标签与 @Resource标签 的区别
  • [ CTF ] WriteUp- 2022年第三届“网鼎杯”网络安全大赛(朱雀组)
  • [ 数据结构 - C++] AVL树原理及实现
  • [].shift.call( arguments ) 和 [].slice.call( arguments )
  • [AIGC] 开源流程引擎哪个好,如何选型?
  • [APIO2015]巴厘岛的雕塑