xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance" xmlns&#61;"http://www.springframework.org/schema/beans" xsi:schemaLocation&#61;"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd "><bean name&#61;"userDao" class&#61;"www.test.dao.impl.UserDaoImpl">bean><bean name&#61;"linkManDao" class&#61;"www.test.dao.impl.LinkManDaoImpl">bean><bean name&#61;"customerDao" class&#61;"www.test.dao.impl.CustomerDaoImpl">bean><bean name&#61;"userService" class&#61;"www.test.service.impl.UserServiceImpl"><property name&#61;"ud" ref&#61;"userDao">property>bean><bean name&#61;"linkManService" class&#61;"www.test.service.impl.LinkManServiceImpl"><property name&#61;"cd" ref&#61;"customerDao">property><property name&#61;"lmd" ref&#61;"linkManDao">property>bean><bean name&#61;"customerService" class&#61;"www.test.service.impl.CustomerServiceImpl"><property name&#61;"customerDao" ref&#61;"customerDao">property>bean>
beans>
注意对象中需要有对应的属性才能注入。
package www.test.service.impl;import www.test.dao.CustomerDao;
import www.test.dao.LinkManDao;
import www.test.dao.impl.CustomerDaoImpl;
import www.test.dao.impl.LinkManDaoImpl;
import www.test.domain.Customer;
import www.test.domain.LinkMan;
import www.test.service.LinkManService;
import www.test.utils.HibernateUtils;public class LinkManServiceImpl implements LinkManService {private CustomerDao cd;private LinkManDao lmd;public void save(LinkMan lm) {//打开事务
HibernateUtils.getCurrentSession().beginTransaction();try {//1 根据客户id获得客户对象Long cust_id &#61; lm.getCust_id();Customer c &#61; cd.getById(cust_id);//2 将客户放入LinkMan中表达关系
lm.setCustomer(c);//3 保存LinkMan
lmd.save(lm);} catch (Exception e) {e.printStackTrace();//回滚事务
HibernateUtils.getCurrentSession().getTransaction().rollback();}//提交事务
HibernateUtils.getCurrentSession().getTransaction().commit();}public CustomerDao getCd() {return cd;}public void setCd(CustomerDao cd) {this.cd &#61; cd;}public LinkManDao getLmd() {return lmd;}public void setLmd(LinkManDao lmd) {this.lmd &#61; lmd;}}
<listener><listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<context-param><param-name>contextConfigLocationparam-name><param-value>classpath:applicationContext.xmlparam-value>
context-param>
//&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;
//获得spring容器&#61;>从Application域获得即可//1 获得servletContext对象
ServletContext sc &#61; ServletActionContext.getServletContext();
//2.从servletContext中获得ApplicationContext容器
WebApplicationContext ac &#61; WebApplicationContextUtils.getWebApplicationContext(sc);//3.从容器中获得CustomerService
CustomerService cs &#61; (CustomerService) ac.getBean("customerService");//&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;