据我所知,如果声明了变量Lazy,那么当我们使用该Value属性时会调用它的构造函数.
我需要将一些参数传递给此Lazy实例,但无法找到正确的语法.这不是我的设计,我正在使用MEF ExportFactory,它会返回我Lazy的部件实例.我的部分有构造函数,我需要用一些参数调用这些构造函数.
我有一个CDI Managed Bean(一个用@Named注释的bean,用于JSF),它注入了一个Stateful Session Bean.这个会话bean就像一个服务,它有实体管理器(用@PersistenceContext(type = PersistenceContextType.EXTENDED)注释)并公开来操作某些实体的方法.这些实体位于托管bean上,即ConversationScoped.然后,JSF调用托管bean的方法,托管bean调用"service"(有状态会话bean)的某个方法.我不知道这是不是最好的设计,但它运作良好.但是有一个实体有一些需要用LAZY获取的集合.第一次打开页面时,它似乎运行良好,但是当我尝试单击任何按钮或执行任何操作时,我都有LazyInitializationException.有人有任何提示吗?我不知道有什么不对劲.我把会话bean放在有状态,扩展持久化上下文.此会话bean在具有实体的托管bean中注入.为什么它会抛出这个例外?如何关闭实体经理?
这是陷入困境的实体的代码:
@Entity
public class ParametrosVingentes implements Serializable {
public static final String ID = "settings";
private static final long serialVersionUID = 1L;
@Id
private String id;
@OneToOne
private ValorHora valorHora;
@OneToMany
@JoinTable(
name="BuscaSistecVingentes",
joinColumns = @JoinColumn( name="parametros_vingentes_fk"),
inverseJoinColumns = @JoinColumn( name="agendamento_fk")
)
private List<AgendamentoBuscaSistec> agendamentosBuscaSistec;
@OneToMany
@JoinTable(
name="ExportacaoZeusVingentes",
joinColumns = @JoinColumn( name="parametros_vingentes_fk"),
inverseJoinColumns = @JoinColumn( name="agendamento_fk")
)
private List<AgendamentoExportacaoZeus> agendamentosExportacaoZeus;
@OneToMany
@JoinTable(
name="ImportacaoZeusVingentes",
joinColumns = @JoinColumn( name="parametros_vingentes_fk"),
inverseJoinColumns = @JoinColumn( name="agendamento_fk")
)
private …Run Code Online (Sandbox Code Playgroud) entitymanager stateful-session-bean lazy-initialization jsf-2 ejb-3.1
private static final long[] reservedFromIps;
static {
reservedFromIps = {0l, 167772160l, 1681915904l,
2130706432l, 2851995648l, 2886729728l, 3221225984l, 3227017984l, 3232235520l,
3323068416l, 3325256704l, 3405803776l, 3758096384l, 4026531840l, 4294967295l};
}
Run Code Online (Sandbox Code Playgroud)
错误是"表达式的非法启动,而不是声明,;预期"
而以下工作正常:
private static final long[] reservedFromIps = {0l, 167772160l, 1681915904l,
2130706432l, 2851995648l, 2886729728l, 3221225984l, 3227017984l, 3232235520l,
3323068416l, 3325256704l, 3405803776l, 3758096384l, 4026531840l, 4294967295l};
Run Code Online (Sandbox Code Playgroud) 我正在努力 UITableViewController
@interface GinkgoDeliveryOrdersTableViewController : UITableViewController
@property PFQuery * query;
@property NSArray * products;
@end
Run Code Online (Sandbox Code Playgroud)
我该如何初始化这两个属性?目前,我正在做懒惰的初始化:
@implementation GinkgoDeliveryOrdersTableViewController
@synthesize query = _query;
@synthesize products = _products;
- (void) initQuery {
_query = [PFQuery queryWithClassName:@"_Product"];
}
- (void) initProducts {
if(! _query)
[self initQuery];
_products = [_query findObjects];
}
Run Code Online (Sandbox Code Playgroud)
因此,每次我想使用这两个属性时,我都必须这样做:
if(! self.products)
[self initProducts];
Run Code Online (Sandbox Code Playgroud)
和
if(! self.query)
[self initQuery];
Run Code Online (Sandbox Code Playgroud)
我觉得我在做错了.有更清洁的方法吗?非常感谢你!
我有一个使用Laravel和Angular的微型网站.这是一个单页微型网站,响应迅速,分为5个部分.我想懒得加载它们以减少一次加载.
<body ng-app>
<div id="wrapper">
<section id="intro">1</section>
<section id="Second">2</section>
<section id="Third">3</section>
<section id="Fourth">4</section>
<section id="Fifth">5</section>
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
我正在寻找在页面加载时加载1和2然后当您向下滚动页面时加载另一个视图并使用漂亮的淡入淡出然后加载其交互式项目.
说我有这个:
int x;
int x = (State Determined By Program);
const char * pArray[(const int)x]; // ??
Run Code Online (Sandbox Code Playgroud)
在使用之前如何初始化pArray? 因为Array的初始大小由用户输入决定
谢谢!
我有一个属性声明为的类:
@property (nonatomic, strong) NSMutableArray *links;
Run Code Online (Sandbox Code Playgroud)
我想懒惰地实例化它,所以有以下自定义 getter:
- (NSMutableArray *)links {
if (!_links) {
_links = [NSMutableArray array];
}
return _links;
}
Run Code Online (Sandbox Code Playgroud)
我的应用程序取得了一些进展,现在可以从不同的线程访问该对象。我将声明更改为:
@property (atomic, strong) NSMutableArray *links;
Run Code Online (Sandbox Code Playgroud)
这会生成一个编译器警告:可写原子属性不能将合成的 setter 与用户定义的 getter 结合起来。
我明白 - 我想。 我的问题是,为了使用自定义 getter 创建原子属性,执行以下操作是正确的吗?:
@synchronized@synchronized编辑:这是我的新自定义 setter 和 getter 的代码:
- (NSMutableArray *)links {
if (!_links) {
@synchronized(self) {
if (!_links) {
_links = [NSMutableArray array];
}
}
}
return _links;
}
- (void)links:(NSMutableArray …Run Code Online (Sandbox Code Playgroud) 我有一个带有静态字符串变量的类,它具有一些有点复杂的初始化(我不能将它设置为等于带引号的字符串"whatever").我需要运行几行代码来实际创建值.一旦设置,其值将不会改变.它目前被设置为刚刚在第一次调用时设置的属性get.
class MyClass
{
private static string _myString = "";
public static string MyString
{
get
{
if(_myString == "")
{
// use an object "obj" here to create the value
MyObject obj = new MyObject();
obj.someSetupHere();
_myString = obj.ToString();
}
return _myString;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:有更好的方法吗?我希望在设置所有其他变量时设置值,而不是在值的第一个"get"上设置.我应该在Lazy<T>这里使用吗?我真的很喜欢这样的东西:
private static string _myString =
{
// use an object "obj" here to create the value
MyObject obj = new MyObject();
obj.someSetupHere();
_myString = obj.ToString();
}
Run Code Online (Sandbox Code Playgroud)
我知道这可能不是有效的语法,但希望它传达了我正在尝试做的事情.
我正在实现我自己的类,它提供了其成员的延迟初始化.我this在lambda中遇到了一种奇怪的捕获行为.
这是一个再现此错误的示例.
//Baz.h
#include <memory>
#include <functional>
#include "Lazy.hpp"
struct Foo
{
std::string str;
Foo() = default;
Foo(std::string str) : str(str) {}
Foo(Foo&& that) : str(that.str) { }
};
class Baz
{
std::string str;
Lazy<std::unique_ptr<Foo>> foo;
public:
Baz() = default;
Baz(const std::string& str) : str(str)
{
//lazy 'this->foo' initialization.
//Is capturing of 'this' valid inside ctors???.
this->foo = { [this] { return buildFoo(); } };
}
Baz(Baz&& that) : foo(std::move(that.foo)), str(that.str) { }
std::string getStr() const
{
return …Run Code Online (Sandbox Code Playgroud) 在 Python 中,我可以轻松地装饰方法,以便它们记住结果:
def store(self):
a = line1()
b = line2(a)
return line3(b)
Run Code Online (Sandbox Code Playgroud)
=>
from lazy import lazy
@lazy
def store(self):
a = line1()
b = line2(a)
return line3(b)
Run Code Online (Sandbox Code Playgroud)
Ruby 中是否有一些类似的习惯用法只计算一次方法结果?
c# ×2
c++ ×2
objective-c ×2
angularjs ×1
arrays ×1
atomic ×1
c++11 ×1
cocoa ×1
constructor ×1
ejb-3.1 ×1
ios ×1
java ×1
javascript ×1
jsf-2 ×1
lambda ×1
mef ×1
ruby ×1
visual-c++ ×1