Spring核心功能IOC之HelloWorld

1 博客課程內容簡述

關於Spring的簡介請移步Spring官網或者我的另一篇博客Spring簡介進行查看,這篇博客內容主要介紹Spring IOC 快速入門,讓你通過簡單的代碼快速瞭解Spring IOC 到底是一個什麼東東。

2 Spring maven環境的搭建

第一步引入我們spring的核心依賴

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.1.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>5.1.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.1.4.RELEASE</version>
    </dependency>

然後在引入日誌和測試相關的依賴

	<dependency>
		<groupId>org.slf4j</groupId>
		<artifactId>slf4j-api</artifactId>
		<version>1.7.5</version>
		<scope>compile</scope>
	</dependency>
	<dependency>
		<groupId>org.slf4j</groupId>
		<artifactId>slf4j-log4j12</artifactId>
		<version>1.7.10</version>
	</dependency>
	<dependency>
		<groupId>log4j</groupId>
		<artifactId>log4j</artifactId>
		<version>1.2.14</version>
		<scope>runtime</scope>
	</dependency>
	<!-- Test Artifacts -->
  	<dependency>
	
	 <groupId>org.springframework</groupId>
		<artifactId>spring-test</artifactId>
		<version>5.1.4.RELEASE</version>
		<scope>test</scope>
	</dependency>
    <dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
	</dependency> 

在/springexamples/src/main/resources/下創建log4j.properties 內容如下:

log4j.rootCategory=INFO, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %40.40c:%4L - %m%n

在 /springexamples/src/main/resources/下創建Spring 配置文件ioc-helloworld-context.xml 內容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx.xsd ">

</beans>

3 IOC 之xml版本

首先創建我們的測試類 IocHelloWorldXml 裏面有一個方法say() 輸入內容爲:xml hello word… 具體內容如下:

package cn.zhuoqianmingyue.ioc;


public class IocHelloWorldXml {
	public void say() {
		System.out.println("xml hello word....");
	}
}

在spring配置文件中通過添加bean來配置我們的測試類具體方式如下:

 <bean id="iocHelloWorldXml"  class="cn.zhuoqianmingyue.ioc.IocHelloWorldXml"></bean> 

測試我們的配置是否生效:
測試類的具體代碼如下:

public class IocHelloWorldNotAnnotationTest {
	
	private static Logger log = LoggerFactory.getLogger(IocHelloWorldNotAnnotationTest.class);
	ApplicationContext appliction = null;
	@Before
	public void initApplication() {
		 appliction = new ClassPathXmlApplicationContext("ioc-helloworld-context.xml");
	}
	@Test
	public void classPathXml() {
		IocHelloWorldXml iocHelloWorld = (IocHelloWorldXml)appliction.getBean("iocHelloWorldXml");
		iocHelloWorld.say();
		IocHelloWorldAnnotation iocHelloWorldAnnotation = (IocHelloWorldAnnotation)appliction.getBean("iocHelloWorldAnnotation");
		iocHelloWorldAnnotation.say();
	}

測試結果:
在這裏插入圖片描述

4 IOC 之 註解版本

註解版相對與配置版更爲簡單首先在我們定義的測試類上聲明@Component()註解然後爲其制定名稱 例如:@Component(“iocHelloWorldAnnotation”) 這裏的iocHelloWorldAnnotation和下圖中是相同的。
在這裏插入圖片描述
具體代碼如下:

@Component("iocHelloWorldAnnotation")
public class IocHelloWorldAnnotation {
	public void say() {
		System.out.println("annotation hello word....");
	}
}

第二就是需要在spring的配置文件中添加 註解掃描的範圍具體代碼如下:

<context:component-scan base-package="cn.zhuoqianmingyue"></context:component-scan>

完成上面2步我們的IOC註解版本就搞定啦 接下來進行測試我們的註解配置,具體測試代碼如下:

	@Test
	public void classPathXml() {
		IocHelloWorldAnnotation iocHelloWorldAnnotation = (IocHelloWorldAnnotation)appliction.getBean("iocHelloWorldAnnotation");
		iocHelloWorldAnnotation.say();
	}

在這裏插入圖片描述
到此爲止我們的Spring IOC(控制反轉)helloword 演示完畢,覺得鄙人寫的還行就給點個讚唄。 後面我們將詳細介紹IOC 相關的內容。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章