在这篇文章中,我们将为您详细介绍Springio@Autowired:空白的final字段可能尚未初始化的内容,并且讨论关于处于空白的相关问题。此外,我们还会涉及一些关于@AutowiredinSpr
在这篇文章中,我们将为您详细介绍Spring io @Autowired:空白的final字段可能尚未初始化的内容,并且讨论关于处于空白的相关问题。此外,我们还会涉及一些关于@Autowired in Spring PermissionEvaluator、delphi – 有关变量的缺失警告可能尚未初始化、java – 三个参数运算符:局部变量可能尚未初始化、Java-变量可能尚未初始化错误的知识,以帮助您更全面地了解这个主题。
本文目录一览:- Spring io @Autowired:空白的final字段可能尚未初始化(处于空白)
- @Autowired in Spring PermissionEvaluator
- delphi – 有关变量的缺失警告可能尚未初始化
- java – 三个参数运算符:局部变量可能尚未初始化
- Java-变量可能尚未初始化错误
Spring io @Autowired:空白的final字段可能尚未初始化(处于空白)
我认为这是一个非常基本的问题-
关于此错误,有几种类型的问题,但在前5个结果中没有一个具有Spring的细微差别。
我有一个在spring编写的REST-ful webapp的开始。我正在尝试将其连接到数据库。
我有一个名为Workspace的实体,正在尝试使用bean的弹簧注入(正确的术语?)来保存工作区实体的实例
package com.parrit;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.util.Assert;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;import com.parrit.models.Workspace;import com.parrit.models.WorkspaceRepository;@RestController@RequestMapping("/workspace")public class WorkspaceController { @Autowired private final WorkspaceRepository repository; @RequestMapping(method = RequestMethod.POST) void save( @RequestBody String workspaceHTML) { Workspace ws = new Workspace(); ws.setHTML(workspaceHTML); repository.save(ws); }}
我的错误是在存储库变量上private final WorkspaceRepositoryrepository
。编译器抱怨它可能没有初始化,并且尝试运行该应用程序会产生相同的结果。
如何在我的控制器中获取此存储库对象的实例,以便对其执行保存操作?
答案1
小编典典在@Autowired
球场上拥有和决赛是矛盾的。
后者表示:此变量只有一个值,并且在构造时初始化。
前者说:Spring将构造对象,将此字段保留为空(其默认值)。然后,Spring将使用反射类型为WorkspaceRepository的bean初始化此字段。
如果要自动绑定最终字段,请使用构造函数注入,就像您自己进行注入一样:
@Autowiredpublic WorkspaceController(WorkspaceRepository repository) { this.repository = repository;}
@Autowired in Spring PermissionEvaluator
首先,我已经广泛搜索了这个,虽然似乎有一个固定的地方我无法成功引用PermissionEvaluator中注入的@Bean:
https://jira.springsource.org/browse/SEC-2136?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
在该问题的评论部分,Rob Winch提供了一个解决方案的建议
to work around this issue,you can proxy your permissionEvaluator using LazyInitTargetSource
话虽这么说,我在实现发布的XML的基于注释的JavaConfig版本时遇到了麻烦.我正在使用Spring Boot 1.0.0.BUILD-SNAPSHOT和spring-boot-starter-security.
我有一个类来配置方法安全性,如下所示:
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
@Override
protected MethodSecurityExpressionHandler createExpressionHandler() {
DefaultMethodSecurityExpressionHandler expressionHandler = new DefaultMethodSecurityExpressionHandler();
expressionHandler.setPermissionEvaluator(new MyPermissionEvaluator());
expressionHandler.setParameterNamediscoverer(new SimpleParameterdiscoverer());
return expressionHandler;
}
}
并且PermissionEvaluator的开始:
public class MyPermissionEvaluator implements PermissionEvaluator {
private static final Logger LOG = LoggerFactory.getLogger(MyPermissionEvaluator.class);
@Autowired
private UserRepository userRepo;
@Override
public boolean hasPermission(Authentication authentication,Object targetDomainObject,Object permission) {
if (authentication == null || !authentication.isAuthenticated()) {
return false;
}
if (permission instanceof String) {
switch((String) permission) {
case "findUser":
return handleUserPermission(authentication,targetDomainObject);
default:
LOG.error("No permission handler found for permission: " + permission);
}
}
return false;
}
@Override
public boolean hasPermission(Authentication authentication,Serializable targetId,String targettype,Object permission) {
throw new RuntimeException("Id-based permission evaluation not currently supported.");
}
private boolean handleUserPermission(Authentication auth,Object targetDomainObject) {
if (targetDomainObject instanceof Long) {
boolean hasPermission = userRepo.canFind((Long) targetDomainObject);
return hasPermission;
}
return false;
}
}
需要做什么才能从PremissionEvaluator中获取对UserRepository的引用?我尝试了各种变通方法,但没有成功.似乎没有任何东西可以@Autowired到PermissionEvaluator ……
delphi – 有关变量的缺失警告可能尚未初始化
procedure TfrmCageSetup.actDeleteFloatExecute(Sender: TObject); var conn: TADOConnection; begin ... if not float.CanDelete(conn,{out}noDeleteReason) then begin ... end;
我忘了初始化变量conn.结果,它是堆栈垃圾.当目标被调用者检查参数时,它会通过:
function TFloat.CanDelete(Connection: TADOConnection; out NoDeleteReason: string): Boolean; begin if Connection = nil then raise EArgumentNullException.Create('Connection');
我有时会遇到非常奇怪的访问冲突.
但为什么Delphi没有抓住它呢?
是的,这是我的错.但部分原因是我使用静态的,强类型的语言,这样可以帮助我捕捉这些愚蠢的错误.
首先,我检查了我实际上并没有将它初始化为某些损坏或被破坏的物体(也许它已被初始化 – 只是非常糟糕).但不,它在声明和首次使用之间真的没有初始化:
然后我想也许我已经关闭了警告:
Variable might not have been initialized
但不,它已启用(全局,对于我的平台,我的发布类型):
所以然后我再次构建它(即使这个bug在代码中存在了几个月),以确保我没有错过错误.但不是;除了VCL和JVCL中的一些错误外,Delphi没有发现错误:
我希望德尔福帮助我
有什么可以解释这个?
我想知道我的代码中是否还有其他地方有相同,可怕,完全可检测的错误.
也许它是64位编译器.有些人抱怨64位后端(全新)并不像32位后端那样聪明.如果我尝试将其更改为32位怎么办?
仍然没有:
与32位版本和64位版本相同.
2015年1月25日 – 它是否显示出这样的警告?
重现警告的步骤确实显示:
procedure TForm1.FormCreate(Sender: TObject); var silverWarrior: Integer; begin IsWrong(silverWarrior); end; function TForm1.IsWrong(n: Integer): Boolean; begin Result := True; end;
发出警告:
W1036 Variable ‘silverWarrior’ might not have been initialized
您甚至不必将值传递给另一个函数;简单地使用未初始化的变量会发出警告(取决于特定日期编译器的情绪):
procedure TForm1.FormCreate(Sender: TObject); var silverWarrior: Integer; theAnswer: Integer; begin theAnswer := silverWarrior + 42; end;
发出警告:
W1036 Variable ‘silverWarrior’ might not have been initialized
超过32个局部变量导致失败?
没有;你仍然得到警告:
procedure TForm1.FormCreate(Sender: TObject); var localVariable1: Integer; localVariable2: Integer; localVariable3: Integer; localVariable4: Integer; localVariable5: Integer; localVariable6: Integer; localVariable7: Integer; localVariable8: Integer; localVariable9: Integer; localVariable10: Integer; localVariable11: Integer; localVariable12: Integer; localVariable13: Integer; localVariable14: Integer; localVariable15: Integer; localVariable16: Integer; localVariable17: Integer; localVariable18: Integer; localVariable19: Integer; localVariable20: Integer; localVariable21: Integer; localVariable22: Integer; localVariable23: Integer; localVariable24: Integer; localVariable25: Integer; localVariable26: Integer; localVariable27: Integer; localVariable28: Integer; localVariable29: Integer; localVariable30: Integer; localVariable31: Integer; localVariable32: Integer; localVariable33: Integer; localVariable34: Integer; localVariable35: Integer; localVariable36: Integer; localVariable37: Integer; localVariable38: Integer; localVariable39: Integer; localVariable40: Integer; localVariable41: Integer; localVariable42: Integer; localVariable43: Integer; localVariable44: Integer; localVariable45: Integer; localVariable46: Integer; localVariable47: Integer; localVariable48: Integer; localVariable49: Integer; silverWarrior: Integer; theAnswer: Integer; begin theAnswer := silverWarrior + 42; end;
W1036 Variable ‘silverWarrior’ might not have been initialized
解决方法
我没有找到任何模式.我没有解决方案.我有一个暗示控制语句如break和exit可能与这些问题有关,但我没有硬性证据就是这样.
只有Embarcadero可以解决这个问题,所以你能做的最好就是提交一份错误报告,希望Embarcadero的某个人能够关心.
java – 三个参数运算符:局部变量可能尚未初始化
import java.util.Random; public class ThreeArgumentOperator { private static final Random RANDOM = new Random(); public static void main(String[] args) { String test; System.out.println(test = getValue() == null ? "" : test); } public static String getValue() { if (RANDOM.nextBoolean()) { return ""; } else { return null; } } }
Eclipse编译器(我正在使用Juno)报告以下错误:
The local variable test may not have been initialized
我的问题是:在这种情况下,编译器是否应该报告它不能将boolean转换为String?我理解运算符==优先于=因此编译器应该抱怨转换,而是抱怨可能没有初始化值.
当我更改以下行时
System.out.println(test = getValue()== null?“”:test);
至
System.out.println((test = getValue())== null?“”:test);
一切正常.
编辑:我也尝试直接使用javac编译它.它给出了同样的错误.
error: variable test might not have been initialized System.out.println(test = getValue() == null ? "" : test);
解决方法
getValue() == null
为了继续,我们假设结果是错误的.下一个表达式如下:
false ? "" : test
结果就是测试.而我们最后的表达……
test = test
但测试从未初始化,因此错误.
Java-变量可能尚未初始化错误
如何解决Java-变量可能尚未初始化错误?
你声明了它们,但没有初始化它们。初始化它们是将它们设置为等于一个值:
int a; // This is a declaration
a = 0; // This is an initialization
int b = 1; // This is a declaration and initialization
因为未初始化变量,但a++
在for
循环中增加了变量(例如),因此会收到错误消息。
Java原语具有默认值,但如下一位用户所述
当声明为类成员时,它们的默认值为零。局部变量没有默认值
解决方法
当我尝试编译时:
public static Rand searchCount (int[] x)
{
int a ;
int b ;
...
for (int l= 0; l<x.length; l++)
{
if (x[l] == 0)
a++ ;
else if (x[l] == 1)
b++ ;
}
...
}
我得到这些错误:
Rand.java:72: variable a might not have been initialized
a++ ;
^
Rand.java:74: variable b might not have been initialized
b++ ;
^
2 errors
在我看来,我在方法的顶部初始化了它们。怎么了
今天关于Spring io @Autowired:空白的final字段可能尚未初始化和处于空白的讲解已经结束,谢谢您的阅读,如果想了解更多关于@Autowired in Spring PermissionEvaluator、delphi – 有关变量的缺失警告可能尚未初始化、java – 三个参数运算符:局部变量可能尚未初始化、Java-变量可能尚未初始化错误的相关知识,请在本站搜索。
本文标签: