Spring——依赖注入的注解解析器

<?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:context="http://www.springframework.org/schema/context"
       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">
      <!--  
      	导入依赖注入的注解解析器
      		1.导入基于注解的命名空间(XSD)
      			xmlns:context="http://www.springframework.org/schema/context"
      			http://www.springframework.org/schema/context
           		http://www.springframework.org/schema/context/spring-context-2.5.xsd
           		
           	2.导入注解解析器
           		<context:annotation-config></context:annotation-config>
           		
           	3.导入person和student
      -->
      <!--  
      	工作原理:
      		当Spring容器启动的时候。
      			ApplicationContext context = new \
      				ClassPathXmlApplicationContext("cn/itcast/spring01/di/annotation/applicationContext.xml")
      		spring容器会创建纳入spring容器管理的bean,分别为Person和Student
      		spring容器会解析配置文件,会解析到<context:annotation-config></context:annocation-config>
      		会在纳入spring的bean范围内查找属性上是否存在
      		@Resource(name="student")
      			*如果存在:
  					继续解析@Resource有没有name属性
  						*如果没有name属性(就会在所属的属性上把属性的名称解析出来,会让属性的名称和spring中的bean中的id进行匹配
  							*如果匹配成功,则把spring容器中相应的对象赋值给该属性
  							*如果匹配不成功,则按照类型进行匹配)
  						
  						*如果@Resource有name属性(就会解析name属性的值,把这个值和spring容器中的bean的id进行匹配
  							*如果匹配成功,则把spring容器中的相应的对象赋值给该属性
  							*如果匹配不成功,则直接报错
  				*如果不存在:
  					不做任何事情  
  		
  		xml和注解的写法:
  			xml:书写比较麻烦,但是效率比较高
  			注解:书写比较简单,但是效率比较低
  		注解的写法只适合引用类型
      -->
      
      <context:annotation-config></context:annotation-config>
      
      <bean id="person" class="cn.itcast.spring01.di.annotation.Person"></bean>
      <bean id="student" class="cn.itcast.spring01.di.annotation.Student"></bean>
</beans>



<!--
依赖注入的注解解析器:

	在配置文件中:
		* xsd(命名空间)
			xmlns:context="http://www.springframework.org/schema/context"
      		http://www.springframework.org/schema/context
           	http://www.springframework.org/schema/context/spring-context-2.5.xsd
		
		* 注解解析器
			<context:annotation-config></context:annotation-config>
		
		* 支持的注解
		    @Resource
		    @Autowried
		    @Qualifier
		    @PostConstruct 初始化方法的注解
		    @PreDestory    销毁的方法的注解

-->


 

Student类

package cn.itcast.spring01.scan;

public class Student {

	public void say() {
		System.out.println("student");
	}
}


 

Person类

package cn.itcast.spring01.scan;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class Person {
	@Resource(name="student")
	//下面两个注解就相当于@Resource,他们是spring自己发明的
//	@Autowired
//	@Qualifier(value="student")
	private Student student;
	
	public void say(){
		this.student.say();
	}
	
	//下面这样写,在配置文件中就不需要写init-method和destory-method
//	@PostConstruct
//	public void init(){
//		System.out.println("init");
//	}
//	@PreDestroy
//	public void desctory(){
//		System.out.println("desctory");
//	}
}


 

PersonTest测试类

package cn.itcast.spring01.scan;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class PersonTest {
	@Test
	public void test(){
		ApplicationContext context = new ClassPathXmlApplicationContext("cn/itcast/spring01/di/annotation/applicationContext.xml");
		Person person = (Person)context.getBean("person");
		person.say();
	}
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

招风的黑耳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值