spring源码第一天,300行代码体验spring核心

本文通过300行代码详细介绍了Spring的核心实现,从配置阶段开始,包括web.xml的设定、自定义注解的创建,如@GPAutowired等,再到初始化阶段,涉及application.properties配置、GPDispatcherServlet的继承与重写,以及自动化依赖注入、HandlerMapping的初始化等关键步骤。最后通过编写DemoAction进行测试,提供了源码和视频资源供读者深入学习。
摘要由CSDN通过智能技术生成

一:实现思路

pom文件

<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.gupaoedu.vip.gpmvc</groupId>
	<artifactId>gupaoedu-vip-spring</artifactId>
	<version>1.0</version>
	<packaging>war</packaging>

    
	<properties>
		<!-- dependency versions -->
		<servlet.api.version>2.4</servlet.api.version>
	</properties>


	<dependencies>
		
		<!-- requied start -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>${servlet.api.version}</version>
			<scope>provided</scope>
		</dependency>
		<!-- requied end -->
		
	</dependencies>

	<build>
		<finalName>${artifactId}</finalName>
		<resources>
			<resource>
				<directory>${basedir}/src/main/resources</directory>
				<includes>
					<include>**/*</include>
				</includes>
			</resource>
			<resource>
				<directory>${basedir}/src/main/java</directory>
				<excludes>
					<exclude>**/*.java</exclude>
					<exclude>**/*.class</exclude>
				</excludes>
			</resource>
		</resources>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
					<encoding>UTF-8</encoding>
					<compilerArguments>
						<verbose />
						<bootclasspath>${java.home}/lib/rt.jar</bootclasspath>
					</compilerArguments>
				</configuration>
			</plugin>

			<plugin>
				<artifactId>maven-resources-plugin</artifactId>
				<version>2.5</version>
				<executions>
					<execution>
						<id>copy-resources</id>
						<!-- here the phase you need -->
						<phase>validate</phase>
						<goals>
							<goal>copy-resources</goal>
						</goals>
						<configuration>
							<encoding>UTF-8</encoding>
							<outputDirectory>${basedir}/target/classes</outputDirectory>
							<resources>
								<resource>
									<directory>src/main/resources</directory>
									<includes>
										<include>**/*.*</include>
									</includes>
									<filtering>true</filtering>
								</resource>
							</resources>
						</configuration>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.mortbay.jetty</groupId>
				<artifactId>maven-jetty-plugin</artifactId>
				<version>6.1.26</version>
				<configuration>
					<webDefaultXml>src/main/resources/webdefault.xml</webDefaultXml>
					<contextPath>/</contextPath>
					<connectors>
						<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
							<port>8080</port>
						</connector>
					</connectors>
					<scanIntervalSeconds>0</scanIntervalSeconds>
					<scanTargetPatterns>
						<scanTargetPattern>
							<directory>src/main/webapp</directory>
							<includes>
								<include>**/*.xml</include>
								<include>**/*.properties</include>
							</includes>
						</scanTargetPattern>
					</scanTargetPatterns>
					<systemProperties>
						<systemProperty>
							<name>
								javax.xml.parsers.DocumentBuilderFactory
							</name>
							<value>
								com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
							</value>
						</systemProperty>
						<systemProperty>
				
package cn.javass.spring.chapter4; import java.io.File; import java.io.IOException; import junit.framework.Assert; import org.jboss.vfs.VFS; import org.jboss.vfs.VirtualFile; import org.jboss.vfs.spi.RealFileSystem; import org.junit.Test; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; @SuppressWarnings("all") public class ResourcePatternTest { @Test public void testClasspathPrefix() throws IOException { ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); //只加载一个绝对匹配Resource,且通过ResourceLoader.getResource进加载 Resource[] resources = resolver.getResources("classpath:META-INF/INDEX.LIST"); Assert.assertEquals(1, resources.length); //只加载一个匹配的Resource,且通过ResourceLoader.getResource进加载 resources = resolver.getResources("classpath:META-INF/*.LIST"); Assert.assertTrue(resources.length == 1); //只加载一个绝对匹配Resource,且通过ResourceLoader.getResource进加载 resources = resolver.getResources("classpath:META-INF/MANIFEST.MF"); Assert.assertEquals(1, resources.length); } @Test public void testClasspathAsteriskPrefix() throws IOException { ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); //将加载多个绝对匹配的所有Resource //将首先通过ClassLoader.getResources("META-INF")加载非模式路径部分 //然后进遍历模式匹配 Resource[] resources = resolver.getResources("classpath*:META-INF/INDEX.LIST"); Assert.assertTrue(resources.length > 1); //将加载多个模式匹配的Resource resources = resolver.getResources("classpath*:META-INF/*.LIST"); Assert.assertTrue(resources.length > 1); } @Test public void testClasspathAsteriskPrefixLimit() throws IOException { ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); //将首先通过ClassLoader.getResources("")加载目录, //将只返回文件系统的类路径不返回jar的跟路径 //然后进遍历模式匹配 Resource[] resources = resolver.getResources("classpath*:asm-*.txt"); Assert.assertTrue(resources.length == 0); //将通过ClassLoader.getResources("asm-license.txt")加载 //asm-license.txt存在于com.springsource.net.sf.cglib-2.2.0.jar resources = resolver.getResources("classpath*:asm-license.txt"); Assert.assertTrue(resources.length > 0); //将只加载文件系统类路径匹配的Resource resources = resolver.getResources("classpath*:LICENS*"); Assert.assertTrue(resources.length == 1); } @Test public void testFilekPrefix() throws IOException { ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); Resource[] resources = resolver.getResources("file:D:/*.txt"); Assert.assertTrue(resources.length > 0); } @Test public void testVfsPrefix() throws IOException { //1.创建一个虚拟的文件目录 VirtualFile home = VFS.getChild("/home"); //2.将虚拟目录映射到物理的目录 VFS.mount(home, new RealFileSystem(new File("d:"))); //3.通过虚拟目录获取文件资源 VirtualFile testFile = home.getChild("test.txt"); ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); Resource[] resources = resolver.getResources("/home/test.txt"); Assert.assertTrue(resources.length > 0); System.out.println(resources[0].getClass()); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值