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 18 Java/Android
图解排序算法之希尔排序Java实现
Jun 26 Java/Android
Java中PriorityQueue实现最小堆和最大堆的用法
Jun 27 Java/Android
Java数组与堆栈相关知识总结
Jun 29 Java/Android
小程序与后端Java接口交互实现HelloWorld入门
Jul 09 Java/Android
MyBatis-Plus 批量插入数据的操作方法
Sep 25 Java/Android
Java实现二分搜索树的示例代码
Mar 17 Java/Android
Java Lambda表达式常用的函数式接口
Apr 07 Java/Android
Java 超详细讲解hashCode方法
Apr 07 Java/Android
解决springboot druid数据库连接失败后一直重连的方法
Apr 19 Java/Android
Android基础入门之dataBinding的简单使用教程
Jun 21 Java/Android
AndroidStudio图片压缩工具ImgCompressPlugin使用实例
Aug 05 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
一个用于网络的工具函数库
2006/10/09 PHP
php 保留小数点
2009/04/21 PHP
在Windows系统上安装PHP运行环境文字教程
2010/07/19 PHP
PHP中error_reporting()函数的用法(修改PHP屏蔽错误)
2011/07/01 PHP
php中存储用户ID和密码到mysql数据库的方法
2013/02/06 PHP
PHP魔术引号所带来的安全问题分析
2014/07/15 PHP
PHP编程实现csv文件导入mysql数据库的方法
2017/04/29 PHP
google jQuery 引用文件,jQuery 引用地址集合(jquery 1.2.6至jquery1.5.2)
2011/04/24 Javascript
利用JS自动打开页面上链接的实现代码
2011/09/25 Javascript
js原生appendChild的bug解决心得分享
2013/07/01 Javascript
jquery实现弹出层完美居中效果
2014/03/03 Javascript
2014年最火的Node.JS后端框架推荐
2014/10/27 Javascript
js多功能分页组件layPage使用方法详解
2016/05/19 Javascript
基于js实现checkbox批量选中操作
2016/11/22 Javascript
JS中parseInt()和map()用法分析
2016/12/16 Javascript
配置nodejs环境的方法
2017/05/13 NodeJs
Angularjs添加排序查询功能的实例代码
2017/10/24 Javascript
vue+swiper实现组件化开发的实例代码
2017/10/26 Javascript
给localStorage设置一个过期时间的方法分享
2018/11/06 Javascript
jQuery实现根据身份证号获取生日、年龄、性别等信息的方法
2019/01/09 jQuery
vue 的 solt 子组件过滤过程解析
2019/09/07 Javascript
Vue组件化开发之通用型弹出框的实现
2020/02/28 Javascript
Vue2.x-使用防抖以及节流的示例
2021/03/02 Vue.js
[01:07:19]2018DOTA2亚洲邀请赛 4.5 淘汰赛 Mineski vs VG 第一场
2018/04/06 DOTA
wxPython窗口中文乱码解决方法
2014/10/11 Python
基于Pycharm加载多个项目过程图解
2020/01/19 Python
python实现与redis交互操作详解
2020/04/21 Python
万得城电器土耳其网站:欧洲第一大电子产品零售商
2016/10/07 全球购物
英国在线女鞋目的地:SIMMI
2018/12/27 全球购物
公务员年总结的自我评价
2013/10/25 职场文书
公司董事长职责
2013/12/12 职场文书
工厂实习感言
2014/01/14 职场文书
音乐教学随笔感言
2014/02/19 职场文书
国际贸易专业个人鉴定
2014/02/22 职场文书
党员干部承诺书
2014/03/25 职场文书
公证委托书大全
2014/04/04 职场文书