我用最通俗的语言问一个问题:
ssh结构做一个项目。需要写“接口”,然后“实现”这个“接口”,然后又在action或者什么地方用到这个“实现”的“方法”。
到底在applicationContext中怎么配置!!!这个“实现”才能在action中或者用到的地方用上。我想知道的是set/get方法的依赖注入
ssh结构做一个项目。需要写“接口”,然后“实现”这个“接口”,然后又在action或者什么地方用到这个“实现”的“方法”。
到底在applicationContext中怎么配置!!!这个“实现”才能在action中或者用到的地方用上。我想知道的是set/get方法的依赖注入
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();
第一,我不清楚配置文件内的内容。书也没有讲那么细的。我看“spring开发手册”了,讲是讲了。但是没有那么细!我根本不明白是什么意思。lgstarzkhl写的跟我意思差不多。不过还是有点区别。
如果lgstarzkhl能再看下就好了。
我的意思是,既然实现了这个“接口”,那么这个“实现类”肯定就会在某地被调用,对么?
例如:
public class LoginAction extends Action {
private AService aService;
aService.sayA();
}对吧?这个接口肯定会被调用。配置文件里边不用设置,就可以调用这个接口?可以么?
所以,我就是不明白,配置文件里边怎样设置,才可以调用这个接口
哥们,你说的好啊。我要是知道怎样在applicationContext上下文里边写。那我不就不问大家了么?设定代理你的类 就可以了!!!
这句也说的好呀,关键我怎样设置?哥们,拉兄弟一把吧。要是知道怎样写,就麻烦把applicationContext内的代码写一下。
我就是想要一段在applicationContext写好的代码
我就是想要一段在applicationContext写好的代码
我就是想要一段在applicationContext写好的代码
我就是想要一段在applicationContext写好的代码
我就是想要一段在applicationContext写好的代码
我就是想要一段在applicationContext写好的代码
我就是想要一段在applicationContext写好的代码
我就是想要一段在applicationContext写好的代码
我就是想要一段在applicationContext写好的代码
我就是想要一段在applicationContext写好的代码
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结构的项目中,不都是这么写么?
可以假设能用么?在上下问里边应该怎样写?
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> 不知道是不是要这样的效果.
<property name="aiface" ref="aiface" />
<property name="biface" ref="biface" />
</bean>
咱们就拿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应该怎样写。
<bean id="aService" class="A" />
<!--下边的bean中的name可以是任意的-->
<bean name="/LoginAction" class="LoginAction ">
<property name="aService" ref="aService">
</bean>