@Resource装配实例


@Autowired与@Resource的区别?

1、@Autowired@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上。

 2、@Autowired默认按类型装配(这个注解是属业spring的),默认情况下必须要求依赖对象必须存在,如果要允许null 值,可以设置它的required属性为false,如:@Autowired(required=false) ,如果我们想使用名称装配可以结合@Qualifier注解进行使用,如下: 

@Autowired 
@Qualifier("customerDao")     
private CustomerDao customerDao;   

  3、@Resource(这个注解属于J2EE的),默认安照名称进行装配,名称可以通过name属性进行指定, 如果没有指定name属性,当注解写在字段上时,默认取字段名进行按照名称查找,如果注解写在setter方法上默认取属性名进行装配。 当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。

@Resource(name="customerDao")     
private CustomerDao customerDao;   


 个人建议使用 @Resource注解在字段上,而且这个注解是属于J2EE的,减少了与spring的耦合。最重要的这样代码看起就比较优雅。

以下给出@Resource的完整案例:

一、编写Bean类

CustomerDao接口:

package com.yiidian.dao;
/**
 * 
 * @author http://www.yiidian.com
 *
 */
public interface CustomerDao {
	public void save();
}

CustomerDaoImpl实现类:

package com.yiidian.dao.impl;

import org.springframework.stereotype.Repository;

import com.yiidian.dao.CustomerDao;
/**
 * @author http://www.yiidian.com
 *
 */
@Repository(value="customerDao")
public class CustomerDaoImpl implements CustomerDao {

	@Override
	public void save() {
		System.out.println("执行了CustomerDaoImpl的save()方法");
	}
	
}

CustomerService接口:

package com.yiidian.service;
/**
 * 
 * @author http://www.yiidian.com
 *
 */
public interface CustomerService {

	public void save();
}

CustomerServiceImpl实现类:

package com.yiidian.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yiidian.dao.CustomerDao;
import com.yiidian.service.CustomerService;
/**
 * 
 * @author http://www.yiidian.com
 *
 */
@Service(value="customerService")
public class CustomerServiceImpl implements CustomerService {
	@Resource(name="customerDao")
	private CustomerDao customerDao;

	@Override
	public void save() {
		customerDao.save();
	}
}

二、配置applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p"
	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.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
	
	<!-- 开启SpringIOC注解扫描 -->
	<context:component-scan base-package="com.yiidian"></context:component-scan>
	
</beans>

三、编写测试类

package com.yiidian.test;

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

import com.yiidian.service.CustomerService;
/**
 * @author http://www.yiidian.com
 *
 */
public class Demo1 {
	
	@Test
	public void test1(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		CustomerService customerService = (CustomerService)ac.getBean("customerService");
		customerService.save();
	}
}

四、运行结果

源码下载:http://pan.baidu.com/s/1jHG3Xgq