Spring学习笔记之依赖的注解(2)
2015-10-23 08:23
429 查看
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
Person.java
Student.java
public class Student {
public void say(){
System.out.println(“student”);
}
}
PersonTest.java
5.0 小结
6.0 spring注解的继承
xml的继承需要,parent:spring容器中的继承
而注解的继承配置文件不需要parent。
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。
相关文章推荐
- Java抽象和接口的学习和笔记——黑马训练营
- Java集合框架之Map集合学习和笔记——黑马训练营
- spring unit test start error
- java socket编程中的一些问题
- java基础-程序流程控制
- [深入理解Java虚拟机]第六章 字节码指令简介
- 机器学习实战朴素贝叶斯的java实现
- java jar命令及补丁方法
- 我编辑的JAVA日历程序
- java finally return
- javaweb错误集锦
- java基础——Java概述
- maven创建springmvc项目
- Spring+Mybatis+Mysql搭建分布式数据库访问框架的方法
- Java记录 -43- Java ArrayList
- 在spring data jpa使用UUID
- Spring download address
- eclipse的一些快捷键
- JUnit测试运行器
- 经典Spring AOP