日期:2014-05-18  浏览次数:20623 次

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

我想知道的是set/get方法的依赖注入

------解决方案--------------------
接口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();
------解决方案--------------------
建议找本技术手册 系统学学
------解决方案--------------------
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>

 不知道是不是要这样的效果.

------解决方案--------------------
去网上搜索吧,一大堆的资料呢,呵,也不用在这里进行讨论这个问题,这个问题在已经有人很好的解决了啊,呵.
基本搭建环境,做例子,的啊,呵.
祝你好运吧.
------解决方案--------------------
建议去看看这本书吧,挺不错的。


------解决方案--------------------
我是来顶并且接分的






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

------解决方案--------------------
接分
------解决方案--------------------