package com.bebig.service;

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

//逻辑业务层
public class UserService {
    
// 构造函数里有一个userDAO对象需要初始化
    public UserService(UserDAO userDAO) {
        
super();
        
this.userDAO = userDAO;
    }


    
private UserDAO userDAO;

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

    }


    
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 -->
    
    
<!-- 在启动容器时,如果lazy-init为true,则不启动时不自动创建此对象,在使用时再创建 -->
    
<bean id="u" class="com.bebig.dao.impl.UserDAOImpl" lazy-init="true" />
    
    
<!-- 指定对象初始化方法、对象销毁方法 -->
    
<bean id="userService" class="com.bebig.service.UserService"
        init-method
="init" destroy-method="destory">
        
<constructor-arg>
            
<ref bean="u" />
        
</constructor-arg>
    
</bean>

</beans>
注意:destroy-methd 不要和prototype一起用,因为在scope为prototype时,由容器自动来销毁对象。