1. 創建一個Maven的項目,我的項目結構如下: 2. 在pom文件里寫下需要導入的依賴: 3. 在Java文件夾下新建package,在package包下新建介面及其實現類 介面: 實現類: 4. 在resources目錄下新建 文件 我們需要在spring容器的配置文件中進行註冊該Bean s ...
- 創建一個Maven的項目,我的項目結構如下:
- 在pom文件里寫下需要導入的依賴:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.abc</groupId>
<artifactId>01-first(Spring)</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>6</source>
<target>6</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>10</maven.compiler.source>
<maven.compiler.target>10</maven.compiler.target>
<spring.version>5.1.0.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jcl</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.10</source>
<target>1.10</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
在Java文件夾下新建package,在package包下新建介面及其實現類
介面:
public interface SomeService { void doSome(); }
實現類:public class SomeServiceImpl implements SomeService { public SomeServiceImpl() { System.out.println("創建SomeServiceImpl對象"); } @Override public void doSome() { System.out.println("執行SomeServiceImpl里的doSome()方法"); } }
- 在resources目錄下新建
applicationContext.xml
文件
- 我們需要在spring容器的配置文件中進行註冊該Bean
spring使用的配置文件為xml文件,當然需要引入約束文件,一般將spring的配置文件命名為applicationContext.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--註冊Servcie其造價於如下代碼: SomeServiceImpl someService = new SomeServiceImpl(); 其底層是通過反射機制創建的someService對象 Object someService = Class.forName("com.abc.service.SomeServiceImpl").newInstance();--> <bean id="someService" class="com.abc.service.SomeServiceImpl"/> </beans>
- spring的根元素是benas顯然是註冊Bean,子標簽是Bean
- 註冊:
<bean id="someService" class="com.abc.service.SomeServiceImpl"/>
- id屬性為了唯一確定一個對象,class屬性裡邊應寫類全名
- 註冊完畢後我們要在測試類中獲取spring容器,而獲取Spring容器有兩種方式。
package com.abc.service;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class SomeServiceImplTTest {
// spring容器獲取的兩種方式
@Test
public void test01(){
//在類路徑下載入Spring配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//在當前目錄的根下載入該配置文件,路徑應為文件實際存放的目錄
ApplicationContext ac2 = new FileSystemXmlApplicationContext
("D:\\IDEA-workspace\\01-first(Spring)\\src\\main\\resources\\applicationContext.xml");
System.out.println(ac2);
}
@Test
public void test02() {
// 載入Spring配置文件,創建Spring容器對象
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//調用spring容器的getBean方法獲取carImpl,方法參數為bean的id
SomeService service = (SomeService) ac.getBean("SomeService");
service.doSome();
}
}
Spring入門程式到此就結束了