初学spring的方法(1)

来源:互联网 发布:如何做好网络咨询 编辑:程序博客网 时间:2024/06/14 08:33

1首先打开spring的官网文档http://spring.io/,看到几个目录分别是DOCS(文档),GUIDES(指引),PROJECTS(产品项目),BLOG(博客),QUESTIONS(问题)等,首先看看文档里的技术模块划分,spring分为好多模块,比如下图


2看完文档有个大致了解后,可以看看GUIDES(指引).

3再看看项目(PROJECTS)http://projects.spring.io/spring-framework/#quick-start,这就是一个简单的列子,这样一步一步的把其它模块加进来进行测试

  1)添加maven依赖

 <dependency>        <groupId>org.springframework</groupId>        <artifactId>spring-context</artifactId>        <version>4.3.3.RELEASE</version>    </dependency>

2spring demo代码

package hello;public interface MessageService { String getMessage();}

package hello;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;@Componentpublic class MessagePrinter { final private MessageService service;    @Autowired    public MessagePrinter(MessageService service) {        this.service = service;    }    public void printMessage() {        System.out.println(this.service.getMessage());    }}

package hello;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@ComponentScanpublic class Application {    @Bean    MessageService mockMessageService() {        return new MessageService() {            public String getMessage() {              return "Hello World!";            }        };    }  public static void main(String[] args) {      ApplicationContext context =           new AnnotationConfigApplicationContext(Application.class);      MessagePrinter printer = context.getBean(MessagePrinter.class);      printer.printMessage();  }}

4还可以看网上别人的博客

5或者github的samples,有好多简单的入门列子




0 0