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 相关文章推荐
深入理解以DEBUG方式线程的底层运行原理
Jun 21 Java/Android
java设计模式--原型模式详解
Jul 21 Java/Android
Sleuth+logback 设置traceid 及自定义信息方式
Jul 26 Java/Android
Java 在生活中的 10 大应用
Nov 02 Java/Android
OpenCV实现普通阈值
Nov 17 Java/Android
详解Spring Security中的HttpBasic登录验证模式
Mar 17 Java/Android
SpringBoot2零基础到精通之数据与页面响应
Mar 22 Java/Android
springcloud整合seata
May 20 Java/Android
SpringBoot使用ip2region获取地理位置信息的方法
Jun 21 Java/Android
java实现web实时消息推送的七种方案
Jul 23 Java/Android
httpclient调用远程接口的方法
Aug 14 Java/Android
Android实现获取短信验证码并自动填充
May 21 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
汉字转化为拼音(php版)
2006/10/09 PHP
PHP 学习路线与时间表
2010/02/21 PHP
完美实现GIF动画缩略图的php代码
2011/01/02 PHP
关于UEditor编辑器远程图片上传失败的解决办法
2012/08/31 PHP
解析php中die(),exit(),return的区别
2013/06/20 PHP
php生成随机颜色的方法
2014/11/13 PHP
Laravel5中contracts详解
2015/03/02 PHP
javascript基础的动画教程,直观易懂
2007/01/10 Javascript
js每次Title显示不同的名言
2008/09/25 Javascript
跟我学Nodejs(二)--- Node.js事件模块
2014/05/21 NodeJs
Jquery操作Ajax方法小结
2015/11/29 Javascript
JavaScript代码判断点击第几个按钮
2015/12/13 Javascript
jquery使用Cookie和JSON记录用户最近浏览历史
2016/04/19 Javascript
浅谈JavaScript对象的创建方式
2016/06/13 Javascript
浅谈JavaScript中变量和函数声明的提升
2016/08/09 Javascript
利用Node.js编写跨平台的spawn语句详解
2017/02/12 Javascript
Bootstrap Table使用整理(五)之分页组合查询
2017/06/09 Javascript
Vue项目路由刷新的实现代码
2019/04/17 Javascript
微信小程序如何调用图片接口API并居中显示
2019/06/29 Javascript
[01:24:34]2014 DOTA2华西杯精英邀请赛5 24 DK VS LGD
2014/05/25 DOTA
[06:06]2018DOTA2亚洲邀请赛主赛事第四日战况回顾 全明星赛欢乐上演
2018/04/07 DOTA
python制作websocket服务器实例分享
2016/11/20 Python
Flask框架实现给视图函数增加装饰器操作示例
2018/07/16 Python
python os.listdir按文件存取时间顺序列出目录的实例
2018/10/21 Python
对Python正则匹配IP、Url、Mail的方法详解
2018/12/25 Python
对python中类的继承与方法重写介绍
2019/01/20 Python
Python3.5实现的三级菜单功能示例
2019/03/25 Python
python读取多层嵌套文件夹中的文件实例
2020/02/27 Python
基于python实现获取网页图片过程解析
2020/05/11 Python
浅谈Python里面None True False之间的区别
2020/07/09 Python
浅谈HTML5 服务器推送事件(Server-sent Events)
2017/08/01 HTML / CSS
英国最大的线上保健品零售商之一:Vitamin Planet
2016/12/01 全球购物
德国在线购买葡萄酒网站:Geile Weine
2019/09/24 全球购物
文明上网主题班会
2015/08/14 职场文书
2016年安康杯竞赛活动总结
2016/04/05 职场文书
幽默口才训练经典句子(48句)
2019/08/19 职场文书