Spring学习笔记之依赖的注解(2)
Spring学习笔记之依赖的注解(2)
1.0 注解,不能单独存在,是Java中的一种类型
1.1 写注解
1.2 注解反射
2.0 spring的注解
spring的
@Controller@Component@Service//更多典型化注解,但是@Controller@Service建议使用
@service(“personService”)可以代替set get 方法,@Resource(name=personDao)
@Autowired//按照类型匹配
@Qualifier(“student”)两者结合相当于id匹配
相当于java的@Resource(name=”student”)
3.0 DI(依赖注入)的注解@Resource、@PostConstruct
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<bean id="person" class="come.demo.spring.di.annotation.Person">
</bean>
<bean id="student" class="come.demo.spring.di.annotation.Student"></bean>
<context:annotation-config></context:annotation-config>
</beans>
Person.java
public class Person {
/**
* @Autowired
@Qualifier("student")
==
@Resource(name="student")
*/
@Resource(name="student")
private Student student;
@PostConstruct //在构造器之后
public void init(){
System.out.println("init");
}
@PreDestroy //在spring容器销毁之前
public void destroy(){
}
public void say(){
this.student.say();
}
}
Student.java
public class Student {
public void say(){
System.out.println(“student”);
}
}
PersonTest.java
public class PersonTest {
/**
* 1、启动spring容器
* 2、把spring配置文件中的bean实例化(person,student)
* 3、当spring容器解析配置文件
* <context:annotation-config></context:annotation-config>
* spring容器会在纳入spring管理的bean的范围内查找哪些类的属性上是否加有@Resource注解
* 4、如果在属性上找到@Resource注解
* 如果@Resource的注解的name属性的值为""
* 则把@Resource所在的属性的名称和spring容器中的id作匹配
* 如果匹配成功,则赋值
* 如果匹配不成功,则会按照类型进行匹配
* 如果匹配成功,则赋值,匹配不成功,报错
* 如果@Resource的注解的name属性的值不为""
* 则解析@Resource注解name属性的值,把值和spring容器中的ID进行匹配
* 如果匹配成功,则赋值
* 如果匹配不成功,则报错
*
* 说明:
* 注解代码越来越简单,效率越来越低
* 注解只能应用于引用类型
*/
@Test
public void testDIAnnotation(){
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person)context.getBean("person");
person.say();
}
}
4.0 scan-类扫描的注解(减少配置代码,却降低了性能)
4.1 配置文件
<context:component-scan
base-package="com.demo.spring.scan.annotation"></context:component-scan
4.2 原理:
0.0 把一个类放到spring容器中,该类也称为component
1.0 启动spring容器
2.0 当spring容器解析配置文件时,会去base-package指定的包及子包中扫描所有的类。
3.0 把类放到spring容器中
看哪些类上面是否加有@Component注解
如果该类上面有@Component注解
检查该类的value属性是否为““
如果为”“,则会把该类注解所在的类的类名以这样的方式
@Component
public class Person{
}
==
<bean id = "person" class="..person"
如果该类的value不为空,则以这样的形式:
@Component("aa")
public class Person{}
==
<bean id="aa" class="..Person"
4.0 扫描spring容器中所有的bean,进行@Resource规制
5.0 小结
1、IOC把一个类放到容器里边,spring容器给他创建对象
1.1 spring创建对象的三种方式
1 默认构造函数 2.静态工程3.实例工程
默认情况下把一个类放到spring容器里面,默认是单例。
什么时候创建单例对象?
默认情况下spring容器启动时候创建单例对象。
但是把lazy-init="true"的话,是在contextgetbean时候才创建对象。
如果scope="property"即多例,在contextgetbean时候才创建对象。
初始化(init,只需要在配置文件中init-method,spring容器在调用完构造器立即自动调用)
销毁(destroy,在spring容器销毁时候才内部调用)
2、DI给属性赋值
2.1赋值方法
利用set方法可以给属性赋值还可以利用构造器,也可以用注解。进行注入。
基本属性、引用类型、集合进行装配。
@Resource用于给一个属性进行注入。
类扫描<context:component-scan减少配置文件代码。
6.0 spring注解的继承
xml的继承需要,parent:spring容器中的继承
而注解的继承配置文件不需要parent。
Spring学习笔记之依赖的注解(2)的更多相关文章
- Spring学习笔记(14)——注解零配置
我们在以前学习 Spring 的时候,其所有的配置信息都写在 applicationContext.xml 里,大致示例如下: java代码: <beans> <bean n ...
- Spring学习笔记1—依赖注入(构造器注入、set注入和注解注入)
什么是依赖注入 在以前的java开发中,某个类中需要依赖其它类的方法时,通常是new一个依赖类再调用类实例的方法,这种方法耦合度太高并且不容易测试,spring提出了依赖注入的思想,即依赖类不由程序员 ...
- Spring 学习笔记(八)—— 注解使用整合
@Autowired —— 自动装配 需先在配置文件中,配置一个org.springframework.beans.factory.annotation. AutowiredAnnotationBe ...
- spring学习笔记 星球日two - 注解方式配置bean
注解要放在要注解的对象的上方 @Autowired private Category category; <?xml version="1.0" encoding=" ...
- 不错的Spring学习笔记(转)
Spring学习笔记(1)----简单的实例 --------------------------------- 首先需要准备Spring包,可从官方网站上下载. 下载解压后,必须的两个包是s ...
- SpringMVC:学习笔记(11)——依赖注入与@Autowired
SpringMVC:学习笔记(11)——依赖注入与@Autowired 使用@Autowired 从Spring2.5开始,它引入了一种全新的依赖注入方式,即通过@Autowired注解.这个注解允许 ...
- Spring学习笔记(一)
Spring学习笔记(一) 这是一个沉淀的过程,大概第一次接触Spring是在去年的这个时候,当初在实训,初次接触Java web,直接学习SSM框架(当是Servlet都没有学),于是,养成了一个很 ...
- 【Spring学习笔记-MVC-3.1】SpringMVC返回Json数据-方式1-扩展
<Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...
- spring学习笔记(一) Spring概述
博主Spring学习笔记整理大部分内容来自Spring实战(第四版)这本书. 强烈建议新手购入或者需要电子书的留言. 在学习Spring之前,我们要了解这么几个问题:什么是Spring?Spring ...
随机推荐
- https://blog.csdn.net/cddcj/article/details/52193932
https://blog.csdn.net/cddcj/article/details/52193932 兼容性
- 不能访问windows installer 服务,可能你在安全模式下运行 windows ,或者windows installer
windows installer服务解决方案 很多朋友在安装MSI格式的文件包时,经常会遇到windows installer出错的情况,有如下几种现象: 1.所有使用windows install ...
- Js正则匹配处理时间
<html> <body> <script type="text/javascript"> //将long 型 转换为 日期格式 年-月-日 h ...
- 【转载】java的常见类型转换
//Int型数字转换成String int num1=123456; //方法1 String str1=num1+""; System.out.println(str1); // ...
- Oracle开发常用函数 max 最大数 自动加 1
max 最大数 自动加 1 create or replace function fun_getmaxlot( vend in varchar2 , domain IN VARCHAR2, tag i ...
- 学习EXTJS6(1)安装环境
1.官方下载地址: extjs6 GPL版:https://www.sencha.com/legal/gpl/ sencha cmd:https://www.sencha.com/products/e ...
- foj 2173 floyd+矩阵快速幂
Problem 2173 Nostop Accept: 52 Submit: 210 Time Limit: 3000 mSec Memory Limit : 32768 KB Pro ...
- [bzoj2002][Hnoi2010]Bounce弹飞绵羊_LCT
Bounce弹飞绵羊 bzoj-2002 Hnoi-2010 题目大意:n个格子,每一个格子有一个弹簧,第i个格子会将经过的绵羊往后弹k[i]个,达到i+k[i].如果i+k[i]不存在,就表示这只绵 ...
- [bzoj1090][SCOI2003]字符串折叠_区间dp
字符串折叠 bzoj-1090 SCOI-2003 题目大意:我说不明白...链接 注释:自己看 想法:动态规划 状态:dp[i][j]表示从第i个字符到第j个字符折叠后的最短长度. 转移:dp[l] ...
- 使用excel进行数据挖掘(2)----分析关键影响因素
使用excel进行数据挖掘(2)----分析关键影响因素 在配置环境后,能够使用excel进行数据挖掘. 环境配置问题可參阅: http://blog.csdn.net/xinxing__8185/a ...