Tag: spring

Spring + JPA:不从数据库中获取对象

我正在实施一个在线商店。 我有以下代码来保存订单: @Service @Transactional public class OrderServiceImpl extends GenericServiceImpl implements OrderService { @Inject private ItemRepository itemRepository; @Override public void saveOrder(Order order) { this.updateItemsAccordingToOrderedQuantities(order); repository.save(order); } private void updateItemsAccordingToOrderedQuantities(Order order) { List orderedItems = order.getOrderedItems(); for (OrderedItem orderedItem : orderedItems) { // fetch from database Item item = itemRepository.findOne(orderedItem.getItem().getId()); item.reduceWeightInColdStoreBy(orderedItem.getWeight()); itemRepository.update(item); } } } 在实际保存订单之前,我更新了每个商品的“重量”属性(一些数量与此订单一起销售,因此剩下的更少)。 OrderedItem对象保存对Item的引用,但我想从数据库中获取新的Item […]

Spring Boot – 从依赖jar加载application.properties/yml

我有一个Spring Boot应用程序,我想按特定顺序将值注入@ConfigurationProperties bean。 例如, @ConfigurationProperties(“myproperties”) class MyProperties { private String property1; …. } 基application.yml myproperties: property1: some-value 上面的类和属性文件位于jar文件中。 在我的Spring Boot应用程序的邮件应用程序(它具有上面的jar作为依赖项)中,我使用了@PropertySource(value = { “application.yml”, “base-application.yml”})但在MyProperties得到了null值。 我试过了 @PropertySources({ @PropertySource(“classpath:application.yml”), @PropertySource(“classpath*:base-application.yml”) }) 同样,但这也不起作用。 如果我在application.yml中添加myproperties.property1值,那么它可以正常工作。 是否可以从另一个jar内的属性文件中注入属性值? 如果是这样,我在这里做错了什么?

在打包到jar后,Spring启动应用程序不提供静态资源

我有一个应用程序,通过ide或命令行启动时工作得很好:mvn spring-boot:run。 但是当我将它打包到jar中时,我无法访问静态资源(未找到404)。 我不想将静态文件存储在资源漏洞中,所以每次我需要更改静态文件时都不必重新加载服务器。 所以我在我的pom.xml中使用了这个插件: maven-resources-plugin 2.6 copy-resources validate copy-resources ${basedir}/target/classes/static src/main/webapp true 我可以看到文件正在两个目录“static”中复制。 这是我的资源处理程序配置: @Configuration @EnableWebMvc public class WebMvcConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler(“/**”).addResourceLocations(“/”); } 控制器RequestMappings工作正常,问题只与静态资源有关。

Spring:在启动时初始化多个连接池

我想通过从数据库表中读取连接参数来在上下文启动时初始化多个连接池。 基本上我想解决以下两件事 从数据库读取连接属性而不是属性文件。 它们是多个连接池(db中的行)详细信息。 所以我的问题是如何在spring-context文件中迭代数据库返回的行列表并创建多个数据源对象并使用唯一键存储它们(让我们说在地图中)? 数据库表结构有点像: + ————– + —————- + ————— + | DBSERVERNAME | DBDRIVERCLASS | DBMINPOOLSIZE | + ————– + —————- + ————— + | Server1 | Mysql-Driver | 10 | | Server2 | Oracle驱动程序| 20 | | Server3 | DB2-Driver | 10 | + ————– + —————- + ————— + 如果需要更多细节,请告诉我。 […]

JSP表单:复选框到ac:foreach

本论坛的许多post都提到了类似的问题; 但没有人有特定的解决方案,我感谢你帮助我: 我正在使用spring开发一个Web应用程序,我不知道我应该在表单的路径中放置什么:checkbox标签里面的c:foreach one,这是我的代码: Download Delete ‘ rights ‘是Spring文档中定义的字符串列表,它有一个getter和一个像其他属性一样的setter,我的复选框在c:foreach标签之外工作,但是当它们包含在这个标签中时会生成这个exception: org.springframework.beans.NotReadablePropertyException: Invalid property ‘person’ of bean class [java.util.ArrayList]: Bean property ‘person’ is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter? 你对这个问题有什么看法吗?

如何在Spring中将对象从一个控制器传递到另一个控制器而不使用Session

我有一个要求,用户从表单中选择一些数据,我们需要在下一页显示所选数据。 目前我们使用会话属性执行此操作,但问题是如果第一页在另一个浏览器选项卡中打开,它将覆盖数据,其中再次选择和提交数据。 所以我只想在将数据从一个控制器传输到另一个控制器时摆脱这个会话属性。 注意:我使用的是基于XML的Spring配置,因此请使用XML而不是注释来显示解决方案。

刷新Spring的属性占位符机制

我有一个Spring上下文xml文件和一个由PropertyPlaceholderConfigurer机制创建的特定bean。 我的问题是:当应用程序正在运行相关的属性文件更改时,我不想重启应用程序。 我想重新加载相关的bean没有重启应用程序。 什么是我的目标的最佳解决方案?

如何在启动SLF4J之前运行一个类 – Logback?

正如标题所说,我想运行一个类,它将在启动logback之前检查目录中是否存在属性。 所以我试过的是我在spring的监听器之前放置一个将在启动时启动的监听器,并且我确保在该监听器中没有使用Logger。 但是Logger仍然设法在听众面前解雇。 这是我的web.xml: SekaiServer listener.InitLog listener.InitConfig listener.InitListener contextConfigLocation classpath:spring/application-config.xml /WEB-INF/security-context.xml org.springframework.web.context.ContextLoaderListener dispatcherServlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation /WEB-INF/mvc-config.xml 1 dispatcherServlet / springSecurityFilterChain org.springframework.web.filter.DelegatingFilterProxy springSecurityFilterChain /*

如何通过Spring框架从数据库加载应用程序属性(v4.0.3)

我试图找出如何通过Spring(4.0.3)从数据库表加载我的所有应用程序属性。 现在我的应用程序有一组属性文件(大约十几个)。 这些属性文件是每个环境的重复(而不是值)。 拿下面: config.jar 开发 inErrorCodes.properties outErrCodes.properties report.properties email.properties 测试 inErrorCodes.properties outErrCodes.properties report.properties email.properties 刺 inErrorCodes.properties outErrCodes.properties report.properties email.properties 这是xml配置的一个片段: 然后在源文件中使用: … import javax.inject.Inject; import javax.inject.Named; @Named(“testService”) public class TestServiceImpl implements TestService { private Properties inboundErrorCodes = null; private Properties outboundErrorCodes = null; private Properties reportProperties = null; private Properties emailProperties = null; @Inject […]

如何在spring mvc控制器中使用junit返回类型的方法

我在我的Spring MVC控制器上做junit – @RequestMapping(value = “index”, method = RequestMethod.GET) public HashMap handleRequest() { HashMap model = new HashMap(); String name = “Hello World”; model.put(“greeting”, name); return model; } 以下是我对上述方法的说法 – public class ControllerTest { private MockMvc mockMvc; @Before public void setup() throws Exception { this.mockMvc = standaloneSetup(new Controller()).build(); } @Test public void test01_Index() { try […]