简体   繁体   English

卡住将 Spring xml 配置转换为 java 配置

[英]Stuck converting Spring xml config to java config

I stuck converting my current test app in Spring from using XML configuration to using Java configuration...我坚持将 Spring 中的当前测试应用程序从使用 XML 配置转换为使用 Java 配置...

I have the following files我有以下文件

App.java App.java

package com.spring.ioc.DependencyInjection;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

 public class App {
    public static void main(String[] args) {
     ApplicationContext ctx = new ClassPathXmlApplicationContext("app-config.xml");
        Phone p = ctx.getBean("phone", Phone.class);
     p.calling();
     p.data();

    }
}

Galaxy.java Galaxy.java

package com.spring.ioc.DependencyInjection;

public class Galaxy implements Phone {
    public void calling() {
        System.out.println("Calling using Galaxy");
    }
    public void data() {
        System.out.println("Browsing internet using Galaxy");
    }
}

IPhone.java iPhone.java

package com.spring.ioc.DependencyInjection;

public class IPhone implements Phone {
    public void calling() {
        System.out.println("Calling using iPhone");
    }
    public void data() {
        System.out.println("Browsing internet using iPhone");
    }
}

Phone.java电话.java

package com.spring.ioc.DependencyInjection;

public interface Phone {
    void calling();

    void data();
}

app-config.xml应用程序配置.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
        
      <bean id="phone" class="com.spring.ioc.DependencyInjection.IPhone"></bean>  
        
 </beans>

The code above allows me to demo how you can use XML & edit the text between 'IPhone' or 'Galaxy' by changing the bean name at the end of the fully qualified name上面的代码允许我演示如何使用 XML 并通过更改完全限定名称末尾的 bean 名称来编辑“iPhone”或“Galaxy”之间的文本

<bean id="phone" class="com.spring.ioc.DependencyInjection.IPhone"></bean> or <bean id="phone" class="com.spring.ioc.DependencyInjection.Galaxy"></bean> <bean id="phone" class="com.spring.ioc.DependencyInjection.IPhone"></bean><bean id="phone" class="com.spring.ioc.DependencyInjection.Galaxy"></bean>

How can do the same in using JavaConfig instead of XML config?如何在使用 JavaConfig 而不是 XML 配置时做同样的事情?

I know how to use Java configuration to just pull one bean but am lost how to set it up to alternate between two objects.我知道如何使用 Java 配置来只拉一个 bean,但我不知道如何设置它以在两个对象之间交替。

Can you please show me by modifying the code I provided or adding any other code needed?你能通过修改我提供的代码或添加任何其他需要的代码来告诉我吗?

I believe you can use我相信你可以使用

@Component("iphone")
public class IPhone {}

@Component("galaxy ")
public class Galaxy {}

and where you inject it,以及你注射它的地方,

@Autowired
@Qualifier(value = "iphone")
private Phone iPhone;

@Autowired
@Qualifier(value = "galaxy")
private Phone galaxy;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM