美文网首页
java配置 [配置扫描]

java配置 [配置扫描]

作者: _王仔 | 来源:发表于2017-12-05 00:32 被阅读6次

实现功能

@Service
public class FunctionService {
    
    public String sayHello() {
        return "success";
    }
}

java注解扫描

@Configuration
@ComponentScan("com.wangzhi.test")
public class DiConfig {
}

服务

@Service
public class useFunctionService {
    @Autowired
    FunctionService functionService;
    //调用工具类的sayHello
    public void sayhello() {
        System.out.println(functionService.sayHello());
    }
    
}

测试

public class Main {
    public static void main(String[] args) {
        //获取Java配置文件
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DiConfig.class);
        //获取bean,在这里functionService也被注入了 [@Autowired]
        useFunctionService useFunctionService = context.getBean(useFunctionService.class);
        //调用他的方法,再去调用functionService 的方法
        useFunctionService.sayhello();
        //关闭applicationContext
        context.close();
    }
}

相关文章

网友评论

      本文标题:java配置 [配置扫描]

      本文链接:https://www.haomeiwen.com/subject/ohxqixtx.html