Spring Boot入門 (二) :無xml配置的實現
阿新 • • 發佈:2018-12-17
無xml配置的實現
-
自Spring3.X 以後 spring 提供了很多的註解來代替XML檔案的配置,最核心的是下面兩個註解。
- ::@Configuration:: 標註該類是配置類,類似於我們定義的applicationContext.xml
- ::@Bean:: 類似於我們在之前的spring配置檔案中配置的
<bean id=" " class="">
-
有了上面兩個註解我們可以用編碼的方式來完成spring 的相關配置,接下來我們就來使用java編碼的方式來實現spring配置
-
1、新建一個工程,裡面是使用原生Spring實現無配置檔案。 — 建立July-javaconfig 工程 — 在pom.xml中加入一下內容:
<properties> <java.version>1.8</java.version> <spring.version>5.0.8.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build>
這裡使用spring 5.0.8.RELEASE 版本。
— 在工程中新建包 cn.org.july.spring.service
— 在該包下新建Class HelloService.class
,該service 實現一個方法sayHello(),程式碼如下:
package cn.org.july.spring.service; */*** * ****@author***wanghongjie* * * 2018-10-26* * */* public class HelloService { public String sayHello(){ return "Hello JavaConfig....."; } }
— 在該目錄下建立Class ApplicationConfiguration
,該類相當於定義了一個applicationContext.xml。
在ApplicationConfiguration.class 中定義一個方法,該方法獲取HelloService
。程式碼如下:
package cn.org.july.spring.service;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
*/***
* * 使用 ApplicationConfiguration 代替 配置檔案*
* ****@author***wanghongjie*
* */*
@Configuration //相當於定義了一個applicationContext.xml
public class ApplicationConfiguration {
@Bean // 相當於在applicationContext.xml中定義一個bean標籤
public HelloService helloService(){
return new HelloService();
}
}
— 建立測試類:Application.class
,這裡引入一個新的ClassAnnotationConfigApplicationContext
物件。通過該類來載入Spring上下文資訊。
存在applicationContext.xml
public static void main(String[] args) {
//所有配置檔案
args = new String[] {
"classpath:spring/spring-servlet.xml",
"classpath:spring/ApplicationContext.xml",
"classpath:spring/mybatis-config.xml",
};
ApplicationContext actx = new ClassPathXmlApplicationContext(args);
//得到類的例項
HelloService helloService = (HelloService) actx.getBean("helloService");
//呼叫類的方法
helloService.sayHello();
}
— 不使用applicationContext.xml檔案獲取Spring上下文資訊。
package cn.org.july.spring.service;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Application {
public static void main(String[] args) {
// 獲取spring 容器。
AnnotationConfigApplicationContext configApplicationContext
= new AnnotationConfigApplicationContext(ApplicationConfiguration.class);
//從spring容器中獲取bean
HelloService helloService = configApplicationContext.getBean(HelloService.class);
//方法呼叫
String value = helloService.sayHello();
System.*out*.printf(value);
}
}
專案結構如下:
執行結果:
以上是兩種啟動方式對比。