UserService.java
package com.bebig.service;

import javax.annotation.Resource;

import com.bebig.dao.UserDAO;
import com.bebig.model.User;


public class UserService {
    
private UserDAO userDAO;

    
public void add(User u) {
        userDAO.save(u);

    }


    @Resource
    
//默认按名称,名称找不到,再按类型
    public void setUserDAO(UserDAO userDAO) {
        
this.userDAO = userDAO;
    }


    
public UserDAO getUserDAO() {
        
return userDAO;
    }


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


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

}

beans.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-3.0.xsd
http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd"
>

    
<!-- a service object; we will be profiling its methods -->
    
<context:annotation-config />
    
<!-- 存在与属性名称相同id的bean -->
    
<bean id="userDAO" class="com.bebig.dao.impl.UserDAOImpl"  />
    
    
<bean id="u2" class="com.bebig.dao.impl.UserDAOImpl"  />
    
    
<bean id="userService" class="com.bebig.service.UserService">

    
</bean>

</beans>
 

1.     @Resource(重要)

a)     默认按名称,名称找不到,再按类型

b)     可以指定特定名称

c)     推荐使用

d)     不足:如果没有源码,就无法运用annotation,只能使用xml