初学spring的方法(1)

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;

@Component
public 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
@ComponentScan
public 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,有好多简单的入门列子




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值