news 2026/9/2 14:52:19

Spring使用注解开发

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Spring使用注解开发

Spring使用注解开发

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

bean如何注解

约束配置

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="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-scanbase-package="com.cike6"/><context:annotation-config/></beans>

要注解的类

// 等价于 <bean id="user" class="com.cike6.dao.User"/>// @Component 组件@ComponentpublicclassUser{publicStringname="cike_y";}

测试方法

publicclassspring_6_Test{@Testpublicvoidtest(){ApplicationContextcontext=newClassPathXmlApplicationContext("applicationContext.xml");Useruser=context.getBean("user",User.class);System.out.println(user.name);}}

属性如何注入

在对应属性上面加一个@Value 注解即可

@ComponentpublicclassUser{// 相当于 <property name="name" value="cike_y"/>@Value("cike_y")publicStringname;}

衍生的注解

@Component 有几个衍生注解,我们在web开发中,会按照mvc三层架构分层!都是组件的意思,只是名字不一样为了划分,功能一样

dao【@Repository】

- 一般dao层都会用这个注解

service【@Service】

- service层用这个注解

controller【@Controller】

- 控制层用这个注解
controller的案例
  • 如果找不到controller的容器id,我们还可以指定注解显示定义一个容器id名

使用bean注解的类,并且属性注入的注解方式

@Controller("usercontroller")publicclassUserController{@Value("aa")publicStringname;}

注解约束的xml配置

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="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-scanbase-package="com.cike6"/><context:annotation-config/></beans>

测试方法并且实例化容器id

@Testpublicvoidtest2(){ApplicationContextcontext=newClassPathXmlApplicationContext("applicationContext.xml");UserControllerusercontroller=context.getBean("usercontroller",UserController.class);System.out.println(usercontroller.name);}

可以看见成功输出 UserController类下的属性值name

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

自动装配

@Autowired:自动装配通过类型。名字 如果Autowired不能唯一自动装配上属性,则需要通过@Qualifier(value="xx") @Nullable 字段标记了这个注解,说明这个字段可以null @Resource 自动装配通过名字、类型

作用域

@Component@Scope("singleton")// 单例模式// 原型模式 @Scope("prototype")publicclassUser{// 相当于 <property name="name" value="cike_y"/>@Value("cike_y")publicStringname;}

小结

xml与注解:

  • xml更加万能,适用于任何场景!维护简单方便
  • 注解不是自己的类使用不了,维护相对复杂

xml与注解的最佳实践:

  • xml用来管理bean;
  • 注解之复杂完成属性的注入
  • 我们在使用的过程中,只需要注意一个问题:必须要注解生效就需要开启注解的支持、扫描包
<context:component-scanbase-package="com.cike6"/><context:annotation-config/>
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/9/2 22:10:18

ROS2十年演进(2015–2025)

ROS2十年演进&#xff08;2015–2025&#xff09; 一句话总论&#xff1a; ROS2从2015年概念提出、2017年Alpha版起步&#xff0c;本想解决ROS1实时性差、分布式弱、安全性无的问题&#xff0c;却在2025年彻底凉凉——全球新机器人项目份额<1%&#xff0c;在中国市场<0.5…

作者头像 李华
网站建设 2026/9/2 9:46:13

末端执行器十年演进(2015–2025)

末端执行器十年演进&#xff08;2015–2025&#xff09; 一句话总论&#xff1a; 2015年末端执行器还是“刚性二指/三指夹爪气动/伺服驱动固定抓取模式”的工业工具&#xff0c;2025年已进化成“多指灵巧手全电驱力控<0.1N端到端VLA大模型意图直驱自适应变形/吸盘融合量子鲁…

作者头像 李华
网站建设 2026/9/2 11:28:13

springboot基于Java的旅游攻略分享平台设计实现

背景分析随着旅游业的快速发展&#xff0c;个性化旅游需求日益增长&#xff0c;传统攻略平台存在信息分散、互动性差等问题。基于SpringBoot的旅游攻略分享平台整合用户生成内容&#xff08;UGC&#xff09;&#xff0c;通过技术手段解决信息碎片化&#xff0c;提升用户体验。技…

作者头像 李华
网站建设 2026/9/2 10:11:18

学术论文降重技巧盘点:6种AI辅助方法全解析

写论文查重技巧排名&#xff1a;6大AI方法合集推荐 排名 工具/方法 核心优势 适用场景 1 aibiye 智能降重学术语言优化 初稿完成后深度润色 2 aicheck 多维度查重选题辅助 全程论文质量监控 3 秒篇 一键生成逻辑结构优化 紧急补论文初稿 4 AskPaper 文献解析…

作者头像 李华