小编pas*_*ian的帖子

在Spring Boot中获取EntityManager的句柄

有没有办法获得给定实体对象的EntityManager句柄?我正在使用带有JPA启动器的spring boot 1.2.3,并且我进一步明确地配置了多个数据源@configuration

我检查了[已解决] SPRING BOOT对entityManager的访问权限,似乎没有回答这个问题.

谢谢.

编辑:我添加了如何定义数据源的说明:

@Component
@Configuration
public class DataSources {
    @Bean
    @Primary
    @ConfigurationProperties(prefix="first.datasource")
    public DataSource getPrimaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties(prefix="second.datasource")
    public DataSource getSecondDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties(prefix="third.final.datasource")
    public DataSource getThirdFinalDataSource() {
        return DataSourceBuilder.create().build();
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的application.yml中,我有以下部分

first.datasource:
  name: 'first_datasource',
  #other attributes...
second.datasource:
  name: 'second_datasource',
  #other attributes...
third.final.datasource:
  name: 'first_datasource',
  #other attributes...
Run Code Online (Sandbox Code Playgroud)

到目前为止,我已经尝试了@ Stephane的两个建议,但我得到了 NoSuchBeanDefinitionException

假设我的实体被调用Customer然后我试过了

@Service
public class FooService {

    private final EntityManager entityManager;

    @Autowired
    public FooService(@Qualifier("customerEntityManager") …
Run Code Online (Sandbox Code Playgroud)

java jpa spring-data spring-data-jpa spring-boot

14
推荐指数
1
解决办法
3万
查看次数

HTTP 500身体响应?

我有一个RESTEasy服务,当服务器端错误发生时返回HTTP 500.我设法将一个主体附加到HTTP响应,以提供有关错误的更多详细信息.因此,服务产生的响应看起来像这样

HTTP/1.1 500 Internal Server Error
Server: Apache-Coyote/1.1
Content-Type: application/xml;charset=ISO-8859-1
Content-Language: en-US
Content-Length: 3251
Date: Thu, 14 Oct 2010 23:22:49 GMT
Connection: close

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><myErrorEnvelope><internalCode>123</internalCode><description>error details</description></myErrorEnvelope>
Run Code Online (Sandbox Code Playgroud)

我有一个客户端(spring MVC 3.0 REST客户端),我正在尝试捕获HTTP 500并读取响应的主体并反序列化myErrorEnvelope对象.我首先捕获一个捕获RestClientException,它正确告诉我有一个HTTP 500响应,但然后似乎没有办法获得响应的正文.这是我不应该做的事情吗?我应该将错误对象作为HTTP 200响应的主体返回吗?我真的宁愿用身体返回HTTP 500.

谢谢.

http spring-mvc resteasy

11
推荐指数
1
解决办法
6647
查看次数

Spring Data mongodb:为MongoDb访问添加凭据

我的Spring应用程序中有以下工作配置:

<mongo:mongo id="myRs" replica-set="myhost:27017">

  <mongo:options max-wait-time="1500"
                   connect-timeout="30000" />
</mongo:mongo>

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <property name="writeResultChecking" value="EXCEPTION"/>
    <property name="writeConcern" value="FSYNC_SAFE"/>
    <constructor-arg ref="myRs"/>
    <constructor-arg name="databaseName" value="mydb"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

现在,我想要做的就是设置用户名/密码来访问mongo数据库,而无需更改我的代码(即只更新Spring应用程序上下文xml文件).那可能吗?如果是这样,怎么样?

谢谢.

xml credentials mongodb spring-data-mongodb

3
推荐指数
1
解决办法
6849
查看次数

Spring Boot特定于环境的配置

我有一个使用执行器,自动配置和JPA的弹簧启动应用程序.我希望能够在我的测试配置文件中使用内存数据库,在开发期间使用MySQL数据库配置,并在应用程序部署到生产环境时使用单独的生产数据库配置.据推测,从java命令行我应该能够指定环境,并且将拾取application.properties(或.yml)中的正确配置文件或配置块.

我没有找到一个好的帖子,其中的例子描述了如何进行这种切换,所以我想我会问是否有人有一个很好的例子.我的主要目标是在构建时预先定义spring.datasourcespring.jpa属性,然后在运行时使用java命令行参数"动态"切换每个环境的应用程序配置.次要目标是对management配置等做同样的事情.

谢谢.

environment jpa spring-mvc spring-boot microservices

3
推荐指数
1
解决办法
8554
查看次数

Spring-JPA可以与Postgres分区一起使用吗?

我们有一个Spring Boot项目,该项目使用Spring-JPA进行数据访问。我们有几个表,它们在其中一次(或几次,都在几分钟之内)创建/更新行。我们不会更新超过一天的行。这些表(例如审核表)可能会变得非常大,我们希望使用Postgres的表分区功能来按月分解数据。因此,主表始终具有该日历月的数据,但是如果查询需要从前几个月进行检索,它将以某种方式从其他分区读取。

两个问题:

1)这是归档较旧数据的好主意,但仍使其可查询吗?2)Spring-JPA是否可以用于分区表?还是我们必须弄清楚如何分解查询,进行本机查询并连接restult集?

谢谢。

postgresql partitioning spring-data spring-data-jpa spring-boot

3
推荐指数
1
解决办法
2135
查看次数

在tomcat上更改spring mvc app的app root

我正在使用Spring MVC 3.0上的示例RESTEasy 2.0资源并使用Tomcat 6.我可以通过http://localhost:8080/examples-resteasy-2.1-SNAPSHOT/contacts访问我的资源但我希望通过http:// localhost:8080/contacts甚至http:// localhost:8080/myservice/contacts

我的应用程序映射到路径的方式是否需要更改?

在web.xml

<web-app>

    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/contacts/*</url-pattern>
    </servlet-mapping>

</web-app>
Run Code Online (Sandbox Code Playgroud)

用SpringMVC-servlet.xml中

<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/context http: //www.springframework.org/schema/context/spring-context-3.0.xsd
        http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans.xsd
    ">
    <context:component-scan base-package="org.jboss.resteasy.examples.springmvc" />
    <context:annotation-config />
    <import resource="classpath:springmvc-resteasy.xml" />  <!-- this is included in the resteasy-spring library-->

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>
Run Code Online (Sandbox Code Playgroud)

我的RESTEasy资源类

@Controller
@Path("/contacts")
public class ContactsResource { …
Run Code Online (Sandbox Code Playgroud)

spring tomcat

1
推荐指数
1
解决办法
6622
查看次数