java EE開發的顛覆者 spring boot 實戰 隨書學習-1java
1.學習案例都是maven項目,首先要在eclipse 中配置 maven,主要修改maven的配置文件:配置文件下載連接: https://github.com/liuch0228/springboot-wangyunfei-learn ,最主要的是修改鏡像地址,這裏使用阿里雲的鏡像:git
1 <mirrors> 2 <!-- mirror 3 | Specifies a repository mirror site to use instead of a given repository. The repository that 4 | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used 5 | for inheritance and direct lookup purposes, and must be unique across the set of mirrors. 6 | 7 <mirror> 8 <id>mirrorId</id> 9 <mirrorOf>repositoryId</mirrorOf> 10 <name>Human Readable Name for this Mirror.</name> 11 <url>http://my.repository.com/repo/path</url> 12 </mirror> 13 --> 14 <mirror> 15 <id>aliyun</id> 16 <name>aliyun Maven</name> 17 <mirrorOf>*</mirrorOf> 18 <url>http://maven.aliyun.com/nexus/content/groups/public/</url> 19 </mirror> 20 </mirrors>
2. 新建一個maven項目github
pom文件修改,添加spring相關依賴spring
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>com.wisely</groupId> 5 <artifactId>highlight-spring4</artifactId> 6 <version>0.0.1-SNAPSHOT</version> 7 8 <!-- 在properties中定義一個名字爲java.version的變量,在dependency中引用 --> 9 <properties> 10 <java.version>1.7</java.version> 11 </properties> 12 <!-- 添加pom依賴 --> 13 <dependencies> 14 <!-- 添加spring-context依賴 --> 15 <dependency> 16 <groupId>org.springframework</groupId> 17 <artifactId>spring-context</artifactId> 18 <version>4.1.6.RELEASE</version> 19 </dependency> 20 21 <!-- 添加AOP依賴 --> 22 <dependency> 23 <groupId>org.springframework</groupId> 24 <artifactId>spring-aop</artifactId> 25 <version>4.1.6.RELEASE</version> 26 </dependency> 27 <!-- 添加AspectJ依賴 --> 28 <dependency> 29 <groupId>org.aspectj</groupId> 30 <artifactId>aspectjrt</artifactId> 31 <version>1.8.5</version> 32 </dependency> 33 <dependency> 34 <groupId>org.aspectj</groupId> 35 <artifactId>aspectjweaver</artifactId> 36 <version>1.8.5</version> 37 </dependency> 38 </dependencies> 39 40 <!-- 編譯插件依賴 --> 41 <build> 42 <plugins> 43 <plugin> 44 <groupId>org.apache.maven.plugins</groupId> 45 <artifactId>maven-compiler-plugin</artifactId> 46 <version>2.3.2</version> 47 <configuration> 48 <source>${java.version}</source> <!-- 經過變量指定編譯源代碼使用的jdk版本 --> 49 <target>${java.version}</target> <!-- 經過變量指定目標代碼使用的jdk版本 --> 50 </configuration> 51 </plugin> 52 </plugins> 53 </build> 54 55 </project>
改完保存後,鼠標右鍵單擊項目,maven update project,下載依賴jar包:apache
3.依賴注入示例:springboot
3.1 FunctionService 類eclipse
1 package com.wisely.highlight_spring4.ch1.di; 2 3 import org.springframework.stereotype.Service; 4 /** 5 * 定義功能類的bean 6 * @Service註解聲明當前FunctionService類是spring管理的一個bean 7 * 聲明bean的註解還有: 8 * @Repository 數據訪問層使用 9 * @Controller 展示層使用 10 * @Component 聲明被spring管理的組件(也就是自動注入spring容器),沒有明確角色 11 * @author Administrator 12 * 13 */ 14 @Service 15 public class FunctionService { 16 17 public String sayHello(String word) { 18 return "hello " + word + " !"; 19 } 20 21 }
3.2 UseFunctionService 類
1 package com.wisely.highlight_spring4.ch1.di; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.stereotype.Service; 5 /** 6 * 聲明使用功能類FunctionService的bean 7 * @Service 聲明UseFunctionService類是spring管理的一個Bean 8 * @author Administrator 9 * 10 */ 11 @Service 12 public class UseFunctionService { 13 /*@Autowired 把FunctionService的實體bean注入到UseFunctionService類中 14 * 讓UseFunctionService具有FunctionService的功能 ———— 組合關係 15 * */ 16 @Autowired 17 private FunctionService functionService; 18 19 public String sayHello(String word) { 20 return functionService.sayHello(word); 21 } 22 23 }
3.3 Java配置類
java配置是spring4.x推薦的配置方式,能夠徹底替代xml配置,也是springboot推薦的配置方式。Java 配置是經過@Configuration和@Bean來實現的。
@Configuration 聲明當前類是一個配置類 ,@ComponentScan 註解自動掃描指定包下的全部使用@Service @Component @Repository 和@Controller的類,將它們註冊爲spring的Bean
1 package com.wisely.highlight_spring4.ch1.di; 2 3 import org.springframework.context.annotation.ComponentScan; 4 import org.springframework.context.annotation.Configuration; 5 /** 6 * spring配置類 7 * @Configuration 聲明當前類是一個配置類 8 * @ComponentScan 註解自動掃描指定包下的全部使用@Service @Component @Repository 9 * 和@Controller的類,並註冊爲Bean 10 * @author Administrator 11 * 12 */ 13 @Configuration 14 @ComponentScan("com.wisely.highlight_spring4.ch1.di") 15 public class DiConfig { 16 17 }
主測試類:獲取spring容器實例context ,而後拿context獲取註冊的bean, 調用bean的方法maven
1 package com.wisely.highlight_spring4.ch1.di; 2 3 import org.springframework.context.annotation.AnnotationConfigApplicationContext; 4 5 public class MainTest { 6 7 public static void main(String[] args) { 8 //1.使用AnnotationConfigApplicationContext最爲spring容器,接受一個配置類做爲參數 9 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DiConfig.class); 10 //2.得到聲明配置的UseFunctionService 的bean 11 UseFunctionService useFunctionService = context.getBean(UseFunctionService.class); 12 //3.調用bean的方法 13 System.out.println(useFunctionService.sayHello("world")); 14 context.close(); 15 } 16 17 }
運行結果以下:學習