在Spring MVC应用程序中运行JUnit Test时出现问题.测试1(insertTweet)似乎运行正常,但是在测试2中我得到一个"LazyInitializationException"异常(参见下面的完整stactrace).我明白为什么它被抛出但不确定为什么会话正在关闭以及如何在每次测试2开始时重新打开它(或保持现有会话打开以完成剩余的测试)?我已经粘贴了与Test Classes一起抛出的整个StackTrace.
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.project.user.User.tweets, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:566)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:186)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:545)
at org.hibernate.collection.internal.AbstractPersistentCollection.write(AbstractPersistentCollection.java:370)
at org.hibernate.collection.internal.PersistentBag.add(PersistentBag.java:291)
at com.project.core.tweet.Tweet.<init>(Tweet.java:113)
at com.project.core.service.impl.FanoutServiceTester.insertTweet(FanoutServiceTester.java:69)
at com.project.core.service.impl.FanoutServiceTester.testInsertRetweet(FanoutServiceTester.java:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) …Run Code Online (Sandbox Code Playgroud) 客户端定期调用异步方法(长轮询),向其传递一个股票代码的值,服务器使用该值来查询数据库并将对象返回给客户端.
我正在使用Spring的DeferredResult类,但是我不熟悉它的工作原理.请注意我如何使用symbol属性(从客户端发送)来查询数据库以获取新数据(见下文).
也许有更好的方法用于Spring的长轮询?
如何将symbol属性从方法deferredResult()传递给processQueues()?
private final Queue<DeferredResult<String>> responseBodyQueue = new ConcurrentLinkedQueue<>();
@RequestMapping("/poll/{symbol}")
public @ResponseBody DeferredResult<String> deferredResult(@PathVariable("symbol") String symbol) {
DeferredResult<String> result = new DeferredResult<String>();
this.responseBodyQueue.add(result);
return result;
}
@Scheduled(fixedRate=2000)
public void processQueues() {
for (DeferredResult<String> result : this.responseBodyQueue) {
Quote quote = jpaStockQuoteRepository.findStock(symbol);
result.setResult(quote);
this.responseBodyQueue.remove(result);
}
}
Run Code Online (Sandbox Code Playgroud) spring spring-mvc long-polling http-streaming server-sent-events
在@Service类中有几个用@Value注释的字段.这些字段未正确填充并且为空.也许我忽略了一些事情,我已经粘贴了下面的相关代码块.尝试了具有相同结果的替代选项env.getProperty().
输出中以下属性的值为null.
package com.project.service.impl;
import org.springframework.beans.factory.annotation.Value
@Service("aService")
@PropertySource(value="classpath:app.properties")
public class ServiceImpl implements Service{
private Environment environment;
@Value("${list.size}")
private Integer list1;
@Value("${list2.size}")
private Integer list2Size;
@Autowired
public ServiceImpl(StringRedisTemplate stringTemplate){
this.stringTemplate = stringTemplate;
logger.info("TESTING 123: "+list1);
}
// ...
}
@EnableWebMvc
@ComponentScan(basePackages = {"com.project.service","..."})
@Configuration
public class ServletConfig extends WebMvcConfigurerAdapter {
// ...
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
Resource[] resources = new ClassPathResource[] {
new ClassPathResource("app.properties")
};
propertyPlaceholderConfigurer.setLocations(resources);
propertyPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(true);
return propertyPlaceholderConfigurer;
}
}
Run Code Online (Sandbox Code Playgroud) 在扩展AbstractAnnotationConfigDispatcherServletInitializer类时,如何设置"活动配置文件"属性?
用 SliverFillRemaining 包装 TabBarView(填充剩余的空白空间,如 Expanded)给出以下错误输出。
颤振:RenderPositionedBox 期望 RenderBox 类型的子级,但收到颤振类型的子级:RenderSliverList。
TabController tabContoller;
@override
void initState() {
tabContoller = new TabController(
vsync: this,
length: 3,
);
@override
Widget build(BuildContext context) {
return new Scaffold(
floatingActionButton:floatActionBtn(...),
bottomNavigationBar: bottomNavigationBar(...),
appBar: apBar(),
body: Stack(
children: <Widget>[
CustomScrollView(
slivers: <Widget>[
SliverAppBar(
backgroundColor: Colors.transparent,
automaticallyImplyLeading: false,
expandedHeight: 195.0,
flexibleSpace: FlexibleSpaceBar(
background: new Stack(
children: <Widget>[
...
]),
),
),
new SliverFillRemaining(
child: TabBarView(
controller: tabContoller,
children: <Widget>[
Tab(...),
Tab(...),
Tab(...)
],
),
),
],
),
],
)
Run Code Online (Sandbox Code Playgroud) 我需要将 web.xml 的内容复制到 WebAppInitializer.class(Java 配置类)。我已经从 web.xml 复制了 YahooFilter 类(请参阅代码),但我不确定如何实用地添加 init-params。
我已在下面粘贴了 Java 配置类的 web.xml 和片段。有人可以看一下并提供一些反馈吗?
<web-app>
<display-name>sample</display-Aname>
<filter>
<filter-name>YOSFilter</filter-name>
<filter-class>com.yahoo.yos.YahooFilter</filter-class>
<!--
optional param -
underlying oauth client class
possible values:
net.oauth.client.URLConnectionClient (default)
net.oauth.client.httpclient3.HttpClient3
net.oauth.client.httpclient4.HttpClient4
-->
<init-param>
<param-name>oauthConnectionClass</param-name>
<param-value>net.oauth.client.httpclient4.HttpClient4</param-value>
</init-param>
<!--
optional param -
redirect end-user if an access token is not found, set to false if you
are only making two-legged oauth calls e.g. oauth calls without an
access token to retrieve public information
defauts to true
-->
<init-param>
<param-name>redirect</param-name> …Run Code Online (Sandbox Code Playgroud) 尝试从 ListTile 小部件中删除左右填充,以便元素看起来更靠近边缘。我已将两侧的属性“contentpadding”设置为 0,这在纵向模式下工作正常,但当我切换到横向时,填充仍然可见。我在下面提供了一个示例应用程序。
复制/粘贴到新文件并运行,请注意在纵向视图中具有黑色背景的元素看起来更靠近边缘,现在切换到横向以查看差异。contentpadding 属性似乎被忽略。
import 'package:flutter/material.dart';
void main() {
runApp(TestApp());
}
class TestApp extends StatefulWidget {
_TestAppState createState() => new _TestAppState();
}
class _TestAppState extends State<TestApp> {
@override
Widget build(BuildContext context) {
return new MaterialApp(
debugShowCheckedModeBanner: false,
home: new Tester(),
)
;
}
}
class Tester extends StatefulWidget {
Tester({Key key}) : super(key: key);
_TesterState createState() => new _TesterState();
}
class _TesterState extends State<Tester>
with SingleTickerProviderStateMixin {
final TextEditingController searchQuery = new TextEditingController();
ScrollController _scrollController;
List<String> items;
TabController …Run Code Online (Sandbox Code Playgroud) spring ×5
spring-mvc ×4
spring-data ×3
flutter ×2
dart ×1
hibernate ×1
jpa-2.0 ×1
junit ×1
long-polling ×1