【java – 如何使用Spring在枚举构造函数中注入参数?】教程文章相关的互联网学习教程文章

java – 具有私有构造函数的Singleton类的Spring(创建bean,没有可见构造函数时出错)【代码】

我有4个带有私有构造函数的单例类我正在尝试为所有4个类创建bean属性. 主要问题是,我能够为3个类创建bean这三个类与getInstance方法具有相似的结构私有构造函数()(Singleton类)但第四个和最后一个抛出异常(下面粘贴了异常消息) 请在下面找到getInstance方法,即私有构造函数和bean id声明.所有四个bean声明都是一样的 但是,如果我将构造函数从“Private”更改为“Public”,那么我没有得到错误.任何人都可以对正在发生的事情发表任何...

java – Spring Bean和带有params的@Autowired构造函数【代码】

我想了解是否可以使用以及如何使用@Autowired Constructor和params进行Bean操作.@Component public class Routes {private Foo req;@Autowiredpublic Routes(Foo req){this.req = req;}public String getUrl(String destin){return req.getContextPath() + destin;} }@Component public class HomeController {@Autowiredprivate Routes routes;public HomeController(Foo req){String foo = routes.getUrl("something");} }真正的...

Java中构造函数中的最终参数【代码】

我正在学习Java中的类和构造函数.我在一个示例程序中乱用代码,似乎无法弄清楚到底发生了什么. 这段代码不会编译对我来说有意义:class Line { Point start; Point end;Line(final Point start, final Point end) {this.start = new Point(start);this.end = new Point(end);start = new Point(0.4, 0.4);}...我试图通过调用Point对象的构造函数将原始的起始点对象引用分配给另一个Point对象. final关键字与此冲突. 但是当我从Point...

java – 抽象类中的静态构造函数?【代码】

请考虑以下示例情况:public abstract class Parent {private ByteBuffer buffer;/* Some default method implementations, interacting with buffer */public static Parent allocate(int len){// I want to provide a default implementation of this -- something like:Parent p = new Parent();p.buffer = ByteBuffer.allocate(len);return p;} }public class Child extends Parent {/* ... */ }public class App {public stat...

java – Spring构造函数@Autowired和@Qualifier失败【代码】

简化代码(我使用的是Spring 3.1.4). 这是我想要自动装配的课程:public class MyCoolClass {@Autowiredpublic MyCoolClass(@Qualifier("CoolBean1") SomeOtherClass1 foo1,@Qualifier("CoolBean2") SomeOtherClass1 foo2) {this.foo1 = foo1;this.foo2 = foo2;}// ... }这是我的spring config xml:<bean name="CoolBean1" class="mypackage.SomeOtherClass1"/> <bean name="CoolBean2" class="mypackage.SomeOtherClass1"/> <bean...

为什么我需要在这个Java通用子类中有一个构造函数?【代码】

我正在使用Java泛型.这是一个代码示例,后跟问题.public class Test<T extends Comparable<T>> {T data;public Test(T data) {this.data = data;}public T getData() {return this.data;} }class MyClass<T extends Comparable<T>> extends Test<T> {//if I remove this constructor, code will not compilepublic MyClass(T data) {super(data);} }在MyClass中,如果我没有构造函数,则会出现以下编译时错误:Implicit super constr...

java – 为什么在反序列化过程中不调用默认构造函数?【代码】

ObjectInputStream is = new ObjectInputStream(new FileInputStream("test.ser")); TestClass tc = (TestClass)is.readObject();我在反序列化后得到了TestClass的对象,但是没有调用TestClass的默认构造函数.据我所知有两种方法可以创建对象,即使用new运算符或TestClass.class.newInstance().两者都调用默认构造函数. 看起来像反序列化过程创建对象不是用两个方法,这就是为什么不调用默认构造函数.问题是反序列化如何创建对象? 另...

java – 更好的构造函数初始化设计【代码】

我有一个看起来像这样的类,它根据从凭证服务检索的凭据充当客户端的工厂.它构建客户端一次,并在每次调用时返回.public class ClientFactory {private CredentialService credentialService;private ClientA clientA; public ClientFactory(CredentialService credentialService){this.credentialService = credentialService;//initialization in constructorthis.clientA = buildClientA(credentialService.getCredentials());}pu...

Java构造函数设计【代码】

我正在阅读一个开源代码,并且有一个像这样设计的构造函数:public class FeatureSequence2FeatureVector extends Pipe implements Serializable {boolean binary;public FeatureSequence2FeatureVector (boolean binary){this.binary = binary;}public FeatureSequence2FeatureVector (){this (false);}}这可能只是一个微不足道的偏好问题,但我会做的是这样的:public class FeatureSequence2FeatureVector extends Pipe implement...

Spring Java配置,@ Autowire与构造函数注入,@ Transaction和CGLIB【代码】

我们一直在使用@Autowired和基于Java的Spring配置取得了一些成功,但现在我们失去了控制权.每个人都开始在任何地方添加自动连接的依赖项,创建周期和奇怪的错误. 所以我们正在考虑使用构造函数注入和Spring配置的自动装配. 旧:class Bean {@Autowired Foo foo; }@Configuration @Import( FooCfg.class ) class BeanCfg {@Bean public Bean bean() { return new Bean(); } }新:class Bean {public Bean(Foo foo) {...} }@Configurat...

java – 将参数化构造函数作为方法引用传递【代码】

是否可以将参数化构造函数作为方法引用传递给map? 我的代码中有一个看起来像这样的工具items.stream().map(it -> new LightItem(item.getId(), item.getName()).collect(Collectors.toList());我的项目列表包含几个Item对象Itemid, name, reference, key...而LightItem只有两个字段LightItemid, name如果有可能做这样的事情会很好items.stream().map(LightItem::new).collect(Collectors.toList())解决方法:这里只有一种方法可以使...

java – 无法将参数传递给super();在类的构造函数中扩展RealmBaseAdapter(Realm)【代码】

我有一个问题,我无法将参数“context,realmResults,automaticUpdate”传递给super();在扩展RealmBaseAdapter的类的构造函数中.请参阅我的代码和截图,以便为您清楚. *我的代码:package com.twitter.i_droidi.notah;import android.content.Context; import android.view.View; import android.view.ViewGroup; import io.realm.RealmBaseAdapter; import io.realm.RealmObject; import io.realm.RealmResults;public class RealmMo...

java – 在显式定义构造函数时,没有在类中定义0参数的构造函数【代码】

我不明白……我实际上定义了构造函数,但我得到了 没有在类中定义0参数的构造函数@Component public class CustomMessageSource extends ReloadableResourceBundleMessageSource {public CustomMessageSource(Locale locale){this.propertiesHolder = getMergedProperties(locale); this.properties = propertiesHolder.getProperties(); } //... other setting, getters这就是我实例化它的方式 CustomMessageSource customM...

java – 配置类中的Spring MVC Default或no-arg构造函数【代码】

@Configuration public class ApplicationConfig {private DataSource dataSource;@Autowiredpublic ApplicationConfig(DataSource dataSource) {this.dataSource = dataSource;}@Bean(name="clientRepository")ClientRepository jpaClientRepository() {return new JpaClientRepository();} }我刚刚发现了一个Core Spring 4.2认证模拟考试,关于这个类,它说“默认或无参数构造函数是强制性的”.我在我做的测试中尝试了这个类,它没有...

java – 构造函数与setter注入【代码】

我目前正在设计一个API,我希望通过各种方法进行配置.一种方法是通过XML配置模式,另一种方法是通过API,我希望与Spring很好地配合. 我的XML模式解析代码以前是隐藏的,因此唯一关心的是它可以工作但现在我希望构建一个公共API,我非常关心最佳实践. 似乎许多人喜欢javabean类型PoJo的默认零参数构造函数然后setter注入.我试图解决的问题是某些setter方法实现依赖于在它们之前按顺序调用的其他setter方法. 我可以编写肛门设置器,它可以容...

构造函数 - 相关标签