Questions tagged «java»

Java是一种流行的高级编程语言。如果您在使用或理解语言本身时遇到问题,请使用此标签。这个标签很少单独使用,最常与[spring],[spring-boot],[jakarta-ee],[android],[javafx],[gradle]和[maven]结合使用。

13
如何修复Hibernate LazyInitializationException:无法延迟初始化角色集合,无法初始化代理-没有会话
在我的spring项目的自定义AuthenticationProvider中,我尝试读取已记录用户的权限列表,但遇到以下错误: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.horariolivre.entity.Usuario.autorizacoes, 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.read(AbstractPersistentCollection.java:124) at org.hibernate.collection.internal.PersistentBag.iterator(PersistentBag.java:266) at com.horariolivre.security.CustomAuthenticationProvider.authenticate(CustomAuthenticationProvider.java:45) at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:156) at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:177) at org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.attemptAuthentication(UsernamePasswordAuthenticationFilter.java:94) at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:211) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:57) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) …


8
字节顺序标记破坏了Java中的文件读取
我正在尝试使用Java读取CSV文件。有些文件的开头可能有字节顺序标记,但不是全部。如果存在,字节顺序将与第一行的其余部分一起读取,从而导致字符串比较出现问题。 是否存在一种跳过字节顺序标记的简单方法? 谢谢!

15
如何在Mac OSX上使用JDK 7?
我想使用此链接中提到的WatchService API:http : //download.oracle.com/javase/tutorial/essential/io/notification.html 阅读后,我发现WatchService是计划用于JDK 7的NIO类的一部分。因此,它处于beta形式。没关系。 http://jdk7.java.net/download.html具有我下载和提取的JDK。我有一堆文件夹。我不知道该怎么办。 然后,我阅读了更多内容,发现一些不错的人将JDK 7创建为二进制文件,以便像我这样的人可以轻松地安装它。它被称为Open JDK:http : //code.google.com/p/openjdk-osx-build/ 因此,我下载了.dmg文件并安装了它。然后,我打开“ Java首选项”,然后看到OpenJDK7可用。 因此,现在我可以开始尝试使用WatchService API。在第一个链接的教程中,作者提供了一个.java文件,以便首先对其进行测试并确保其正在运行。这是文件的链接:http : //download.oracle.com/javase/tutorial/essential/io/examples/WatchDir.java 因此,我启动了Eclipse(实际上我使用STS)并创建了一个新的Java项目,并在“使用执行环境JRE:”中选择了JaveSE-1.7。在src文件夹下,我复制粘贴了WatchDir.java文件。 而且我仍然看到成堆的红色波浪线。所有的“ import.java.nio。*”都为红色,我无法将其作为Java应用程序运行。 我需要做什么?
107 macos  java  java-7 

4
Java中的JPanel填充
我的Java swing应用程序有一个格式问题。它应该很简单,但是我很难找到任何帮助(每个主题似乎都与删除 JPanel中的默认填充有关)。我的各个JPanels中的文本都拥抱侧面和顶部,触及彩色边框:如何添加填充?谢谢。
107 java  swing  formatting  jpanel 

4
用Scala中的Option包装Java中的null返回方法?
假设我有一个方法,session.get(str: String): String但是您不知道它会返回字符串还是null,因为它来自Java。 在Scala中,有没有更简单的方法来解决此问题session.get("foo") == null?也许有些魔术适用ToOption(session.get("foo")),然后我可以像这样在Scala中对待它 ToOption(session.get("foo")) match { case Some(_) =>; case None =>; }
107 java  scala  scala-option 

11
接口中静态方法和默认方法之间的区别
当我发现您现在可以在接口中定义静态和默认方法时,我正在通过接口进行学习。 public interface interfacesample2 { public static void method() { System.out.println("hello world"); } public default void menthod3() { System.out.println("default print"); } } 请解释两者的区别,并且如果有一个示例说明何时使用它会很好。在接口上有些困惑。
107 java  java-8 

18
没有互联网连接时,Spring schemaLocation失败
我正在使用Spring,并且application-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:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:security="http://www.springframework.org/schema/security" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.xsd" > ..... 当我的互联网连接断开时,我无法通过tomcat或码头运行我的应用程序。 它给: [main] WARN org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Ignored XML validation warning org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document '/spring-beans-2.0.xsd', because 1) could not …
107 java  xml  spring  xsd 


16
Java中有秒表吗?
Java中有秒表吗?在Google上,我只找到无法使用的秒表代码-它们总是返回0毫秒。 我发现的这段代码行不通,但我不明白为什么。 public class StopWatch { private long startTime = 0; private long stopTime = 0; private boolean running = false; public void start() { this.startTime = System.currentTimeMillis(); this.running = true; } public void stop() { this.stopTime = System.currentTimeMillis(); this.running = false; } //elaspsed time in milliseconds public long getElapsedTime() …
107 java  stopwatch 



6
如何在Spring Security / SpringMVC中手动设置经过身份验证的用户
新用户提交“新帐户”表单后,我想手动登录该用户,这样他们就不必在后续页面上登录。 通过spring安全拦截器的普通表单登录页面工作正常。 在新帐户形式的控制器中,我正在创建一个UsernamePasswordAuthenticationToken并在SecurityContext中手动进行设置: SecurityContextHolder.getContext().setAuthentication(authentication); 稍后,我在同一页面上检查用户的登录身份: SecurityContextHolder.getContext().getAuthentication().getAuthorities(); 这将返回我之前在身份验证中设置的权限。一切都很好。 但是,在我加载的下一页上调用相同的代码时,身份验证令牌只是UserAnonymous。 我不清楚为什么它没有保留我在上一个请求中设置的身份验证。有什么想法吗? 可能与会话ID设置不正确有关吗? 是否有某种方式可能会覆盖我的身份验证? 也许我还需要保存身份验证的另一步骤? 还是我需要做一些事情来在整个会话中声明身份验证,而不是某种程度上的单个请求? 只是寻找一些想法可以帮助我了解这里发生的事情。

7
Android-启动时启动服务
从我在Stack Exchange和其他地方看到的所有内容中,我已经正确设置了所有内容,可以在启动Android OS时启动IntentService。不幸的是,它没有在启动时启动,并且我没有收到任何错误。也许专家可以帮忙... 表现: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.phx.batterylogger" android:versionCode="1" android:versionName="1.0" android:installLocation="internalOnly"> <uses-sdk android:minSdkVersion="8" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.BATTERY_STATS" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <service android:name=".BatteryLogger"/> <receiver android:name=".StartupIntentReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> </application> </manifest> 用于启动的BroadcastReceiver: package com.phx.batterylogger; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public …


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.