Spring学习(七)
注解产生原因
1、传统spring的做法:使用xml来对bean进行注入和或者是配置aop、事物配置文件体积庞大,造成了配置文件的可读性和可维护性很低Java文件和xml不断切换,造成思维不连贯开发效率降低
2、引入注解的做法:通过@xxx让注解与Java Bean紧密结合既大大减少了配置文件的体积,又增加了Java Bean的可读性与内聚性。
构造IOC容器的两种方式
1、使用配置文件
2、使用注解
用注解来向Spring容器注册Bean。需要在配置文件中注册<context:component-scan base-package=”pagkage1[,pagkage2,…,pagkageN]”/>,默认启用注解的支持
,<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:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!-- <context:annotation-config /> -->
<!-- 扫描这个包的所有组件 -->
<context:component-scan base-package="ecut.aop.annotation" /> <!-- 为 注解 提供 自动产生 代理对象的 支持 -->
<aop:aspectj-autoproxy proxy-target-class="true" /> </beans>
base-package所指定的包及其子包会被扫描(多个包可用逗号分隔),只要类中带有特殊的注解将会被自动注入
常用的注解
1、标注组件的
- @org.springframework.stereotype.Controller : 专门用来 标注 控制层的组件
package ecut.aop.annotation; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller; /**
* 控制层 ( Controller ) : 对 表示层 的用户操作 做出响应
* 实现技术可以是: Servlet 、Struts 、Spring MVC
* @Controller("sc") value ="sc" 相当于bean的名称是sc,默认名称是 studentController
*/
@Controller
public class StudentController { @Autowired
@Qualifier( "studentService" )
private StudentService studentService ; public String regist( Student s ){
System.out.println( "StudentController # regist ( Student ) " );
studentService.save( s );
return "success" ;
} public String logout() { throw new RuntimeException( "抛出异常" ); } public StudentService getStudentService() {
return studentService;
} public void setStudentService(StudentService studentService) {
this.studentService = studentService;
} }扫描到注解@Controller相当于在配置文件中<bean id="studentController" class="ecut.aop.annotation.StudentController" />增加这段配置。bean的id属性即名称若在注解中没有指定则默认是类名首字母小写后的名称例如StudentController类所默认的名称是studentController,若通过注解中的value指定了名称,则与此保持一致。
- @org.springframework.stereotype.Service : 专门用来标注 业务逻辑层 的组件
package ecut.aop.annotation; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; /**
* 业务逻辑层 ( Service ) 具体的业务逻辑
*/
@Service
public class StudentService { @Autowired
private StudentDao studentDao ; public void save( Student s ) {
System.out.println( "StudentService # save ( Student ) " );
studentDao.persist( s );
} public StudentDao getStudentDao() {
return studentDao;
} public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
} }@Service相当于在配置文件中增加<bean id="studentService" class="ecut.aop.annotation.StudentService" />
- @org.springframework.stereotype.Repository : 专门用来 标注 数据访问层 的 组件
package ecut.aop.annotation; import org.springframework.stereotype.Repository; /**
* 数据访问层 ( Repository ) 专门负责访问数据库
* DAO :Data Access Object , 数据访问对象
* 对象封装数据
*/
@Repository
public class StudentDao { public void persist( Student s ) {
System.out.println( "StudentDao 的 persist 方法提示你: 保存 "+ s.getName() );
} }@Repository相当于在配置文件中增加<bean id="studentDao" class="io.spring.aop.annotation.StudentDao" />
- @org.springframework.stereotype.Component : 可以标注任意的组件
package ecut.aop.annotation; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; @Component
@Aspect
public class StudentAspect {
.......
}当不能确定到底是 Controller 、Service 、Repository 中的那种类型时,适用。
2、标注自动装备的
- @org.springframework.beans.factory.annotation.Autowired : 默认按照类型对某个属性进行装配
package ecut.aop.annotation; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; /**
* 业务逻辑层 ( Service ) 具体的业务逻辑
*/
@Service
public class StudentService { @Autowired
private StudentDao studentDao ;
..............................
}相当于 <bean id="studentService" class="ecut.aop.annotation.StudentService" autowire="byType"/>,当Spring发现@Autowired注解时,将自动在代码上下文中找到和其匹配(默认是类型匹配)的Bean,并自动注入到相应的地方去。
- @org.springframework.beans.factory.annotation.Qualifier 与 Autowired 连用一起实现 按照名称进行自动装配
package ecut.aop.annotation; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller; /**
* 控制层 ( Controller ) : 对 表示层 的用户操作 做出响应
* 实现技术可以是: Servlet 、Struts 、Spring MVC
* @Controller("sc") value ="sc" 相当于bean的名称是sc,默认名称是 studentController
*/
@Controller
public class StudentController { @Autowired
@Qualifier( "studentService" )
private StudentService studentService ;
........
}相当于 <bean id="studentController" class="ecut.aop.annotation.StudentController" autowire="byName"/>或者 <bean id="studentController" class="ecut.aop.annotation.StudentController" p:studentService-ref="studentService"/>
@javax.annotation.Resource:@Resource后面没有任何内容,默认通过name属性去匹配bean,找不到再按type去匹配指定了name或者type则根据指定的类型去匹配bean指定了name和type则根据指定的name和type去匹配bean,任何一个不匹配都将报错
3、@Resource和@Autowired注解的区别
- 默认匹配的方式不同:@Autowired默认按照byType方式进行bean匹配,@Resource默认按照byName方式进行bean匹配。
- 所属架构不同:@Autowired是Spring的注解,@Resource是J2EE的注解(Spring属于第三方的,J2EE是Java自己的东西,因此,建议使用@Resource注解,以减少代码和Spring之间的耦合)
转载请于明显处标明出处:
https://www.cnblogs.com/AmyZheng/p/9277957.html
Spring学习(七)的更多相关文章
- Spring学习七----------Bean的配置之自动装配
© 版权声明:本文为博主原创文章,转载请注明出处 Bean的自动装配(Autowiring) no:不启用自动装配,此时需要手动注入.参考:Spring学习三----------注入方式 defaul ...
- spring学习七 spring和dynamic project进行整合
spring和web项目进行整合,其实就是在项目启动时,就创建spring容器,然后在servlet中使用spring容器进行开. 注意:为了页面可以访问到servlet,因此servlet必须放进t ...
- Spring学习(七)-----Spring Bean的5种作用域
在Spring中,bean作用域用于确定哪种类型的 bean 实例应该从Spring容器中返回给调用者.bean支持的5种范围域: 单例(singleton) - 每个Spring IoC 容器返回一 ...
- Spring学习(七)--Spring MVC的高级技术
一.Spring MVC配置的替代方案 我们已经了解如何通过AbstractAnnotationConfigDispatcherServlet- Initializer快速搭建了Spring MVC环 ...
- Spring学习七:ComponentScan注解
今天主要从以下几个方面来介绍一下@ComponentScan注解: @ComponentScan注解是什么 @ComponentScan注解的详细使用 1.ComponentScan注解是什么 其实很 ...
- spring学习七
一: web.xml中常用配置元素? <servlet></servlet>: 在向servlet或JSP页面制定初始化参数或定制URL时,首先命名servlet或JSP页面. ...
- MyBatis学习七:spring和MyBatis整合
<\mybatis\day02\16mybatis和spring整合-sqlSessionFactory配置.avi;> MyBatis学习七:spring和MyBatis整合.逆向工程 ...
- Spring学习笔记(七)模拟实际开发过程的调用过程XML版-Setter方式注入
模拟实际开发过程的调用过程XML版-Setter方式注入 源码获取github [TOC] 1.项目结构 2.jar包跟上个一样 3.重写set方法 UserServiceImpl.java 1234 ...
- spring学习笔记(七)HttpMessageConverter
spring学习笔记(七)HttpMessageConverter 1. HttpMessageConverter的加载 2. 从StringMessageConverter探究消息转换器的原理 1. ...
随机推荐
- LTE引理——解决数论竞赛题的利器
LTE (Lifting The Exponent Lemma)引理是一个解指数型不定方程的强力工具.它在Olympiad folklore非常知名,虽然它的起源已经无从查找了.它和Hensel’s ...
- 文件上传plupload组件使用
这段时间一直在使用文件上传,简要的介绍一下文件上传的组件使用,先上一段代码. var uploader = new plupload.Uploader( { //用来指定上传方式,指定多个上传方式请使 ...
- 如何在Go中获得 "A1","B2" 类似字符+数字的字符串
package main import ( "fmt" ) func main() { // 字符串 str := "ABCDEFGHIJKLMNOPQRSTUVWXYZ ...
- 【C语言】用C语言输出“心形”图案
在你们的世界里,是不是觉得程序猿一点浪漫都不懂?其实不是的,程序猿的世界也是很浪漫滴! 傻瓜版 int main() { printf("❤"); ; } 高级版 //版本一:单个 ...
- 【C语言】利用递归函数求n的阶乘
递归实现n的阶乘 什么是阶乘:0!= 1,n!=n * (n - 1) * (n - 2)......3 * 2 * 1: 解题思路: 1> 分析题意,很明显0是递归出口: ...
- 苹果公司以注重客户隐私闻名世界,但为什么Siri泄露了我的秘密?
编辑 | 于斌 出品 | 于见(mpyujian) 苹果的Siri因为其作为智能语音助手,方便人们打电话.发信息等功能,被人们所喜爱,但是最近,Siri好像有一些问题,让我们怀疑这位"小伙伴 ...
- input file弹出框选择文件后缀限制
在页面选择文件时的弹出框默认显示的是所有类型的文件,有时候文件太多不好选择,我们就要过滤掉不想展示的文件,这是需要用到input的accept属性,只有在type="file"才有 ...
- 第十八篇 Linux环境下常用软件安装和使用指南
提醒:如果之后要安装virtualenvwrapper的话,可以直接跳到安装virtualenvwrapper的方法,而不需要先安装好virtualenv 安装virtualenv和生 ...
- Mobius反演定理-BZOJ2154
This article is made by Jason-Cow.Welcome to reprint.But please post the article's address. 莫比乌斯定理(未 ...
- opencv:图像去噪(椒盐噪声)
#include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...