spring 容器的简述

来源:互联网 发布:js左右拖动滑块插件 编辑:程序博客网 时间:2024/06/05 02:37

spring容器的简单测试

在maven中引入spring-context(常用的容器管理spring-context集成了许多jar包)

写一个testservice

@Service

public class TestService{

public void sayHello(String word){

return "hello "+word;

}

}

写一个配置类,相当于xml配置文件

@Configuration

@ComponentScan("xx.xx.xx")

public class JavaConfig(){

}

写一个启动类

public class Main(){

根据配置类获取容器

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(JavaConfig.class);//引入配置类
TestService testService = context.getBean(TestService .class);//根据指定类获取bean
System.out.println(testService.sayHello("world"));

}


是不是非常简单非常干净的一个小程序!


1 0
原创粉丝点击