我想知道如何初始化Spring Beans中的字段?这有几种可能的解决方案:
1.直接在声明上初始化字段
import org.springframework.stereotype.Component;
@Component
public class DeclarationInit {
private final int field = Integer.MAX_VALUE;
public int getField() {
return field;
}
}
Run Code Online (Sandbox Code Playgroud)
2.使用@Value注释初始化字段
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class ValueInit {
@Value("#{T(Integer).MAX_VALUE}")
private int field;
public int getField() {
return field;
}
}
Run Code Online (Sandbox Code Playgroud)
3.使用@Autowired注释初始化字段
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class AutowiredInit {
private int field;
@Autowired
private void initField() {
field = Integer.MAX_VALUE;
}
public int getField() {
return field;
}
} …Run Code Online (Sandbox Code Playgroud) java spring dependency-injection autowired in-class-initialization
我需要一些帮助来了解发生此错误的位置:
警告:非静态数据成员的类内初始化是C++ 11扩展[-Wc ++ 11-extensions]
这是它来自的代码部分:
typedef struct Hand {
bool straight = false;
bool flush = false;
bool four = false;
bool three = false;
int pairs = 0;
} Hand;
Run Code Online (Sandbox Code Playgroud) 以下代码是否有效,例如是否带来未定义的行为?
struct S
{
int i = s.i;
static S s;
};
S S::s;
int main()
{
S a; // a.i = 0
S::s.i = 42;
S b; // b.i = 42
}
Run Code Online (Sandbox Code Playgroud)
据我所知,所有具有静态存储持续时间的变量都是零初始化.因此s.i是0在S::s创造,一切都是好的.但也许我错过了一些东西.
c++ initialization static-members language-lawyer in-class-initialization
我已经读过了std :: unique_ptr <T>需要知道T的完整定义吗?和unique_ptr转发声明?,但我的问题更具体.
以下编译:
// Compile with $ g++ -std=c++11 -c <filename>
#include <memory>
class A; // fwd declaration
class AUser
{
AUser(); // defined elsewhere
~AUser(); // defined elsewhere
std::unique_ptr<A> m_a;
};
Run Code Online (Sandbox Code Playgroud)
以下不是:
// Compile with $ g++ -std=c++11 -c <filename>
#include <memory>
class A; // fwd declaration
class AUser
{
AUser(); // defined elsewhere
~AUser(); // defined elsewhere
std::unique_ptr<A> m_a{nullptr};
};
Run Code Online (Sandbox Code Playgroud)
错误
$ g++ -std=c++11 -c fwd_decl_u_ptr.cpp
In file included from /usr/include/c++/4.7/memory:86:0,
from …Run Code Online (Sandbox Code Playgroud) 特定
struct X {};
constexpr auto x = X{};
struct S {
static constexpr auto& rx = x;
};
Run Code Online (Sandbox Code Playgroud)
gcc 4.8说
错误:非常量的类内初始化对静态成员'S :: rx'无效
static constexpr auto& rx = x;
^
Run Code Online (Sandbox Code Playgroud)
错误:(需要进行类外初始化)
错误:'S :: rx'在声明时无法通过非常量表达式初始化
我希望x它是一个常量表达式,适合这种初始化.这是一个gcc bug吗?如果没有,这里发生了什么?
c++ static-members constant-expression in-class-initialization c++11
假设我有一个unique_ptr我想在课堂初始化的成员对象,请参阅下面的代码.为什么我必须使用统一初始化(花括号)?第二个声明吐出一个错误,比如说
so.cpp:10:31: error: expected parameter declarator
std::unique_ptr<Foo> upf2(new Foo);
^
so.cpp:10:31: error: expected ')'
so.cpp:10:30: note: to match this '('
std::unique_ptr<Foo> upf2(new Foo); ^
2 errors generated.
Run Code Online (Sandbox Code Playgroud)
而且我认为这不是一个最令人烦恼的解析问题,至少我不相信.
#include <memory>
class Foo
{
};
class Bar{
std::unique_ptr<Foo> upf1{new Foo}; // works fine
// std::unique_ptr<Foo> upf2(new Foo); // error here
};
int main()
{
Bar bar;
}
Run Code Online (Sandbox Code Playgroud) 刚刚学习Kotlin在下面的第一个代码val中,其他代码中没有关键字,没有,如果val和var被省略,这里有什么不同?
class Person(val firstName: String, val lastName: String) {
}
class Person(firstName: String, lastName: String) {
}
Run Code Online (Sandbox Code Playgroud) 我最近发现你不能同时在类初始化和初始化列表中.以下代码失败:
struct s
{
int i=0;
};
int main() {
s s1; //s1.i = 0
//s s2={42}; //fails
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我删除了类初始化,初始化列表工作正常!
有人可以解释我为什么不允许这样的事情?
有没有办法直接检索成员的默认值,该默认值是使用类内初始化定义的?例如:
struct Test
{
int someValue = 5;
};
int main(int argc,char *argv[])
{
auto val = declvalue(Test::someValue); // Something like this; Should return 5
std::cout<<val<<std::endl;
for(;;);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
基本上是"复制"(类似于decltype)整个声明的东西,包括默认值.这样的事情存在吗?
为什么在这个例子中
struct Foo {
atomic<int> x = 1;
};
Run Code Online (Sandbox Code Playgroud)
编译器(gcc 4.8)试图使用atomic& operator=(const atomic&)被删除的(因此示例不会编译),而这里
struct Bar {
Bar() { x = 1; }
atomic<int> x;
};
Run Code Online (Sandbox Code Playgroud)
它int operator=(int)按预期调用了吗?
PS:我已经知道了
struct Xoo {
atomic<int> x{1};
};
Run Code Online (Sandbox Code Playgroud)
很好(无论如何是更好的初始化方式x),但我仍然很好奇为什么会Foo被打破.
PS:我误读了编译器错误(并且忘了将它包含在问题中).它实际上说:
error: use of deleted function ‘std::atomic<int>::atomic(const std::atomic<int>&)’
std::atomic<int> x = 1;
^
[...] error: declared here
atomic(const atomic&) = delete;
^
Run Code Online (Sandbox Code Playgroud)
所以我上面的陈述"......试图使用这atomic& operator=(const atomic&)只是完全错误的.