Spring 使用注解开发


Posted in Java/Android onMay 20, 2022

在Spring4之后 要使用注解开发 必须保证aop包导入了

Spring 使用注解开发

使用注解需要导入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>

@Component:组件放在类上 说明这个类被Spring管理了 就是bean

import org.springframework.stereotype.Component;
//等价于<bean id="user" class="com.kero.pojo.User"/>
@Component
public class User {
    public String name = "xxx";
}

@Value

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
//等价于<bean id="user" class="com.kero.pojo.User"/>
@Component
public class User {
    @Value("xxx")
//等价于<property name="name" value="xxx"/>
    public String name;
}

或者

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
//等价于<bean id="user" class="com.kero.pojo.User"/>
@Component
public class User {  
    public String name;
    @Value("xxx")
    public void setName(String name) {
        this.name = name;
    }
}

@Component有几个衍生的注解 我们在Web开发中会按照MVC三层架构分层

·dao[@Repository]

·service[@Service]

·controller[@Controller]

这四个注解功能一样 都是代表将某个类注册到Spring中 装配Bean

Spring 使用注解开发

Spring 使用注解开发

Spring 使用注解开发

注解的作用域@Scope

@Scope 放在类上,默认是单例模式

@Scope(prototype)是原型模式,每次创建的都是一个新的对象

Spring 使用注解开发

其作用等价于

Spring 使用注解开发

补充:

@Scope("singleton") 或者@Scope 单例模式 下面代码输出结果为true

@Scope("prototype")下面代码输出结果为false

import com.kero.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");
        User user = context.getBean("user", User.class);
        User user2 = context.getBean("user", User.class);
        System.out.println(user==user2);
    }
}

xml vs 注解

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

·注解 不是自己类使用不聊 维护相对复杂

最佳实践:xml用来管理bean

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

我们在使用的过程中 需要注意 使用以下代码

<!--指定要扫描的包 这个包下的注解就会生效->-->
    <context:component-scan base-package="com.kero"/>
    <!--开启注解的支持-->
    <context:annotation-config/>

针对最佳实践的例子

<?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:component-scan base-package="com.kero"/>
    <!--开启注解的支持-->
    <context:annotation-config/>
    <bean id="user" class="com.kero.pojo.User" scope="prototype"/>
</beans>
import org.springframework.beans.factory.annotation.Value;
public class User {
    @Value("XXX")
    public String name;
    public void setName(String name) {
        this.name = name;
    }
}

到此这篇关于Spring详解使用注解开发流程的文章就介绍到这了!


Tags in this post...

Java/Android 相关文章推荐
Java各种比较对象的方式的对比总结
Jun 20 Java/Android
如何解决springcloud feign 首次调用100%失败的问题
Jun 23 Java/Android
java固定大小队列的几种实现方式详解
Jul 15 Java/Android
Java比较两个对象中全部属性值是否相等的方法
Aug 07 Java/Android
Java 在生活中的 10 大应用
Nov 02 Java/Android
Java异常处理try catch的基本用法
Dec 06 Java/Android
Java中的随机数Random
Mar 17 Java/Android
springboot layui hutool Excel导入的实现
Mar 31 Java/Android
Java设计模式之代理模式
Apr 22 Java/Android
JAVA springCloud项目搭建流程
May 11 Java/Android
Java 轮询锁使用时遇到问题
May 11 Java/Android
SpringBoot接入钉钉自定义机器人预警通知
Jul 15 Java/Android
MyBatis核心源码深度剖析SQL语句执行过程
Java 轮询锁使用时遇到问题
May 11 #Java/Android
Java 死锁解决方案
May 11 #Java/Android
JAVA springCloud项目搭建流程
May 11 #Java/Android
Java死锁的排查
May 11 #Java/Android
Java线程的6种状态与生命周期
May 11 #Java/Android
Java 多线程协作作业之信号同步
May 11 #Java/Android
You might like
example1.php
2006/10/09 PHP
一个用于网络的工具函数库
2006/10/09 PHP
PHP 上传文件大小限制
2009/07/05 PHP
destoon公司主页模板风格的添加方法
2014/06/20 PHP
PHP中round()函数对浮点数进行四舍五入的方法
2014/11/19 PHP
jQuery 使用手册(六)
2009/09/23 Javascript
JS 判断undefined的实现代码
2009/11/26 Javascript
JavaScript DOM学习第四章 getElementByTagNames
2010/02/19 Javascript
JS 实现完美include载入实现代码
2010/08/05 Javascript
理解Javascript_13_执行模型详解
2010/10/20 Javascript
js捕获鼠标滚轮事件代码
2013/12/16 Javascript
jquery制作搜狐快站页面效果示例分享
2014/02/21 Javascript
jquery操作checkbox实现全选和取消全选
2014/05/02 Javascript
javascript单例模式的简单实现方法
2015/07/25 Javascript
ionic js 复选框 与普通的 HTML 复选框到底有没区别
2016/06/06 Javascript
jquery弹出框插件jquery.ui.dialog用法分析
2016/08/20 Javascript
JavaScript实战(原生range和自定义特效)简单实例
2016/08/21 Javascript
AngularJS表单验证中级篇(3)
2016/09/28 Javascript
基于ajax和jsonp的原生封装(实例)
2017/10/16 Javascript
代码详解javascript模块加载器
2018/03/04 Javascript
vue实现扫码功能
2020/01/17 Javascript
JS如何实现封装列表右滑动删除收藏按钮
2020/07/23 Javascript
Python转码问题的解决方法
2008/10/07 Python
Python字典及字典基本操作方法详解
2018/01/30 Python
解决python3 安装不了PIL的问题
2019/08/16 Python
TensorFlow实现自定义Op方式
2020/02/04 Python
Python3压缩和解压缩实现代码
2021/03/01 Python
详解如何在css3打包后自动追加前缀插件:autoprefixer
2018/12/18 HTML / CSS
欧迪办公美国官网:Office Depot
2016/08/22 全球购物
信息专业本科生个人的自我评价
2013/10/28 职场文书
普通话宣传标语
2014/06/26 职场文书
民政局2016年“六一”儿童节慰问活动总结
2016/04/06 职场文书
2019自荐信范文集锦!
2019/07/03 职场文书
Html5调用企业微信的实现
2021/04/16 HTML / CSS
python 实现德洛内三角剖分的操作
2021/04/22 Python
请求模块urllib之PYTHON爬虫的基本使用
2022/04/08 Python