Spring学习笔记之依赖的注解(2)
来源:互联网 发布:三坐标编程 编辑:程序博客网 时间:2024/06/06 12:37
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。
0 0
- Spring学习笔记之依赖的注解(2)
- (学习笔记)spring入门之Bean的依赖注入
- Spring学习笔记之---注解的使用
- Javaweb学习笔记之Spring的依赖注入方式
- Spring学习笔记(10.Spring Bean装配之Bean的定义域及注解实现)
- 《Pro Spring》学习笔记之依赖解析
- 《Pro Spring》学习笔记之依赖检查
- spring注解基于Annotation的依赖注入配置笔记
- Spring学习笔记之通过注解配置Bean(2)
- Spring学习笔记之通过注解配置Bean(1)
- spring mvc学习笔记之注解
- Spring学习笔记之JDK注解知识
- spring依赖注入的注解
- 学习之Dragger2依赖注解
- Spring学习笔记(5)----依赖注入的简单实现
- Spring学习笔记(5)----依赖注入的简单实现
- spring依赖注入(2)-注解
- Spring框架(三)依赖注入之注解方式
- 第8周 项目3-顺序串算法
- 浅谈Activity中setContentView()
- 第七周项目三-负数把正数赶出队列
- 第七周项目1 建立顺序环形队列算法库
- 第六周项目~括号匹配
- Spring学习笔记之依赖的注解(2)
- 第七周项目5--排队看病模拟
- 第七周项目1 建立顺序环形队列算法库
- 第七周项目2建立链对算法库
- 第四周项目4 猴子选大王
- 第七周项目4--队列数组
- 第7周项目3负数把正数赶出队列
- 建立链串的算法库
- leetcode 107 Binary Tree Level Order Traversal II(难易度:Easy)