跳至主要內容

XML配置


XML配置

XML配置demo

项目结构

Spring xml bean配置

Main

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        UseFunctionService useFunctionService = applicationContext.getBean(UseFunctionService.class);
        System.out.println(useFunctionService.SayHello("World"));
    }

}

FunctionService

public class FunctionService {

    public String sayHello(String word){
        return "Hello " + word + "!";
    }

}

UseFunctionService

public class UseFunctionService {

    private FunctionService functionService;

    public FunctionService getFunctionService() {
        return functionService;
    }

    public void setFunctionService(FunctionService functionService) {
        this.functionService = functionService;
    }

    public String SayHello(String word) {
        return functionService.sayHello(word);
    }

}
上次编辑于:
贡献者: Neil