Java 使用两种不同的上下文开发SpringWeb应用程序
在 第2.3节使用场景,有一段是这样的Java 使用两种不同的上下文开发SpringWeb应用程序,java,spring,spring-mvc,Java,Spring,Spring Mvc,在 第2.3节使用场景,有一段是这样的 有时情况不允许您完全切换到不同的框架。春天 框架确实如此 不 强迫你使用其中的一切;这不是一个问题 要么全有,要么全无 解决方案现有的 使用Struts、Tapestry、JSF或其他UI框架构建的前端可以与Spring集成- 基于中间层,允许您使用Spring事务特性。你只需要把你的电话接通就行了 使用ApplicationContext的业务逻辑 并使用WebApplicationContext 整合 您的web层。 现在我不能理解最后一句话。我们如何
有时情况不允许您完全切换到不同的框架。春天 框架确实如此 不 强迫你使用其中的一切;这不是一个问题 要么全有,要么全无 解决方案现有的 使用Struts、Tapestry、JSF或其他UI框架构建的前端可以与Spring集成- 基于中间层,允许您使用Spring事务特性。你只需要把你的电话接通就行了 使用ApplicationContext的业务逻辑 并使用WebApplicationContext 整合 您的web层。 现在我不能理解最后一句话。我们如何使用ApplicationContext连接业务逻辑,并使用WebApplicationContext与web层集成。我们如何才能做到这一点?他们谈论的web层是否包含控制器和JSP 就我所记得的,如果我们需要类中的任何对象,我们只需自动连接它们,剩下的工作由spring完成 有人能举例说明吗。请原谅我的无知,因为我刚刚开始学习春天
如果有人问类似的问题,请有人给我指出正确的方向您可以设置两个甚至三个不同的项目或模块,每个项目或模块都有各自的上下文。例如,使用WebApplicationContext呈现视图并调用业务方法的web项目,即来自业务层的usinf restful web服务。并设置一个单独的项目或模块来处理具有自己的上下文文件和bean的业务。甚至是一个commons项目,在web和业务层之间包含共享bean。您可以设置两个甚至三个不同的项目或模块,每个项目或模块都有自己的上下文。例如,使用WebApplicationContext呈现视图并调用业务方法的web项目,即来自业务层的usinf restful web服务。并设置一个单独的项目或模块来处理具有自己的上下文文件和bean的业务。甚至是一个commons项目,在web和业务层之间包含共享bean。这是可能的,即使您可以按层次结构创建多个不同的上下文 我将给出两个答案,包括层次和非层次。我将使用基于java的配置来实现这两种功能。我将给出两个上下文的答案,但您可以在许多上下文中实现这一点 1)非层次结构 创建两个不同的
context.xml
,假设context1.xml
和context2.xml
。context1.xml应该是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=..... some imports >
<context:annotation-config />
<context:component-scan base-package="desiredPackage1" />
<bean id="properties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>db.properties</value>
</list>
</property>
</bean>
<context:property-placeholder properties-ref="properties"/>
现在您有了两个不同的上下文,如果您想按层次结构对其进行更改,请更改main方法,如下所示:
2)层次结构
public class Config {
public static void main(String[] args) throws Exception {
ApplicationContext desiredContext1 = new ClassPathXmlApplicationContext("file:////...path.../context1.xml");
String[] congigPath = new String[1];
congigPath[0] = "file:////...path.../context2.xml";
ApplicationContext desiredContext2 = new ClassPathXmlApplicationContext(congigPath,desiredContext1);
}
}
在这种情况下,desiredContext2对象可以看到desiredContext1对象,但desiredContext1对象无法看到desiredContext2对象
如果您想在构建web应用程序时使用它,请在配置类中使用此注释
@Configuration
@ImportResource("context1.xml", "context2.xml")
public class Config { ....
我希望这将对您有所帮助。这是可能的,即使您可以按层次结构创建多个不同的上下文 我将给出两个答案,包括层次和非层次。我将使用基于java的配置来实现这两种功能。我将给出两个上下文的答案,但您可以在许多上下文中实现这一点 1)非层次结构 创建两个不同的
context.xml
,假设context1.xml
和context2.xml
。context1.xml应该是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=..... some imports >
<context:annotation-config />
<context:component-scan base-package="desiredPackage1" />
<bean id="properties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>db.properties</value>
</list>
</property>
</bean>
<context:property-placeholder properties-ref="properties"/>
现在您有了两个不同的上下文,如果您想按层次结构对其进行更改,请更改main方法,如下所示:
2)层次结构
public class Config {
public static void main(String[] args) throws Exception {
ApplicationContext desiredContext1 = new ClassPathXmlApplicationContext("file:////...path.../context1.xml");
String[] congigPath = new String[1];
congigPath[0] = "file:////...path.../context2.xml";
ApplicationContext desiredContext2 = new ClassPathXmlApplicationContext(congigPath,desiredContext1);
}
}
在这种情况下,desiredContext2对象可以看到desiredContext1对象,但desiredContext1对象无法看到desiredContext2对象
如果您想在构建web应用程序时使用它,请在配置类中使用此注释
@Configuration
@ImportResource("context1.xml", "context2.xml")
public class Config { ....
我希望这将对您有所帮助。谢谢您的回答。我们可以为单个项目或模块实现这种配置吗?谢谢您的回答。我们能否为单个项目或模块实现这种配置?这肯定会有所帮助。现在,如果我在任何类中自动连接任何对象,spring将搜索上下文desiredContext1和desiredContext2,以检查bean声明是否存在于任何上下文中,是否正确?非常感谢您的回答,这肯定会有所帮助。现在,如果我在任何类中自动连接任何对象,spring将搜索上下文desiredContext1和desiredContext2,以检查bean声明是否存在于任何上下文中,对吗?非常感谢您的回答