spring注解(Component、依赖注入、生命周期、作用域)
1、注解
注解就是一个类,使用@加上注解名称,开发中可以使用注解取代配置文件
2、@Component 取代<bean class="">,@Component 取代<bean id="" class="">
(1)创建一个类(该类与dao层无联系,是一个单独的类)
@Component("studentService")
public class StudentServiceImpl implements StudentService {
public void addStudent(){
System.out.println("StudentService的实现类的Add方法!!");
}
}
(2)创建配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="pers.zhb.service"></context:component-scan>
</beans>
(3)测试类:
public class TestCycle {
public static void main(String[] args) {
ApplicationContext applicationContext=new
ClassPathXmlApplicationContext("applicationContext.xml");//创建容器对象
StudentService studentService= (StudentService) applicationContext.getBean("studentService");
studentService.addStudent();
}
}
(4)web开发中,提供了3个@Component注解的衍生注解(功能一样),它们是spring框架为我们提供的三层使用的注解,使得我们的三层对象更加清晰
@Repository:dao层(持久层)
@Service:service层(业务层)
@Controller:web层(表现层)
(5)@Component注解
作用:用于把当前类的对象存入到spring容器中
属性:
value:指定bean的id,不写的时候默认是当前的类名,且首字母小写。当指定属性的值的时候就要用该值
3、依赖注入
(1)创建一个Action:
@Controller("studentAction")
public class StudentAction {
@Autowired//默认按照类型注入
private StudentService studentService;
public void execute(){
studentService.addStudent();
}
}
使用@Autowired注解(依赖注入)的时候可以写在属性或set方法处,写在属性处可以节省代码量,优先按照bean的类型去找。
(2)service层:
public interface StudentService {
public void addStudent();
}
@Service
public class StudentServiceImpl implements StudentService {
private StudentDao studentDao; @Qualifier("studentDao")
public StudentDao getStudentDao() {
return studentDao;
}
@Autowired
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
} public void addStudent(){
studentDao.addStudent();
}
}
(3)dao层:
public interface StudentDao {
public void addStudent();
}
@Repository("studentDao")
public class StudentDaoImpl implements StudentDao {
@Override
public void addStudent() {
System.out.println("StudentDao的实现类的Add方法!!");
}
}
(4)配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--扫描-->
<context:component-scan base-package="pers.zhb"></context:component-scan>
</beans>
(5)测试:
public class ActionTest {
public static void main(String[] args) {
ApplicationContext applicationContext=new
ClassPathXmlApplicationContext("applicationContext.xml");//创建容器对象
StudentAction studentAction= (StudentAction) applicationContext.getBean("studentAction");
studentAction.execute();
}
}
StudentDao的实现类的Add方法!!
@Autowired
- 可以省略类内的get和set方法以及配置文件中bean的依赖注入的内容,但是在spring容器中还是需要配置bean的,可以结合生成bean的注解使用
- 自动按照类型注入,只要容器中有唯一一个bean对象类型和要注入的变量类型匹配,就可以注入成功
- 出现的位置可以是变量上也可以是方法上
- 按照类型注入的时候,如果有多个可以注入,不能注入成功的时候,可以按照名称注入(需要手动修改注入的变量名称),例如,一个接口有多个实现类,那么这些实现类就是相同类型的,注入的时候会出现问题,因为注入的时候匹配到了多个bean
@Qualifier
属性value用于指定注入的bean的id
在给类成员注入的时候不能独立使用,也就是说要和Autowired注解配合使用。当有多个相同类 型 的 bean 却只 有 一个需 要 自 动 装 配 时 , 将 @Qualifier 注 解 和@Autowire 注解结合使用以消除这种混淆, 指定需要装配的确切的 bean。
@Resource
直接按照bean的id注入,可以独立使用
属性可以指定bean的id
以上三种注解都是能用于注入bean类型的数据,而基本类型和String类型无法使用上述注解
@Value:用于注入基本类型和String类型,属性用于指定值
@Required :如果你在某个java类的某个set方法上使用了该注释,那么该set方法对应的属性在xml配置文件中必须被设置,否则就会抛出BeanInitializationException。
4、生命周期
初始化:@PostConstruct
销毁:@PreDestory
分别添加到初始化和销毁方法之前。
5、bean的作用域
(1)单例对象singleton
测试:
Student student1 =(Student)applicationContext.getBean("student");
Student student2 =(Student)applicationContext.getBean("student");
System.out.println(student1==student2);
返回结果为true,说明创建的是同一个对象。在spring容器中只存在一个bean的实例,bean以单里的形式存在
(2)多例对象prototype
测试:
Student student1 =(Student)applicationContext.getBean("student");
Student student2 =(Student)applicationContext.getBean("student");
System.out.println(student1==student2);
返回的结果为false,创建的是两个不同的对象。每次调用getBean()的时候都会返回一个新的实例
(3)request
request: 每次 http 请求都会创建一个 bean, 该作用域仅在基于 web 的 Spring ApplicationContext 情形下有效
(4)session
session: 在一个 HTTP Session 中, 一个 bean 定义对应一个实例。 该作用域仅在基于 web 的 Spring ApplicationContext 情形下有效。
(5)global-session
global-session: 在一个全局的 HTTP Session 中, 一个 bean 定义对应一个实例。 该作用域仅在基于 web 的 Spring ApplicationContext 情形下有效。
缺省的 Spring bean 的作用域是 Singleton。
spring注解(Component、依赖注入、生命周期、作用域)的更多相关文章
- Spring注解驱动开发(二)-----生命周期、属性赋值
bean的生命周期 bean的生命周期:bean创建---初始化----销毁的过程容器管理bean的生命周期:我们可以自定义初始化和销毁方法:容器在bean进行到当前生命周期的时候来调用我们自定义的初 ...
- 07 Spring框架 依赖注入(四)基于注解的依赖注入
前面几节我们都在使用xml进行依赖的注入,但是在实际的开发中我们往往偏爱于使用注解进行依赖注入,因为这样更符合我们人的思维,并且更加快捷,本节就来讲述Spring基于注解的依赖注入: 信息注入注解 @ ...
- Spring:基于注解的依赖注入的使用
1.什么是pojo?什么是bean? 首先,在之前几篇Spring的介绍文章当中,自己都提到了一个名词叫做POJO类,但是在回顾Spring的注解的使用的时候,去形容java当中的对象还有一个名词是叫 ...
- Spring Boot2(007):关于Spring beans、依赖注入 和 @SpringBootApplication 注解
一.关于Spring beans 和 依赖注入(Dependency Injection) spring boot 和 Spring 全家桶无缝衔接,开发过程中可以很轻松地使用 Spring 全家桶的 ...
- spring 四种依赖注入方式以及注解注入方式
平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程 ...
- Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->Spring Framework的依赖注入和控制反转
Dependency Injection and Inversion of Control 1.概述: 1.1相关概念 bean:由IoC容器所管理的对象,也即各个类实例化所得对象都叫做bean 控制 ...
- Spring-Context的注解实现依赖注入功能
使用Spring-Context的注解实现依赖注入功能. Demo要点: 本例子中主要使用Annotation功能来实现对MoviceService的注入.我们将Cinema.java的头部标注为@C ...
- spring六种种依赖注入方式
平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程 ...
- JavaEE开发之Spring中的依赖注入与AOP
上篇博客我们系统的聊了<JavaEE开发之基于Eclipse的环境搭建以及Maven Web App的创建>,并在之前的博客中我们聊了依赖注入的相关东西,并且使用Objective-C的R ...
- JavaEE开发之Spring中的依赖注入与AOP编程
上篇博客我们系统的聊了<JavaEE开发之基于Eclipse的环境搭建以及Maven Web App的创建>,并在之前的博客中我们聊了依赖注入的相关东西,并且使用Objective-C的R ...
随机推荐
- Windows10 上的国产锁屏广告?
不知从什么时候开始,我的笔记本(Windows 10 Home,联想X1)在开机.锁屏时都会显示一些国产的“公益广告”(可惜不能截屏),有时是关于时令节气,有时是一些鸡汤短句,有时节假日则是叫我爱党爱 ...
- Linux下如何知道是否有人在使坏?
在 Linux 下查看用户的行为,不仅仅是网管要做的事,也是开发人员所应该具备的基本技能之一.为什么呢?因为有时其他同事在做一些很消耗资源的事情,比如在编译大型程序,可能会导致服务器变得很慢,从而影响 ...
- 时间转换c#
简要 本篇文章主要介绍基于.net(c#)封装的时间帮助类.主要功能是将时间戳与DateTime类型相互转换. 1.其中包括将毫秒级时间戳(13位).秒级时间戳(10位)转换为DateTime. 2. ...
- 力扣Leetcode 33. 搜索旋转排序数组
33. 搜索旋转排序数组 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). 搜索一个给定的目标值, ...
- 总结java中文件拷贝剪切的5种方式-JAVA IO基础总结第五篇
本文是Java IO总结系列篇的第5篇,前篇的访问地址如下: 总结java中创建并写文件的5种方式-JAVA IO基础总结第一篇 总结java从文件中读取数据的6种方法-JAVA IO基础总结第二篇 ...
- 洛谷 P3177 [HAOI2015]树上染色 树形DP
洛谷 P3177 [HAOI2015]树上染色 树形DP 题目描述 有一棵点数为 \(n\) 的树,树边有边权.给你一个在 \(0 \sim n\)之内的正整数 \(k\) ,你要在这棵树中选择 \( ...
- Unity游戏资源反解工具
https://github.com/ata4/disunity http://devxdevelopment.com/UnityUnpacker 链接:https://pan.baidu.com/s ...
- [BUUOJ记录] [BJDCTF 2nd]文件探测
感觉算是这次比赛里面综合性很强的一道题了,主要考察SSRF.PHP伪协议包含.挖掘逻辑漏洞和一个小tirck.委屈的是第一天晚上就做到了最后一步,想到了SESSION置空即可绕过,但是最后读Flag姿 ...
- 关于Vue的那些事儿
Vue 渐进式框架 众前端周知,Vue是一套用于构建用户界面的渐进式框架,自底向上逐层应用,关注视图层.那我们就来说道说道: 渐进式:声明式渲染->组件系统->客户端路由(router)- ...
- 下载安装gradle
1.登录官网:www.gradle.org,进入到下图的界面: 2.点击Install Gradle,跳转到下一个界面后: 3.下载 4.下载成功后,解压到任意位置,将路径添加到path路径下,选择我 ...