我想知道如何使JAXB编译器使我的XML模式中的某些元素在java类定义中声明为final,我也希望能够控制不同的构造函数,因为我想要一个构造函数可以使用类中包含的完整参数列表以及JAXB所需的默认私有构造函数来创建对象.
有帮助吗?
谢谢.
这是我想要做的一个例子:
<xs:complexType name="mycomplex">
<xs:all>
<xs:element name="myboolean" type="xs:boolean"/>
</xs:all>
</xs:complexType>
Run Code Online (Sandbox Code Playgroud)
现在生成的代码看起来像
public class mycomplex
{
protected boolean myboolean;
public boolean getMyboolean() { return myboolean; }
public void setMyboolean(boolean b) { this.myboolean = b; }
}
Run Code Online (Sandbox Code Playgroud)
但我想编辑架构使其看起来像:
public class mycomplex
{
protected final boolean myboolean;
public mycomplex(boolean b) { this.myboolean = b; }
public boolean getMyboolean() { return myboolean; }
}
Run Code Online (Sandbox Code Playgroud)
这可以实现吗?
GlassFish 3.1的托管bean的CDI实现是否支持构造函数注入?我有一个@SingletonEJB,我想使用构造函数注入注入另一个托管bean(包含在同一个EJB模块中).现场注入确实有效.但随着构造器注入我收到了NullPointerException从AbstractSingletonContainer.
这确实有效:
@Singleton
public class FooBean implements Foo {
@Inject private BarBean bar;
}
Run Code Online (Sandbox Code Playgroud)
这不起作用:
@Singleton
public class FooBean implements Foo {
private final BarBean bar;
@Inject
public FooBean(BarBean bar) {
this.bar = bar;
}
}
Run Code Online (Sandbox Code Playgroud) dependency-injection glassfish constructor-injection cdi java-ee-6
我对Ninject很新,但我已成功使用自定义提供程序将其用于DI.
绑定初始化如下
kernel = new StandardKernel();
kernel.Bind<IPatientRecordLocator>().ToProvider<PatientRecordLocatorProvider>();
Run Code Online (Sandbox Code Playgroud)
在自定义提供程序中,我像这样调用Activator.CreateInstance
protected override IPatientRecordLocator CreateInstance(IContext context)
{
var name = ConfigurationManager.AppSettings["PatientRecordLocator"];
var typeName = name.Split(',')[0];
var assemblyName = name.Split(',')[1];
return Activator.CreateInstance(assemblyName, typeName).Unwrap() as IPatientRecordLocator;
}
Run Code Online (Sandbox Code Playgroud)
(是的,我知道上面的代码中没有错误处理等:))
而这一切都像一个魅力.
现在,我面临的问题是当我引入一个我希望注入IPatientRecordLocator实例的新类时.当我向其中一个类添加如下构造函数时,会出现问题
[Inject]
public MockPatientRecordLocator (IContactAdapter contactAdapter)
{
...
}
Run Code Online (Sandbox Code Playgroud)
然后,为了使Activator.CreateInstance工作,我还必须向类MockPatientRecordLocator添加一个无参数构造函数,即
public MockPatientRecordLocator()
{
}
Run Code Online (Sandbox Code Playgroud)
所以,我的问题是:如何让Ninject注入一个实现IContactAdapter的类的实例到例如MockPatientRecordLocator?我尝试过注射方法,但无济于事.
我忘了解释我正在尝试实现的是一种链式注入,其中一个类PatientRecordSummary的实例被注入一个MockPatientRecordLocator实例(使用构造函数注入),并且所述MockPatientRecordLocator实例应该注入一个IContactAdapter实例(再次使用构造函数注入(如果可能)).链的第一部分工作,第二部分没有.
我一直试图找出编写测试友好代码的最佳实践,但更具体地说是与对象构造相关的实践.在蓝皮书中,我们发现在创建对象时应该强制执行不变量,以避免我们的实体,值对象等的损坏,考虑到这一点,Design By Contract似乎是避免我们的对象损坏的解决方案,但是当我们遵循这个,我们最终可能会编写如下代码:
class Car
{
//Constructor
public Car(Door door, Engine engine, Wheel wheel)
{
Contract.Requires(door).IsNotNull("Door is required");
Contract.Requires(engine).IsNotNull("Engine is required");
Contract.Requires(wheel).IsNotNull("Wheel is required");
....
}
...
public void StartEngine()
{
this.engine.Start();
}
}
Run Code Online (Sandbox Code Playgroud)
嗯,这看起来很好看吗?看来我们正在构建一个安全类,暴露了所需的契约,因此每次Car创建对象时我们都可以确定该对象是"有效的".
现在让我们从测试驱动的角度来看这个例子.
我想构建测试友好的代码,但为了能够隔离测试我的Car对象我需要为每个依赖创建一个模拟存根或一个虚拟对象来创建我的对象,即使我可能只是想测试一种方法,只使用这些依赖项之一,如StartEngine方法.遵循Misko Hevery的测试理念我想编写我的测试,明确指出我不关心Door或Wheel对象只是将null引用传递给构造函数,但是因为我正在检查null,所以我不能这样做
这只是一小段代码,但是当您面对真正的应用程序时,编写测试变得越来越难,因为您必须解决主题的依赖关系
Misko建议我们不要滥用代码中的空值检查(这与设计合同相矛盾)因为这样做,编写测试会变得很痛苦,作为替代方案,他认为编写更多的测试比"只有这样的测试"更好.我们的代码是安全的,因为我们到处都有空检查"
你对此有何看法?你会怎么做?什么应该是最好的做法?
unit-testing design-by-contract dependency-injection constructor-injection
我是Spring Bean的新手,所以我没有在Constructor-arg中使用ref。为什么不像在此示例中那样再次使用值,
这是TextEditor.java文件的内容:
package com.tutorialspoint;
public class TextEditor {
private SpellChecker spellChecker;
public TextEditor(SpellChecker spellChecker) {
System.out.println("Inside TextEditor constructor." );
this.spellChecker = spellChecker;
}
public void spellCheck() {
spellChecker.checkSpelling();
}
}
Run Code Online (Sandbox Code Playgroud)
以下是另一个依赖类文件SpellChecker.java的内容:
package com.tutorialspoint;
public class SpellChecker {
public SpellChecker(){
System.out.println("Inside SpellChecker constructor." );
}
public void checkSpelling() {
System.out.println("Inside checkSpelling." );
}
}
Run Code Online (Sandbox Code Playgroud)
以下是MainApp.java文件的内容:
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
TextEditor te = (TextEditor) …Run Code Online (Sandbox Code Playgroud) 我有一个MainClass有2个变量.我想将这两个值传递给springframework bean类"Test".我如何在applicationContext.xml中定义它,以及如何将这两个变量值传递给bean"Test".
例如:
class MainClass {
public int var1;
public int var2;
public Test test;
public void setVar1(int var11) {
var1 = var11;
}
public void setVar2(int var22) {
var2 = var22;
}
public static void main(String args[]) {
ApplicationContext context =
new FileSystemXmlApplicationContext("applicationContext.xml");
Test = context.getBean("test");
}
}
Run Code Online (Sandbox Code Playgroud)
------------ TEST类------------
public class Test {
public Test (int var1, int var2) {}
}
Run Code Online (Sandbox Code Playgroud)
------------- applicationContext.xml -------------
<bean id="test" class="com.path.test">
<constructor-arg index="0" type="int" value="????"/>
<constructor-arg index="1" type="int" value="????"/>
</bean>
Run Code Online (Sandbox Code Playgroud) 我刚从Ninject更改为TinyIoC以进行依赖注入,而我在构造函数注入方面遇到了麻烦.
我已设法将其简化为此代码段:
public interface IBar { }
public class Foo
{
public Foo(IBar bar) { }
}
public class Bar : IBar
{
public Bar(string value) { }
}
class Program
{
static void Main(string[] args)
{
var container = TinyIoCContainer.Current;
string value = "test";
container.Register<IBar, Bar>().UsingConstructor(() => new Bar(value));
var foo = container.Resolve<Foo>();
Console.WriteLine(foo.GetType());
}
}
Run Code Online (Sandbox Code Playgroud)
这会导致抛出TinyIoCResolutionException:
"Unable to resolve type: TinyIoCTestApp.Foo"
Run Code Online (Sandbox Code Playgroud)
在该异常内部是一系列内部异常:
"Unable to resolve type: TinyIoCTestApp.Bar"
"Unable to resolve type: System.String"
"Unable to resolve type: System.Char[]"
"Value …Run Code Online (Sandbox Code Playgroud) 有一个关于如何使用吉斯提供商绑定到一个通用类,如很多网络资源的维基条目这样和这样的SOF的问题.但是,我无法找到有关如何使用通用本身的提供程序的资源.
例如,假设我有以下内容:
public interface Foo {
...
}
public class Bar implements Foo {
...
}
Run Code Online (Sandbox Code Playgroud)
如您所见,Bar有一个注入参数,必须在构造函数中.在我的情况下,我们无法将此参数移动到字段或方法注入.
所以,现在我想为Foo编写一个Provider,但它返回一个Bar实例.像这样的东西:
@Singleton
public class FooProvider<T extends Foo> extends Provider<Foo> {
@Inject
public FooProvider(Object someParameterWeMustInjectIntoConstructor) {
...
}
...
@Override
public Foo get() {
return new Bar(...);
}
...
}
Run Code Online (Sandbox Code Playgroud)
注意这个类是如何依赖于Guice注入本身的单例,因此必须使用配置Provider .toProvider(FooProvider.class).我们也只能使用构造函数注入而不是方法和字段注入,可能是因为具有此Provider的类型层次结构.
我试图找到某种使用方式.toProvider(FooProvider<Bar>.class),但我们都知道这不是有效的Java语法.有谁知道我如何.toProvider(Class<? extends Provider>)在模块中使用Guice绑定器的方法,所以我可以使用通用的Provider来创建一个具体的实现类?如果我的问题有点令人困惑,请告诉我.
java inheritance dependency-injection guice constructor-injection
所以我觉得我在这里遗漏了一些东西,但我基本上是在制作一个将调用多个处理程序的控制台应用程序。
程序.cs
private static IContainer _container;
static void Main(string[] args)
{
_container = AutoFacBootstrapper.Init();
var _backupFactory = _container.Resolve<IBackupFactory>();
UserHandler.Init();
}
Run Code Online (Sandbox Code Playgroud)
AutoFacBootstrapper.cs
public static IContainer Init()
{
var builder = new ContainerBuilder();
builder.RegisterType<BackupFactory>().As<IBackupFactory>();
return builder.Build();
}
Run Code Online (Sandbox Code Playgroud)
看到这一切既美好又有趣,我可以在 Program.cs 中使用我的工厂,但是当我尝试在不同的类中使用我的工厂时(比如 UserHandler.cs),我的_backupFactory将保持为 null(这是有道理的)。
用户处理程序
private static IBackupFactory _backupFactory;
public UserHandler(IBackupFactory backupFactory)
{
_backupFactory = backupFactory;
}
public static void Init(string[] subCommands)
{
var users = _backupFactory.GetFtpUsers();
}
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题,以便我可以在另一个类中使用构造函数注入,而只需在启动时对其进行初始化?
我已经阅读了依赖注入。然后来了
它们与依赖注入有何不同或它们都相同?注射在这里是什么意思?只是将所需的对象/参数提供给类?像构造注入意味着将所需参数作为构造函数参数传递?还是我错过了什么?
java ×4
c# ×2
spring ×2
activator ×1
autofac ×1
cdi ×1
glassfish ×1
guice ×1
inheritance ×1
java-ee-6 ×1
jaxb ×1
ninject ×1
spring-mvc ×1
terminology ×1
tinyioc ×1
unit-testing ×1