【问题标题】:Developing A Spring web-app using two different contexts使用两种不同的上下文开发 Spring Web 应用程序
【发布时间】:2017-05-28 11:01:40
【问题描述】:

spring reference documentation

2.3节使用场景,有一段是这样的

有时情况不允许您完全切换到不同的框架。春天 框架确实 不是 强迫您使用其中的所有内容;它不是一个 全有或全无 解决方案。现存的 使用 Struts、Tapestry、JSF 或其他 UI 框架构建的前端可以与 Spring- 基于中间层,它允许您使用 Spring 事务功能。你只需要连接你的 使用 ApplicationContext 的业务逻辑 并使用 WebApplicationContext 集成 你的网络层。

现在我无法理解最后一句话。我们如何使用 ApplicationContext 连接我们的业务逻辑并使用 WebApplicationContext 与 Web 层集成。我们怎样才能做到这一点?他们所说的 web 层是否包含控制器和 jsps?

据我所知,如果我们需要类中的任何对象,我们只需对它们进行自动装配,其余的工作由 spring 完成。

有人可以通过示例提供解释吗?刚开始学spring,请见谅。

如果有人问类似的问题,请指点我正确的方向

【问题讨论】:

    标签: java spring spring-mvc


    【解决方案1】:

    这是可能的,即使您可以分层创建多个不同的上下文。

    我会给出两个答案,包括等级和非等级。我将为两者使用基于 java 的配置。我将给出两个上下文的答案,但你可以在许多上下文中实现它。

    1)非分层

    创建两个不同的context.xml,假设context1.xmlcontext2.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"/>
    

    仅适用于 context2.xml 更改

    <context:component-scan base-package="desiredPackage2" /> 
    

    然后像这样创建一个Configuration.java 类:

    public class Config {
    
        public static void main(String[] args) throws Exception {
    
            ApplicationContext desiredContext1 = new ClassPathXmlApplicationContext("file:////...path.../context1.xml");
    
            ApplicationContext desiredContext2 = new ClassPathXmlApplicationContext("file:////...path.../context2.xml");
        }
    }
    

    现在你有两个不同的上下文,如果你想分层,改变 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 对象。

    如果您想在构建网络应用时使用它,请将此注释与您的配置类一起使用,

    @Configuration
    @ImportResource("context1.xml", "context2.xml")
    public class Config { ....
    

    希望对你有帮助。

    【讨论】:

    • 这肯定会有所帮助。因此,现在如果我在任何类中自动装配任何对象,spring 将同时搜索上下文 desiredContext1 和 desiredContext2 以检查 bean 声明是否存在于任何上下文中,对吗?
    • 非常感谢您的回答
    【解决方案2】:

    您可以设置两个甚至三个不同的项目或模块,每个项目或模块都有自己的上下文。例如,带有 WebApplicationContext 的 Web 项目正在呈现视图并调用业务方法,即从业务层使用静态 Web 服务。并设置一个单独的项目或模块来处理具有自己的上下文文件和 bean 的业务。甚至还有一个公共项目,包括 Web 层和业务层之间的共享 bean。

    【讨论】:

    • 感谢您的回答。我们可以为单个项目或模块实现这种配置吗?
    猜你喜欢
    • 2013-04-03
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多