爱玺玺

爱玺玺的生活日记本。wx:lb87626

spring接口属性原理

spring配置了一个bean

<bean id="employerServiceImp" class="cn.lb.services.imp.EmployerServiceImp"/>


这个bean对应的类


package cn.lb.services.imp;


import javax.annotation.Resource;


import org.hibernate.SessionFactory;


import cn.lb.domain.Department;

import cn.lb.services.inter.DepartmentInter;


public class DepartmentImp implements DepartmentInter {

@Resource

    private SessionFactory sessionFactory;

    

public SessionFactory getSessionFactory() {

return sessionFactory;

}


public void setSessionFactory(SessionFactory sessionFactory) {

this.sessionFactory = sessionFactory;

}


@Override

public void addDepartment(Department department) {

sessionFactory.getCurrentSession().save(department);

}


}


这个类实现了一个接口,并且通过注释配置了一个熟悉sessionFactory,这个熟悉名字应该和其它配置有个映射关系。。

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

     <property name="dataSource" ref="dataSource"/>


配置文件没有做ref依赖配置直接在domain里面通过注解配置了。


spring又配置了个action

   <bean name="/login" class="cn.lb.web.action.EmployerAction" scope="prototype">

     <property name="employerServiceInter" ref="employerServiceImp"></property>

   </bean>


这个action初始化两个熟悉,就是上面domain继承的那个接口。


private EmployerServiceInter employerServiceInter;


然后这个action就既然可以直接使用这个接口来引用实现它方法的那个类的方法。这里都不需要配置接口引用。


employer=employerServiceInter.chkUser(employer);


完整代码是:

package cn.lb.web.action;


import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

import org.apache.struts.actions.DispatchAction;

import cn.lb.domain.Employer;

import cn.lb.services.inter.EmployerServiceInter;

import cn.lb.web.form.EmployerForm;


public class EmployerAction extends DispatchAction {

private EmployerServiceInter employerServiceInter;

public EmployerServiceInter getEmployerServiceInter() {

return employerServiceInter;

}


public void setEmployerServiceInter(EmployerServiceInter employerServiceInter) {

System.out.println("通过spring初始化action,属性setEmployerServiceInter");

this.employerServiceInter = employerServiceInter;

}

int i=1;

public ActionForward login(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response)

throws Exception {

//WebApplicationContext webApplicationContext

//=WebApplicationContextUtils.

//getWebApplicationContext(this.getServlet().getServletContext());

//从spring容器中获取bean

//EmployerServiceInter employerServiceInter=(EmployerServiceInter) webApplicationContext.getBean("employerServiceImp");

i++;

System.out.println("通过spring实例化的action"+"判断多例还是单例"+i);

EmployerForm employerForm=(EmployerForm) form;

Employer employer=new Employer();

employer.setId(employerForm.getId());

employer.setPwd(employerForm.getPwd());

System.out.println(employer.getId()+" "+employer.getPwd());

employer=employerServiceInter.chkUser(employer);

if(employer!=null){

request.getSession().setAttribute("employer", employer);

return mapping.findForward("ok");

}else{

return mapping.findForward("err");

}

}

}


上面只声明了个接口,并没有对接口做任何配置既然都能使用了,那只能说明是spring帮忙配置了。


也就是spring配置接口属性会自动去调用实现了这个接口的方法,但是如果有几个类实现了这个接口怎么办?


明白了,不会出现这个问题,因为spring的配置文件指定了使用了个实现接口的实例。


   <bean name="/login" class="cn.lb.web.action.EmployerAction" scope="prototype">

     <property name="employerServiceInter" ref="employerServiceImp"></property>

   </bean>


发表评论:

Powered By Z-BlogPHP 1.4 Deeplue Build 150101

Copyright Your WebSite.Some Rights Reserved.

蜀ICP备11021721号-5