【问题标题】:hibernate jpa with java configuration using spring instead of xml使用spring而不是xml使用java配置休眠jpa
【发布时间】:2016-05-07 10:42:13
【问题描述】:

在标题中,我正在尝试将我的 CRUD 从 xml 重新配置为基于 Java 的配置。

我被 InternalResourceViewer 卡住了。

这里有一点代码:

mvc 配置:

package kaczynski.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@ComponentScan(basePackages="kaczynski")
@EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter{

 @Bean
public ViewResolver getViewResolver(){
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/jsp/");
    resolver.setSuffix(".jsp");
    return resolver;
}
}

我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
id="WebApp_ID" version="2.4">
<display-name>Animal Database</display-name>

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
        </init-param>
        <init-param>
                <param-name>contextConfigLocation</param-name>
            <param-value>/kaczynski/config/MvcConfig.java</param-value>
        </init-param>


    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

在我为 jpa 保存配置的旧 applicationContext.xml 中,我有这一行:

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

如果我删除它,我将看不到视图。我对此有点困惑,因为:

  1. 我的 `MvcConfig.java` 文件中有 `@ComponentScan(basePackages="kaczynski")` 注释。
  2. 我的 `web.xml` 中没有任何指向 `applicationContext.xml` 的指针。

您对此有什么解决方案吗?

【问题讨论】:

  • 你的 JPA 配置在哪里?

标签: java spring hibernate spring-data


【解决方案1】:

这是一篇关于使用 java 配置 JPA 的好博文。

http://www.baeldung.com/2011/12/13/the-persistence-layer-with-spring-3-1-and- jpa/#javaconfig

在我的大多数应用程序中,我每个部分都有一个配置文件 - Web、jpa、安全性、数据源等...

通常对于 JPA,您需要在以

开头的配置文件中配置实体管理器
@Configuration
@EnableTransactionManagement

您还需要一个@ComponentScan,这样spring 才能发现@Controller@Service 之类的东西

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-19
    • 1970-01-01
    相关资源
    最近更新 更多