IDEA下——Spring入門程式
建立一個Maven的專案,我的專案結構如下:
在pom檔案裡寫下需要匯入的依賴:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="" xmlns:xsi="" xsi:schemaLocation=" "> <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="" xmlns:xsi="" xsi:schemaLocation=" /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入門程式到此就結束了
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/964/viewspace-2817994/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Spring MVC 入門程式SpringMVC
- 3. Spring 的入門程式Spring
- spring-boot入門程式詳解Springboot
- Spring Boot入門(一):使用IDEA建立Spring Boot專案並使用yaml配置檔案Spring BootIdeaYAML
- Spring 入門Spring
- 【IntelliJ Idea 軟體下載與入門級安裝教程】IntelliJIdea
- spring入門(二)Spring
- 1、Spring入門Spring
- Spring入門指南Spring
- spring 快速入門Spring
- Spring Boot入門Spring Boot
- GIT從入門到入土(IDEA整合)GitIdea
- .NET程式設計師如何快入門Spring Boot程式設計師Spring Boot
- Spring Security 快速入門Spring
- Spring Security(一)入門Spring
- Spring Cloud Gateway 入門SpringCloudGateway
- Spring Boot HttpExchange 入門Spring BootHTTP
- #Spring JdbcTemplate入門@FDDLCSpringJDBC
- spring mvc 快速入門SpringMVC
- Spring Boot Dubbo 入門Spring Boot
- 入門Spring6Spring
- spring boot快速入門Spring Boot
- Spring Security 入門篇Spring
- spring入門學習Spring
- Spring入門(一):建立Spring專案Spring
- Spring Cloud Gateway入門 - spring.ioSpringCloudGateway
- 如何使用 IDEA 建立 Java 入門應用IdeaJava
- Spring Boot (一)快速入門Spring Boot
- Spring Boot 2.0.1 入門教程Spring Boot
- Spring Boot整合hessian入門Spring Boot
- Spring Cloud Gateway 入門案例SpringCloudGateway
- Spring Boot的Kafka入門Spring BootKafka
- Spring入門配置(一) - IOCSpring
- Spring Shell入門介紹Spring
- spring入門aop和iocSpring
- spring+motan+zookeeper入門Spring
- Go-Spring入門篇GoSpring
- Spring Cloud Alibaba入門篇SpringCloud