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 相关文章推荐
分析Netty直接内存原理及应用
Jun 14 Java/Android
Java 数组内置函数toArray详解
Jun 28 Java/Android
SpringBoot整合RabbitMQ的5种模式实战
Aug 02 Java/Android
Java比较两个对象中全部属性值是否相等的方法
Aug 07 Java/Android
关于EntityWrapper的in用法
Mar 22 Java/Android
Java中的继承、多态以及封装
Apr 11 Java/Android
Spring Boot 使用 Spring-Retry 进行重试框架
Apr 24 Java/Android
Android使用EventBus发送消息,Fragment中接收消息的方法会执行多次
Apr 24 Java/Android
Java Spring Lifecycle的使用
May 06 Java/Android
一文搞懂Java中的注解和反射
Jun 21 Java/Android
Java实现HTML转为Word的示例代码
Jun 28 Java/Android
SpringBoot项目部署到阿里云服务器的实现步骤
Jun 28 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
Session保存到数据库的php类分享
2011/10/24 PHP
php+iframe实现隐藏无刷新上传文件
2012/02/10 PHP
PHP中让json_encode不自动转义斜杠“/”的方法
2017/02/28 PHP
总结PHP代码规范、流程规范、git规范
2018/06/18 PHP
JS效率个人经验谈(8-15更新),加入range技巧
2007/01/09 Javascript
解析javascript 实用函数的使用详解
2013/05/10 Javascript
原生js实现日期联动
2015/01/12 Javascript
基于jQuery实现Ajax验证用户名是否存在实例
2016/03/30 Javascript
JavaScript 不支持 indexof 该如何解决
2016/03/30 Javascript
JS继承之借用构造函数继承和组合继承
2016/09/07 Javascript
轻松理解Javascript变量的相关问题
2017/01/20 Javascript
JS中原始值和引用值的储存方式示例详解
2018/03/23 Javascript
ES6中的迭代器、Generator函数及Generator函数的异步操作方法
2019/05/12 Javascript
vue实现固定位置显示功能
2019/05/30 Javascript
vue组件创建的三种方式小结
2020/02/03 Javascript
JS获取表格视图所选行号的ids过程解析
2020/02/21 Javascript
JavaScript DOM常用操作代码汇总
2020/07/03 Javascript
python pickle 和 shelve模块的用法
2013/09/16 Python
python实现转圈打印矩阵
2019/03/02 Python
Python单元和文档测试实例详解
2019/04/11 Python
Python3安装psycopy2以及遇到问题解决方法
2019/07/03 Python
python3.7通过thrift操作hbase的示例代码
2020/01/14 Python
详解Django配置JWT认证方式
2020/05/09 Python
浅谈Python3中print函数的换行
2020/08/05 Python
使用Python实现NBA球员数据查询小程序功能
2020/11/09 Python
EMPHASIS艾斐诗官网:周生生旗下原创精品珠宝品牌
2020/12/17 全球购物
什么是Deployment descriptors;都有什么类型的部署描述符
2015/07/28 面试题
打架检讨书2000字
2014/02/22 职场文书
施工安全责任书
2014/04/14 职场文书
赡养老人协议书
2014/04/21 职场文书
成立公司计划书
2014/05/07 职场文书
给校长的一封检讨书
2014/09/20 职场文书
群众路线专项整治工作情况报告
2014/10/28 职场文书
女儿满月酒致辞
2015/07/29 职场文书
国际贸易实训总结
2015/08/03 职场文书
事业单位工作人员岗前培训心得体会
2016/01/08 职场文书