我用最通俗的语言问一个问题: 
ssh结构做一个项目。需要写“接口”,然后“实现”这个“接口”,然后又在action或者什么地方用到这个“实现”的“方法”。 
到底在applicationContext中怎么配置!!!这个“实现”才能在action中或者用到的地方用上。我想知道的是set/get方法的依赖注入

解决方案 »

  1.   

    接口A
    Interface AService{
     void sayA();
    }
    接口B
    Interface BService{
     void sayB();
    }
    A实现类
    Class A implements AService{
     void sayA(){
      System.out.println("A");
     }
    }
    B实现类
    Class B implements BService{
     void sayB(){
      System.out.println("B");
     }
    }配置文件如下 appliciationContext.xml
    <bean name="AService" class="包名.A" >
    <bean name="BService" class="包名.B" >
    name指定哪个接口,也可用别名
    测试类如下
    ApplicationContext ctx = new ClassPathXmlApplicationContext("appliciationContext.xml");
    //调用A
           AService a = (AService)ctx.getBean("AService");
    a.sayA();
    //调用B
    BService b = (BService)ctx.getBean("BService");
    b.sayB();
      

  2.   

    谢谢楼上2位
    第一,我不清楚配置文件内的内容。书也没有讲那么细的。我看“spring开发手册”了,讲是讲了。但是没有那么细!我根本不明白是什么意思。lgstarzkhl写的跟我意思差不多。不过还是有点区别。
    如果lgstarzkhl能再看下就好了。
    我的意思是,既然实现了这个“接口”,那么这个“实现类”肯定就会在某地被调用,对么?
    例如:
    public class LoginAction extends Action {
        private AService aService;
        aService.sayA();
    }对吧?这个接口肯定会被调用。配置文件里边不用设置,就可以调用这个接口?可以么?
    所以,我就是不明白,配置文件里边怎样设置,才可以调用这个接口
      

  3.   

    还有你这个接口是 spring上下文的吗 不然不可以代理的 这个是初学的最容易犯的一个错误!
      

  4.   

    建议你看看这个帖子http://topic.csdn.net/u/20071113/16/ecc8ad50-86a5-4b3e-b36c-dcf7b092aec9.html?1388971649
      

  5.   

    还有你这个接口是   spring上下文的吗   不然不可以代理的   这个是初学的最容易犯的一个错误!
    哥们,你说的好啊。我要是知道怎样在applicationContext上下文里边写。那我不就不问大家了么?设定代理你的类   就可以了!!!   
    这句也说的好呀,关键我怎样设置?哥们,拉兄弟一把吧。要是知道怎样写,就麻烦把applicationContext内的代码写一下。
    我就是想要一段在applicationContext写好的代码
    我就是想要一段在applicationContext写好的代码
    我就是想要一段在applicationContext写好的代码
    我就是想要一段在applicationContext写好的代码
    我就是想要一段在applicationContext写好的代码
    我就是想要一段在applicationContext写好的代码
    我就是想要一段在applicationContext写好的代码
    我就是想要一段在applicationContext写好的代码
    我就是想要一段在applicationContext写好的代码
    我就是想要一段在applicationContext写好的代码
      

  6.   

    我看你的不能写 不是spring上下文的
      

  7.   

    有方法变成上下文么?public class LoginAction extends Action {
        private AService aService;
        public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
            aService.sayA();
            return null;
        }    public void setAService(AService aService) {
    this.aService = aService;
        }
    }在SSH结构的项目中,不都是这么写么?
    可以假设能用么?在上下问里边应该怎样写?
      

  8.   

    需要实现spring的 接口,就是上下文的了 呵呵
      

  9.   

    nanjg你真是高手,佩服你。继续等人解答
      

  10.   

    public interface c{
      public void ASay();
    }
    public interface BIface{
      public void BSay();
    }
    public class AClass implements AIface {
      public void ASay() {
        ....
      }
    }
    public class BClass implements BIface {
      public void BSay() {
        ....
      }
    }public class LoginAction extends Action {
      AIface aiface;
      BIface biface;
      public void setAiface(AIface iaface) {
        this.iaface = iaface;
      }
      public void setBiface(AIface baface) {
        this.baface = baface;
      }
    }
    applicationContext.xml <bean id="aiface" class="com.AIface" /> <bean id=biface" class="com.BIface" /><bean name="/loginAction" class="com.LoginAction">
      <property name="aiface" class="com.AClass">
      <property name="biface" class="com.BClass">
    </bean>struts-config.xml
    <!-- 使用到请求委托
       DelegatingRequestProcessor告诉struts自动把动作请求( path)委托给spring上下文中的对应的action,这样我们只需要在action-mapping中配制path就可以-->
      <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor" />
      
      <action-mappings>
        <action path="/loginAction" type="org.springframework.web.struts.DelegationActionProxy" />
     </action-mappings> 不知道是不是要这样的效果.
      

  11.   

    <bean   name="/loginAction"   class="com.LoginAction"> 
        <property   name="aiface"   ref="aiface" /> 
        <property   name="biface"   ref="biface" /> 
    </bean> 
      

  12.   

    关键你的代理的类根本就不是基于spring的。貌似不明白我的意思,象楼上的一定不行的
      

  13.   

    高手,我问一下(代码在下边)。
    咱们就拿SSH项目来说,一般是这么写代码么?
    另外就假设我这样写是通过applicationContext.xml配置后就可以用的。
    我就是不知道该怎样在xml文件中配置接口A   Java codeInterface   AService{ 
      void   sayA(); 

    A实现类   Java codeClass   A   implements   AService{ 
      void   sayA(){ 
        System.out.println("A"); 
      } Action里边 
    Java codepublic class LoginAction extends Action {
        private AService aService;
        public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
            aService.sayA();
            return null;
        }    public void setAService(AService aService) {
            this.aService = aService;
        }
    }我就是想知道,如果写成这样的代码。applicationContext.xml应该怎样写。
      

  14.   

    对于楼主的代码,在applicationContext.xml中,
    <bean   id="aService"   class="A"   />
    <!--下边的bean中的name可以是任意的-->
    <bean   name="/LoginAction"   class="LoginAction ">
        <property   name="aService"   ref="aService">
    </bean>