spring 注解简单使用

一、通用注解

1、项目结构:

2、新建Person类,注解@Component未指明id,则后期使用spring获取实例对象时使用默认id="person"方式获取或使用类方式获取

package hjp.spring.annotation.commen;import org.springframework.stereotype.Component;//@Component
@Component("personId")
public class Person {
private String name;
private Integer age;
public String getName() {return name;
}
public void setName(String name) {this.name = name;
}
public Integer getAge() {return age;
}
public void setAge(Integer age) {this.age = age;
}
@Override
public String toString() {return "Person [name=" + name + ", age=" + age + "]";
}
}
Person

3、新建beans.xml文件,相比之前配置多了

xmlns:context="http://www.springframework.org/schema/context"

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
不再使用bean节点配置,而是使用context:component-scan节点,属性base-package指明要扫描注解的包
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- <bean id="personId" class="hjp.spring.annotation.commen.Person"></bean> --><context:component-scan base-package="hjp.spring.annotation.commen"></context:component-scan>
</beans>

4、新建测试类

package hjp.spring.annotation.commen;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;public class TestApp {@Testpublic void demo1() {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("hjp/spring/annotation/commen/beans.xml");//注解未指定id时,默认id为person//Person person = applicationContext.getBean("person", Person.class);        //注解指定id,即@Component("personId")时        //Person person = applicationContext.getBean("personId",Person.class);//不管是否指定id,使用类方式获取实例都可以Person person = applicationContext.getBean(Person.class);System.out.println(person);}
}
测试类

 

二、衍生三层开发注解

@Controller 修饰Web层;@Service 修饰service层;@Repository 修饰dao层

依赖注入:方式1、普通数据 @Value

       方式2、引用数据 @Autowired,默认按照类型进行注入;如果想要按照名称进行注入,还需要使用注解@Qualifier("名称")

1、项目结构

2、新建UserDao类

package hjp.spring.annotation.web;import org.springframework.stereotype.Repository;//@Repository
@Repository("userDaoId")
public class UserDao {public void save() {System.out.println("add user");}
}
UserDao

3、新建UserService类

package hjp.spring.annotation.web;import javax.annotation.Resource;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;@Service
public class UserService {@Autowired//@Qualifier("userDaoId") //指向UserDao类注解@Repository("userDaoId")指定的Id//属性注入也可以使用注解@Resource//@Resourceprivate UserDao userDao;public void setUserDao(UserDao userDao) {this.userDao = userDao;}public void addUser() {userDao.save();}
}
UserService

4、新建UserAction类

package hjp.spring.annotation.web;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;@Controller("userActionId")
//@Scope("prototype")//多例
public class UserAction {@Autowiredprivate UserService userService;public void setUserService(UserService userService) {this.userService = userService;}public void execute() {userService.addUser();}
}
UserAction

5、新建测试类

package hjp.spring.annotation.web;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestApp {@Testpublic void demo1() {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("hjp/spring/annotation/web/beans.xml");UserAction userAction = applicationContext.getBean("userActionId", UserAction.class);System.out.println(userAction);//用于测试单例和多例
        userAction.execute();userAction=applicationContext.getBean("userActionId", UserAction.class);System.out.println(userAction);//用于测试单例和多例
    }
}
测试类

6、新建beans.xml配置文件,内容和通用注解一样,只是修改下扫描包的地址

<?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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- <bean id="personId" class="hjp.spring.annotation.commen.Person"></bean> --><!-- 让spring对含有注解的类进行扫描 --><context:component-scan base-package="hjp.spring.annotation.web"></context:component-scan>
</beans>
beans.xml

三、其他注解:如@PostConstruct修饰初始化;@PreDestroy修饰销毁

四、项目中对XML和注解的使用情况

1、纯XML,整合第三方(jar包)

2、纯注解,限制条件必须有源码,简化代码开发

3、xml+注解,xml配置bean,代码中使用注入注解

如果混合使用不需要扫描,只需要加入<context:annotation-config></context:annotation-config>配置

测试Demo可以将UserDao类的@Repository、UserService类的@Service、UserAction类的@Controller去掉,只保留注入注解,

然后修改beans.xml文件为

<?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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- 只使用注入的注解 --><bean id="userDaoId" class="hjp.spring.annotation.web.UserDao"></bean><bean id="userServiceId" class="hjp.spring.annotation.web.UserService"></bean><bean id="userActionId" class="hjp.spring.annotation.web.UserAction"></bean><context:annotation-config></context:annotation-config>
</beans>

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/287744.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

selenium+python笔记3

#!/usr/bin/env python # -*- coding: utf-8 -*- """ desc:学习unittest的用法 注意setUp/setUpClass&#xff0c;tearDown/tearDownClass的区别 ① setUp():每个测试函数运行前运行 ② tearDown():每个测试函数运行完后执行 ③ setUpClass():必须使用classmeth…

【学生选课系统经典】C#与SQLSERVER连接:ASP.NET网站(服务器端,IIS发布)

实验任务描述 1 用C#访问SQLSERVER数据库(两种安全模式); 2 用C#完成数据库指定表上的数据显示; 3 用C#完成数据库指定表上的数据插入、删除和更新; 4 用C#完成数据库用户验证。 此处使用ASP.NET工程来完成这个项目,和Windows应用不同的是:这个项目是在服务器上、依靠IIS服…

TCP包头、UDP包头、IP包头、和MAC帧包头详细字段和包头大小

1 TCP头 TCP是一种可靠的、面向连接的字节流服务,头部定义如下。 /*TCP头定义,共20个字节*/ typedef struct _TCP_HEADER {short m_sSourPort;       // 源端口号16bitshort m_sDestPort;       // 目的端口号16bitunsigned int m_uiSequNum; …

经典面试题:用户反映你开发的网站访问很慢可能会是什么原因

原文链接&#xff1a;http://blog.csdn.net/lv_victor/article/details/53148421 问题场景&#xff1a;某个用户向你反映说你开发的网站访问速度很慢&#xff0c;但是该用户访问其他问题很正常&#xff0c;分析下原因、有哪些工具分析原因、怎么解决问题&#xff1f; 最近面试两…

《假如编程是魔法之零基础看得懂的Python入门教程 》——(三)使用初始魔法跟编程魔法世界打个招呼吧

学习目标 完成显示魔法的使用——输出print完成传入魔法的使用——输入input使魔法生效——运行python文件 目录 第一篇&#xff1a;《假如编程是魔法之零基础看得懂的Python入门教程 》——&#xff08;一&#xff09;既然你选择了这系列教程那么我就要让你听得懂 第二篇&am…

查缺补漏系统学习 EF Core 6 (一)

推荐关注「码侠江湖」加星标&#xff0c;时刻不忘江湖事掌握 ORM 开发方式是每一个 .NET 开发者所必备的技能&#xff0c;而且 .NET 平台有很多优秀的 ORM 框架。很多人都会诟病 .NET 官方标配的 Entity Framework&#xff0c;感觉其笨重难用、性能低下。但其实经过多年发展&am…

mysql 5.5 mysqldump_mysql 5.5 mysqldump 原文翻译

根据mysql 5.5第6.4章节理解和自己翻译水平有限如有纰漏请指教,原文如下.6.4 使用mysqldump备份(Using mysqldump for Backups)首先多余的不用说了备份用来干什么大家都清楚。mysqldump备份分两种输出形式&#xff1a;1. 无--tab选项&#xff0c;输出标准的SQL格式。输出包含CR…

【经典回放】JavaScript学习详细干货笔记之(一)

【经典回放】JavaScript学习详细干货笔记之&#xff08;一&#xff09; 【经典回放】JavaScript学习详细干货笔记之&#xff08;二&#xff09; 【经典回放】JavaScript学习详细干货笔记之&#xff08;三&#xff09; 目录 一、为什么要学JavaScript 二、JavaScript经典案例 …

Java Attach API

catalog 1. instrucment与Attach API 2. BTrace: VM Attach的两种方式 3. Sun JVM Attach API 1. instrucment与Attach API JDK5中增加了一个包java.lang.instrucment&#xff0c;能够对JVM底层组件进行访问。在JDK 5中&#xff0c;Instrument 要求在运行前利用命令行参数或者系…

TCP之三次握手和四次挥手过程

1 TCP包头里面的标志位 下图为TCP头部里面部分信息,入下标志位,每个标志位占一位。 标志位这里会涉及3个,ACK SYN FIN ACK:确认序号有效。 SYN:发起一个新连接。 FIN:释放一个连接。 2 三次握手过程 第一次握手 Client将标志位SYN置1,随机产生一个值seq=J,并将数…

Handler 机制分析

android 子线程和UI线程的交互主要使用Handler的方法进行通信。本文分析Handler机制 Handler 如何使用&#xff1f; Handler的使用比较简单 public class MainActivity extends Activity{private Handler handler new Handler() { public void handleMessage(Message msg) { …

gearman mysql编译_gearman初探(一、编译和安装)

gearman是一个任务分发系统&#xff0c;将计算比较耗时的任务分发给不同的机器专门进行计算&#xff0c;而任务发起的初始程序不必等待这些任务完成就可以返回而继 续执行。笔者最开始做PHP邮件发送的时候&#xff0c;因为邮件发送耗时比较长&#xff0c;这时PHP脚本就会被阻塞…

《假如编程是魔法之零基础看得懂的Python入门教程 》——(四)了解魔法百宝箱列表、字典及基本数据类型

学习目标 了解魔法世界中可更改容器——变量了解魔法世界的基本数值类型——字符串、整数了解魔法百宝箱——字典、列表了解列表如何添加值了解字典如何添加值了解字典与列表定义的不同符号 目录 第一篇&#xff1a;《假如编程是魔法之零基础看得懂的Python入门教程 》——&…

TCP协议之如何保证传输的可靠性

一、问题 TCP协议之如何保证传输的可靠性?我们先看下TCP的头部图片和TCP头部的字段 /*TCP头定义,共20个字节*/ typedef struct _TCP_HEADER {short m_sSourPort;       // 源端口号16bitshort m_sDestPort;       // 目的端口号16bitunsigned int …

【工具篇】在Mac上开发.Net Core需要的工具

微信公众号&#xff1a;趣编程ACE关注可了解更多的.NET日常开发技巧,如需帮助&#xff0c;请后台留言&#xff1b;[如果觉得本公众号对您有帮助&#xff0c;欢迎关注]在Mac上开发.Net Core需要的工具如果您是一个.NET 开发者&#xff0c;想从Windows切换到Mac上开发.NET或者您已…

【Pix4d精品教程】Pix4Dmapper完整航测内业操作流程手把手图文教程

1. 作业流程图 2. 原始资料准备 原始资料包括影像数据、POS数据以及控制点数据。 确认原始数据的完整性,检查获取的影像中有没有质量不合格的相片。同时查看POS数据文件,主要检查航带变化处的相片号,防止POS数据中的相片号与影像数据相片号不对应,出现不对应情况应手动调…

关于构造函数和this调用的思考

文中一系列思考和内容引发自以下问题&#xff1a;我需要在一个类的构造函数中调用另一个对象的构造函数&#xff0c;并使用this初始化其中的一个引用成员。 主要遇到的问题&#xff1a; 1. 构造函数的初始化列表中能访问this吗&#xff1f; 很明显c创建一个对象分为两部分&…

mysql semi join_MySQL 5.6 Semi join优化之materialization strategy

8月 24, 2014 |Nix.Huang考虑如下查询&#xff1a;select * from Countrywhere Country.code IN (select City.Countryfrom Citywhere City.Population > 7*1000*1000)and Country.continentEurope这个子查询是非相关子查询&#xff0c;我们能和外层循环独立的执行它&#x…

【ArcGIS风暴】何为动态投影?这次全面为您揭开ArcGIS中动态投影的神秘面纱!

本课程配套蓝光视频: 【ArcGIS风暴】GIS动态投影问题 GISer们都见过以下警告,该警告的原因是当前加载的数据的坐标系和当前数据框坐标系不一致导致的,核心问题是地理坐标系不一致。如当前数据的坐标系是GCS_Xian_1980,而数据框的坐标系有可能是WGS_1984等,总之跟要加载的数…

《假如编程是魔法之零基础看得懂的Python入门教程 》——(五)我的魔法竟然有了一丝逻辑

学习目标 了解魔法世界中的结构表现——缩进了解魔法世界的逻辑判断——if了解魔法世界的多次逻辑判断——ifelse嵌套了解魔法世界中的逻辑运算——且 and 与或 or 推荐 1.《备受好评的看得懂的C语言入门教程》 目录 第一篇&#xff1a;《假如编程是魔法之零基础看得懂的P…