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 相关文章推荐
springboot @ConfigurationProperties和@PropertySource的区别
Jun 11 Java/Android
Java方法重载和方法重写的区别到底在哪?
Jun 11 Java/Android
Java如何实现树的同构?
Jun 22 Java/Android
springBoot基于webSocket实现扫码登录
Jun 22 Java/Android
Java图书管理系统,课程设计必用(源码+文档)
Jun 30 Java/Android
SpringBoot SpringEL表达式的使用
Jul 25 Java/Android
Java比较两个对象中全部属性值是否相等的方法
Aug 07 Java/Android
关于EntityWrapper的in用法
Mar 22 Java/Android
Java中的继承、多态以及封装
Apr 11 Java/Android
Java 使用类型为Object的变量指向任意类型的对象
Apr 13 Java/Android
Jmerte 分布式压测及分布式压测配置
Apr 30 Java/Android
Java Spring Boot请求方式与请求映射过程分析
Jun 25 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
成为好程序员必须避免的5个坏习惯
2014/07/04 PHP
CodeIgniter多语言实现方法详解
2016/01/20 PHP
PHP常用的三种设计模式汇总
2016/08/28 PHP
php中的单引号、双引号和转义字符详解
2017/02/16 PHP
js异步加载的三种解决方案
2013/03/04 Javascript
js综合应用实例简单的表格统计
2013/09/03 Javascript
Extjs实现进度条的两种便捷方式
2013/09/26 Javascript
Jquery 动态循环输出表格具体方法
2013/11/23 Javascript
JavaScript学习笔记之数组的增、删、改、查
2016/03/23 Javascript
JS修改地址栏参数实例代码
2016/06/14 Javascript
Vue.js自定义指令的用法与实例解析
2017/01/18 Javascript
详解有关easyUI的拖动操作中droppable,draggable用法例子
2017/06/03 Javascript
深入研究jQuery图片懒加载 lazyload.js使用方法
2017/08/16 jQuery
vue实现打印功能的两种方法
2018/09/07 Javascript
JS中的算法与数据结构之队列(Queue)实例详解
2019/08/20 Javascript
vue使用微信扫一扫功能的实现代码
2020/04/11 Javascript
javascript中call,apply,bind的区别详解
2020/12/11 Javascript
python迭代器与生成器详解
2016/03/10 Python
python GUI实例学习
2017/11/21 Python
python实现事件驱动
2018/11/21 Python
Python %r和%s区别代码实例解析
2020/04/03 Python
如何安装并在pycharm使用selenium的方法
2020/04/30 Python
Python logging模块异步线程写日志实现过程解析
2020/06/30 Python
CSS3结构性伪类选择器九种写法
2012/04/18 HTML / CSS
爱尔兰橄榄球店:Irish Rugby Store
2019/12/05 全球购物
临床护士自荐信
2014/01/31 职场文书
学习自我鉴定
2014/02/01 职场文书
保护母亲河倡议书
2014/04/14 职场文书
部门年终奖分配方案
2014/05/07 职场文书
文秘专业应届生求职信
2014/05/26 职场文书
2014年安全员工作总结
2014/11/13 职场文书
工会经费申请报告
2015/05/15 职场文书
同乡会致辞
2015/07/30 职场文书
导游词之江西赣州
2019/10/15 职场文书
nginx结合openssl实现https的方法
2021/07/25 Servers
国产动画《万圣街》日语配音版制作决定!
2022/03/20 国漫