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. ...
随机推荐
- python call perl
import subprocess subprocess.call("csv_gen.pl -by overall", shell=True)
- Flink流处理(二)- 流处理基本概念
1. Dataflow Programming 在讨论流处理的基本概念之前,我们首先介绍一下数据流编程(dataflow programming)的基本概念与术语. 数据流图 数据流程序一般在由数据流 ...
- response下载csv文件内容乱码问题
response下载csv文件内容乱码问题 解决办法:在输出流语句第一行输出 out.write(new byte[]{(byte)0xEF, (byte)0xBB, (byte)0xBF}); Se ...
- 开源镜像站,vmware下载
vmware下载:https://www.newasp.net/soft/345086.html 官网下载链接:https://www.centos.org/download/ http://mirr ...
- Cheapest Palindrome
这个区间dp解的话是先知道小区间再推大区间,具体需要分类讨论当小区间已经是回文串了,下一层判断,所以一层一个呢还是一层两个呢, 下面讨论一层一个的话是什么情况,那么如果一层两个,可以在评论区写下代码供 ...
- map-apply-applymap
In [1]: import warnings import math import pandas as pd import numpy as np import matplotlib warning ...
- Error: Invalid CSS after "xxx": expected 1 selector or at-rule, was "{}"
在VSCode中编译sass文件时,出现报错:Error: Invalid CSS after "xxx": expected 1 selector or at-rule, was ...
- FreeRTOS学习笔记3:内核控制及开启调度器
内核控制函数API 应用层中不会用到taskYIELD() //任务切换.会自动切换当前就绪表里优先级最高的任务 临界区 //不能被打断的代码段任务中进入临界区任务中退出临界区中断服务进入临界区中断服 ...
- cef源码分析之cefsimple
下面是cefsimple的入口代码,主要分成两个部分 // Entry point function for all processes. int APIENTRY wWinMain(HINSTANC ...
- imread函数+cvtColor()函数
加载图像(用cv::imread) imread功能是加载图像文件成为一个Mat对象,其中第一个参数表示图像文件名称 第二个参数,表示加载的图像是什么类型,支持常见的三个参数值 IMREAD_UNCH ...