spring的依赖注入 注解版

要说的都在注解里面了


创建Person和Student类

public class Person {
	
	/**
	 * 
	 */
	@Resource(name="student")
	//@Autowired				//按照类型匹配,这是spring框架提供的一种注解方式
	//@Qualifier("student")   //按照spring容器中bean的id进行匹配,下面两个注解的作用等于上面的注解
	private Student student;

	public void setStudent(Student student) {
		this.student = student;
	}
	public Student getStudent() {
		return student;
	}
}
public class Student{
	public void say(){
		System.out.println("hello");
	}
}



首先更改applicationContext.xml中的头信息

<?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">
      <bean id="person" class="com.mo.entity.Person"></bean>
	  <bean id="student" class="com.mo.entity.Student"></bean>
	  
	 <!-- 启动注解的驱动 -->
     <context:annotation-config/>
     <!-- 
     	原理: 1.当加载spring的配置文件时,加载两个bean到spring容器中去
     		2.因为启动了注解驱动,spring会在容器中寻找bean的属性 上面有@Resource注解的,并将其属性的属性名与spring容器中bean的id对照,
     		一致的话就进行依赖注入,不一致的话就按照属性的类型与spring容器bean的属性的类型进行匹配,类型一致就进行注入,若spring容器中不存在这种类型,那么将报错
     		
     		如果注解是这种形式@Resource(name="xxx"),就按照name中的值与spring容器中bean的id值进行匹配,一致进行注入,若不存在这个id就报错
     		
     	注意:注解只适应于引用类型
     	
     -->
</beans>    

测试

@Test
	public void test4(){
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		Person person = (Person)context.getBean("person");
		person.getStudent().say();
	}
输出就是hello


spring的类扫描

首先applicationContext.xml的头文件要更改

<?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">
           
     <!-- 类扫描启动,在base-package指定的包及子包下扫描所有带有特定注解的类 -->
     <context:component-scan base-package="com.mo.entity"/>
     
</beans>

/*
 * @Component注解会将Person类名  翻译为<bean id="person" class=".."/>
 * 	就是将类名的第一个字母小写,变成spring容器中的一个bean
 * 
 * 如果是这样写@Component("per") 就会翻译为<bean id="per" class=""/>
 */
@Component
public class Person {
@Component
public class Student{

当加载applicationContext.xml配置文件时,这两个类就会被将加载到spring的容器中




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值