有没有办法在Spring Boot中基于命令行参数注入特定的接口实现?
我有一个数据加载应用程序,并根据命令行参数我需要加载特定类型的数据.
这是我的主要课程CommandLineRunner:
@SpringBootApplication
public class DataLoadersApplication implements CommandLineRunner {
private Type1LoadProcess type1LoadProcess;
private Type2LoadProcess type2LoadProcess;
public DataLoadersApplication(Type1LoadProcess type1LoadProcess,
Type2LoadProcess type2LoadProcess) {
this.type1LoadProcess = type1LoadProcess;
this.type2LoadProcess = type2LoadProcess;
}
public static void main(String[] args) {
SpringApplication.run(DataLoadersApplication.class, args);
}
@Override
public void run(String... args) {
if (args[0].equalsIgnoreCase("load-type1")) {
type1LoadProcess.process();
} else if (args[0].equalsIgnoreCase("load-type2")) {
type2LoadProcess.process();
}
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法DataLoadeProcess用两个实现创建一个接口,Type1DataLoadProcess并Type2DataLoadProcess根据命令行arg在main类中注入实现?
我有一个界面:
public interface Handler {
public <T> boolean shouldProcess(final T obj);
Run Code Online (Sandbox Code Playgroud)
和实施:
public class SampleHandler implements Handler {
@Override
public <Long> boolean shouldProcess(final Long date) {
return <some comparison>;
}
Run Code Online (Sandbox Code Playgroud)
我得到"类型参数Long隐藏类型长".我得到这个的原因是什么?
我有两个相同的结构,为消除歧义的目的有不同的类型:
type BaseType struct {
id uint64
name string
}
type LabeledType1 BaseType
type LabeledType2 BaseType
Run Code Online (Sandbox Code Playgroud)
整个链中有一个功能实际上并不关心LabeledType它,它只适用于BaseType(因为它与两者完全相同).事件的发送者必须发送标记类型,而不是基类型,因为实际类型定义了一些后行为.
func handle(evt interface{}) error {
switch e := evt.(type) {
case *LabeledType1:
return handleBaseEvent(e)
case *LabeledType2:
return handleBaseEvent(e)
//there are other types
case *OtherType:
return handleOtherType(e)
}
}
func handleBaseEvent(evt *BaseType) {
//do stuff
}
Run Code Online (Sandbox Code Playgroud)
现在当然这不编译:
cannot convert e (type *LabeledType1) to type BaseType
Run Code Online (Sandbox Code Playgroud)
但我想知道,就我理解这个概念而言,这两种类型都是可分配的,所以应该有一些简单的转换?我尝试过类型转换:
evt.(BaseType))
并且
BaseType(e)
我不能用bool里面的东西BaseType.
我有以下地图:Map<String, Set<String>>我需要初始化.这本身不是问题.然而,由于我希望Set保持插入顺序,我想到初始化整个构造new LinkedHashMap<String, LinkedHashSet<String>>();,但这迫使我将地图更改为:Map<String, LinkedHashSet<String>>.
如果我在初始化时指定特定类型,为什么我不能使用该接口?或者什么是确保嵌套类型实现的更好方法?
为了让我更容易理解为什么我想这样做,这就是代码:
private void sortMap(Map<String, Map<Date, Map<String, Long>>> dataMap) {
Map<String, Set<String>> sortedNames = new LinkedHashMap<String, Set<String>>(); // <-- this initialization
for (Map.Entry<String, Map<Date, Map<String, Long>>> entry : dataMap.entrySet()) {
sortedNames.put(entry.getKey(), entry.getValue().get(getCurrentMonthDate()).keySet());
}
}
Run Code Online (Sandbox Code Playgroud)
编辑
在接受的答案的帮助下,我提出了这个解决方案似乎对我来说很好用:
Map<String, Set<String>> sortedNames = dataMap.entrySet().stream()
.map(e -> new AbstractMap.SimpleEntry<String, LinkedHashSet<String>>(e.getKey(),
e.getValue().get(getCurrentMonthDate()).keySet().stream()
.collect(Collectors.toCollection(LinkedHashSet<String>::new))))
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
Run Code Online (Sandbox Code Playgroud) 我开始学习如何创建C#并使用Interface和Class.有人可以教我如何分离我的类和我的界面,并仍然保持彼此的联系?
原因:如果我要更新我的代码,我会知道在哪里添加它们,我的屏幕中的代码就会减少.
namespace Car
{
class MainClass
{
//My Interface
interface ICar
{
int gas { get; set; }
//void refuel();
int getGasLeft();
}
//My Class
class Car : ICar
{
public int gas { get; set; }
public Car(string _name)
{
name = _name;
dist = 0;
gas = 40;
}
public void refuel(int lit)
{
gas += lit;
}
public int getGasLeft()
{
return gas;
}
}
//Main
public static void Main(string[] args)
{
Car toyota = new …Run Code Online (Sandbox Code Playgroud) 我正在阅读打字稿中的接口,并遇到以下内容.
命名接口Square定义如下:
interface Square {
width: number
}
Run Code Online (Sandbox Code Playgroud)
然后square通过将对象文字Square类型转换为类型来声明名为变量:
let square = <Square>{}
Run Code Online (Sandbox Code Playgroud)
我试着印刷square和typeof square其预期给出了{}和object.但我无法将上述square定义与以下内容区分开来:
let sq: Square = {}
Run Code Online (Sandbox Code Playgroud)
这里的打字稿会抛出一个错误,说明该属性width丢失了sq.这与上面的转换语法有何不同?这里也是typescript-playground的片段.
作为一个例子,我有一个应用程序,使水果冰沙.我可以IEnumerable列出苹果冰沙,这是有效的.
我希望能够制作一个通用的冰沙列表,IEnumerable<ISmoothie<IFruit>>以防我决定添加另一个Fruit就像橙色一样.当我尝试这样做时,代码将无法编译,我收到错误:
无法将类型'System.Collections.Generic.List>'隐式转换为'System.Collections.Generic.IEnumerable>'.存在显式转换(您是否错过了演员?)
public class Program
{
public static void Main()
{
IEnumerable<ISmoothie<Apple>> appleSmoothies = new List<ISmoothie<Apple>>(); // I Can specifically make Apple Smoothies!
IEnumerable<ISmoothie<IFruit>> genericSmoothies = new List<Smoothie<Apple>>(); // Does Not Compile - What if I want generic Smoothies?
Console.WriteLine("Hello World");
}
public class Apple : IApple
{
// Concrete Implimentation of an Apple
}
public interface IApple : IFruit
{
// Specific Interface for Apples
}
public interface IFruit
{
// Basic Fruit Interface …Run Code Online (Sandbox Code Playgroud) 我最近一直在努力学习接口.
我看到了这段代码,无法解决为什么你不能单独使用DateTime.Now它.我不确定为什么界面很有用,有人可以解释一下吗?本书的作者试图解释,但我真的不明白如何按照他们的说法实现它:
程序员是否在抽象的海洋中迷失了自己?你可能会想到这么想,但实际上这很聪明.想象一下,你必须在一个类上运行一些测试,根据当前时间(或日期)给出不同的结果.这根本不常见; 也许这是一个需要在给定日期获得汇率的金融应用程序.因此,如果代码库直接在方法中使用DateTime.Now,请尝试测试.使用INowResolver,您可以现在注入并测试昨天,现在和明天
public interface INowResolver { DateTime GetNow(); }
public class NowResolver : INowResolver {
public DateTime GetNow() {
return DateTime.Now;
}
}
Run Code Online (Sandbox Code Playgroud)
当我测试它时,如果我使用该NowResolver.GetNow方法或刚刚使用,结果是相同的DateTime.Now.
测试:
NowResolver now = new NowResolver();
Console.WriteLine(now.GetNow());
Console.WriteLine(DateTime.Now);
System.Threading.Thread.Sleep(1000);
Console.WriteLine(now.GetNow());
Console.WriteLine(DateTime.Now);
Run Code Online (Sandbox Code Playgroud)
输出:
07/02/2019 15:14:56
07/02/2019 15:14:56
07/02/2019 15:14:57
07/02/2019 15:14:57
Run Code Online (Sandbox Code Playgroud) 我正在研究接口,并遇到了一些奇怪的接口问题,其中的规则是我们必须实现公共接口方法。但是在这个例子中没有。
我尝试了从经验中学到的知识,发现的答案确实违反了规则。
public interface DropV1
{
void Ship();
}
public interface DropV2
{
void Ship();
}
//accepted by the editor
class DropShipping : DropV1, DropV2
{
void DropV1.Ship() { }
void DropV2.Ship() { }
}
Run Code Online (Sandbox Code Playgroud)
我原以为10亿%的实施将是:
public void DropV1.Ship()
public void DropV2.Ship()
Run Code Online (Sandbox Code Playgroud)
为什么会这样呢?
在最近的一次采访中,有人问我一个问题:“我们怎么能说java8中的功能接口类似于标记接口”。
我无法回答这个问题。
但是我认为标记甚至没有任何方法,而功能接口必须要覆盖一种方法。
有人可以帮助我理解这是否在某些情况下是有效的论点,或者问题本身是错误的?
java interface marker-interfaces java-8 functional-interface
interface ×10
c# ×4
java ×4
generics ×2
autowired ×1
casting ×1
class ×1
command-line ×1
go ×1
ienumerable ×1
java-8 ×1
spring-boot ×1
types ×1
typescript ×1