简单的spring项目

尚硅谷Spring5框架教程(idea版) https://www.bilibili.com/video/BV1Vf4y127N5?p=1

1.使用Maven创建项目

使用快速开发模板

image-20211021224335880

2.目录结构如图

image-20211021223530097

3.定义user类

user类的内容

package com.atguigu.spring5;
public class User {
    public void add(){
        System.out.println("add~~~~~");
    }
}

4.定义bean1.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">
    <bean id="user" class="com.atguigu.spring5.User"></bean>
</beans>

5.pom.xml 文件中下载对应依赖

实现一个简单的maven的spring项目,要导入的几个依赖

一共五个包,主要有四个核心依赖 beans,core,context,expression,以及一个关于 日志的jar包,

关于依赖的版本可以在mvn的官网查看依赖的语句

步骤1.点击查找到的mvn仓库
image-20211021225559177

步骤2.点击spring.context
image-20211021225836360
步骤3.选择想要下载的依赖的版本
image-20211021225938442

步骤4.查看依赖的代码并加入到pom.xml中
image-20211021231223127

ependencies>
   <!--  WICKET DEPENDENCIES -->
   <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
   <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>5.2.8.RELEASE</version>
   </dependency>
   <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
   <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.8.RELEASE</version>
   </dependency>
   <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
   <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.2.8.RELEASE</version>
   </dependency>
   <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
   <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>5.2.8.RELEASE</version>
   </dependency>
   <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
   <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.2</version>
   </dependency>
 </dependencies>

pom.xml的完整内容

<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/maven-v4_0_0.xsd">

	<modelVersion>4.0.0</modelVersion>
	<groupId>org.example</groupId>
	<artifactId>spring5_s</artifactId>
	<packaging>war</packaging>
	<version>1.0-SNAPSHOT</version>
	<!-- TODO project name  -->
	<name>quickstart</name>
	<description></description>

	<!-- TODO
		<organization>
		<name>company name</name>
		<url>company url</url>
		</organization>
	-->

	<licenses>
		<license>
			<name>The Apache Software License, Version 2.0</name>
			<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
			<distribution>repo</distribution>
		</license>
	</licenses>

	<dependencies>
		<!--  WICKET DEPENDENCIES -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>5.2.8.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>5.2.8.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>5.2.8.RELEASE</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-expression</artifactId>
			<version>5.2.8.RELEASE</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.2</version>
		</dependency>

		<dependency>
			<groupId>org.apache.wicket</groupId>
			<artifactId>wicket</artifactId>
			<version>${wicket.version}</version>
		</dependency>
		<!-- OPTIONAL 
			<dependency>
			<groupId>org.apache.wicket</groupId>
			<artifactId>wicket-extensions</artifactId>
			<version>${wicket.version}</version>
			</dependency>
		-->

		<!-- LOGGING DEPENDENCIES - LOG4J -->

		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.4.2</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.14</version>
		</dependency>

		<!--  JUNIT DEPENDENCY FOR TESTING -->
		 <dependency>
				 <groupId>junit</groupId>
				 <artifactId>junit</artifactId>
				 <version>3.8.2</version>
				 <scope>test</scope>
		 </dependency>

		<!--  JETTY DEPENDENCIES FOR TESTING  -->

		<dependency>
			<groupId>org.mortbay.jetty</groupId>
			<artifactId>jetty</artifactId>
			<version>${jetty.version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.mortbay.jetty</groupId>
			<artifactId>jetty-util</artifactId>
			<version>${jetty.version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.mortbay.jetty</groupId>
			<artifactId>jetty-management</artifactId>
			<version>${jetty.version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>RELEASE</version>
			<scope>compile</scope>
		</dependency>
	</dependencies>

	<build>
		<resources>
			<resource>
				<filtering>false</filtering>
				<directory>src/main/resources</directory>
			</resource>
			<resource>
				<filtering>false</filtering>
				<directory>src/main/java</directory>
				<includes>
					<include>**</include>
				</includes>
				<excludes>
					<exclude>**/*.java</exclude>
				</excludes>
			</resource>
		</resources>
		<testResources>
			<testResource>
				<filtering>false</filtering>
				<directory>src/test/java</directory>
				<includes>
					<include>**</include>
				</includes>
				<excludes>
					<exclude>**/*.java</exclude>
				</excludes>
			</testResource>
		</testResources>
		<plugins>
			<plugin>
				<groupId>org.mortbay.jetty</groupId>
				<artifactId>maven-jetty-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

	<properties>
		<wicket.version>1.3.2</wicket.version>
		<jetty.version>6.1.4</jetty.version>
	</properties>
</project>

6.TestSpring类中的内容

package com.atguigu.spring5.testdemo;

import com.atguigu.spring5.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring {
    @Test
    public void test() {
        //1 加载spring配置文件
        ApplicationContext context=
                new ClassPathXmlApplicationContext("bean1.xml");
        //2 获取配置的对象
        User user=context.getBean("user", User.class);
        System.out.println(user);
        user.add();
    }
}

运行结果
image-20211021230436307

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值