programing

스프링 데이터 JPA(스프링 부트 없음)

newsource 2023. 3. 11. 09:01

스프링 데이터 JPA(스프링 부트 없음)

Spring Boot은 Google App Engine과 함께 사용할 수 없습니다(적어도 저에게는 해당되지 않습니다).

GitHub 또는 기타 저장소에서 작성 또는 이용할 수 있는 예제의 대부분은 Spring Boot을 사용하고 있는데, Spring-Data-JPA는 이전과 같이 Spring Boot을 사용하지 않고 플레인 컨텍스트 XML을 사용하는 예가 있습니까?

그러면 알 수 있다

- spring/spring.xml(또는 servlet-spring.xml)

이 예제를 작성했습니다.

src/main/webapp/WEB-INF/spring/context.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

    <context:property-placeholder location="/WEB-INF/spring/jdbc.properties" />

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}"
        p:username="${jdbc.username}" p:password="${jdbc.password}" />

    <bean id="hibernateJpaVendorAdapter"
        class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />

    <!-- Configure the entity manager factory bean -->
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
        <!-- Set JPA properties -->
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            </props>
        </property>
        <property name="packagesToScan" value="com.demo.data" />
    </bean>

    <!-- Configure the transaction manager bean -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <tx:annotation-driven />

    <jpa:repositories base-package="com.demo.data" />
    <context:component-scan base-package="com.demo.svc" />
</beans>

/src/main/webapp/WEB-INF/spring/jdbc.properties

# JDBC Connection
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://host:3306/db
jdbc.username=user
jdbc.password=password
hibernate.dialect=org.hibernate.dialect.MySQLDialect

/src/main/webapp/WEB-INF/spring/servlet-context.xml

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

    <context:component-scan base-package="com.demo.web" />
</beans:beans>

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/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/context.xml</param-value>
    </context-param>
    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>demo_jpa</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

pom.xml

<properties>
    <org.springframework-version>4.1.7.RELEASE</org.springframework-version>
    <org.hibernate-em-version>4.3.8.Final</org.hibernate-em-version>
    <org.springframework.data-version>1.9.0.RELEASE</org.springframework.data-version>
    <dbcp-version>1.4</dbcp-version>
    <mysql-connector-version>5.1.28</mysql-connector-version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>${org.springframework.data-version}</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${org.hibernate-em-version}</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql-connector-version}</version>
    </dependency>

    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>${dbcp-version}</version>
    </dependency>
</dependencies>

데이터 엔티티: com.demo.data.사용자

package com.demo.data;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "SYS_USERS")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "USER_ID")
    private int id;

    @Column(name = "USER_NAME")
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

스프링 데이터 JPA 저장소: com.demo.data.사용자 저장소

package com.demo.data;

import org.springframework.data.repository.CrudRepository;

public interface UserRepository extends CrudRepository<User, Integer> {
}

서비스

인터페이스:

package com.demo.svc;

import com.demo.data.User;

public interface UserService {
    User findUserById(Integer id);
}

구현:

package com.demo.svc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo.data.User;
import com.demo.data.UserRepository;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserRepository userRepository;

    public User findUserById(Integer id) {
        return userRepository.findOne(id);
    }
}

Web 컨트롤러: com.demo.web.사용자 컨트롤러

package com.demo.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo.svc.UserService;

@Controller
public class UserController {
    @Autowired
    UserService svc;

    @RequestMapping("/")
    @ResponseBody
    public String test() {
        return svc.findUserById(1).getName();
    }
}

이는 컨텍스트에서 UserRepository의 bean을 작성하지 않았기 때문입니다.@Autowired가 UserRepository 클래스에 개체를 삽입하지 않기 때문에 시작 시 컨테이너에 개체를 만들지 않도록 하기 위해 스프링 부트 프로젝트를 사용하지 않기 위해 여러 가지 방법이 있습니다.이 프로젝트의 경우 프로젝트에서 다음을 업데이트할 수 있습니다.

 package com.demo.svc;

 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;

 import com.demo.data.User;
 import com.demo.data.UserRepository;

 @Service
 public class UserServiceImpl implements UserService {

 UserRepository userRepository;

 public User findUserById(Integer id) {
     return userRepository.findOne(id);
 }

  public UserRepository setRepo(UserRepository repo)
  {
    userRepository=repo;
  }

}

또한 용기에서 물체를 주입해야 합니다.src/main/webapp/WEB-INF/spring/context.xml 내

<bean id="repo" class="com.demo.data.UserRepository"></bean>

<!--Setting the object ref in com.demo.svc.UserServiceImpl-->

<bean id="userserviceImpl" class="com.demo.svc.UserServiceImpl">
 <property name="userRepository" ref="repo"></property>
 </bean>

도움이 되었으면 좋겠다

이를 추가하면 문제가 해결됩니다.javax.servlet servlet-api 2.5 제공.servlet 3.1(또는 3.0.1)을 사용하는 경우 javax.servlet javax.servlet-api 3.1.0 제공.

언급URL : https://stackoverflow.com/questions/23974249/spring-data-jpa-without-spring-boot