java - 向 Spring 应用程序添加另一个上下文

标签 java xml spring spring-mvc

如何向 web xml 添加另一个上下文?

这是我作为 java 类编写的 PersistanceContext :

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {
        "ro.academy.model.daos"
})
@ComponentScan(basePackages = "ro.academy.service")
public class PersistenceContext {
    @Bean(destroyMethod = "close")
    DataSource dataSource(Environment env) {
        BasicDataSource dataSourceConfig = new BasicDataSource();
        dataSourceConfig.setDriverClassName(env.getRequiredProperty("db.driver"));
        dataSourceConfig.setUrl(env.getRequiredProperty("db.url"));
        dataSourceConfig.setUsername(env.getRequiredProperty("db.username"));
        dataSourceConfig.setPassword(env.getRequiredProperty("db.password"));

        return dataSourceConfig;
    }

    @Bean
    LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource,
                                                                Environment env) {
        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(dataSource);
        entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        entityManagerFactoryBean.setPackagesToScan("net.petrikainulainen.springdata.jpa.todo");

        Properties jpaProperties = new Properties();

        //Configures the used database dialect. This allows Hibernate to create SQL
        //that is optimized for the used database.
        jpaProperties.put("hibernate.dialect", env.getRequiredProperty("hibernate.dialect"));

        //Specifies the action that is invoked to the database when the Hibernate
        //SessionFactory is created or closed.
        jpaProperties.put("hibernate.hbm2ddl.auto",
                env.getRequiredProperty("hibernate.hbm2ddl.auto")
        );

        //Configures the naming strategy that is used when Hibernate creates
        //new database objects and schema elements
        jpaProperties.put("hibernate.ejb.naming_strategy",
                env.getRequiredProperty("hibernate.ejb.naming_strategy")
        );

        //If the value of this property is true, Hibernate writes all SQL
        //statements to the console.
        jpaProperties.put("hibernate.show_sql",
                env.getRequiredProperty("hibernate.show_sql")
        );

        //If the value of this property is true, Hibernate will format the SQL
        //that is written to the console.
        jpaProperties.put("hibernate.format_sql",
                env.getRequiredProperty("hibernate.format_sql")
        );

        entityManagerFactoryBean.setJpaProperties(jpaProperties);

        return entityManagerFactoryBean;
    }

    @Bean
    JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory);
        return transactionManager;
    }
}

Web xml 有另一个应用程序上下文(我认为这是默认的):

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/applicationContext.xml

        </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

这是写为 xml 文件的当前应用程序上下文:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <context:annotation-config/>
</beans>

似乎 spring 无法识别我的 PersistanceContext,其中我拥有所有配置。

如何将其添加到应用程序或让 spring 识别它?
非常感谢!

最佳答案

添加@ImportResource("classpath:applicationContext.xml")作为 PersistentContext 类的另一个注释。

另添加<context-param> <param-name>contextClass</param-name> <param-value> org.springframework.web.context.support.AnnotationConfigWebApplicationContext </param-value> </context-param>在 web.xml 中

或者,如果您想采取相反的方法,

您需要添加<context:component-scan base-package="com.javaconfig" />打包在 spring 上下文 xml 文件中以从 spring xml 加载 java 配置

将base-package更改为java配置所在的包名称。

关于java - 向 Spring 应用程序添加另一个上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37260556/

相关文章:

spring - @RequestHeader 请求参数和值所需的属性行为

java - 如何在java中使用H2数据库的全文搜索功能?

java - 使用什么: JPQL or Criteria API?

xml - 更改XML文件中的元素值

java - 如何使用 JAXB 生成其元素可以包含字符串或 XML 的类?

java - 匹配以某个路径元素开头的任何 url

java - 按下按钮后尝试绘画

java - Controller HttpServletRequest 语言环境没有改变

c# - 无法使用以 'μ' 开头的元素反序列化 xml

java - 验证是否从正在测试的方法调用了继承的父类(super class)方法