把如何创建Maven项目和创建Spring IOC的例子分享给大家,希望能对大家有帮助!

我的博客地址:https://www.cnblogs.com/themysteryofhackers/p/12006785.html

更新时间:2019-12-08

一、创建Maven项目

我用的是Intellij IDEA开发工具创建Maven项目的,打开该软件后,直接点击file --->project,如下图所示,

然后就直接跟着我的图片的步骤往下走。

到了这一个就创建好了Maven项目了,然后开发工具会在右下角提示下图的信息,直接点击自动导入就好。

然后就导入Spring IOC的项目依赖,可以去这个网站查找Maven依赖查找。然后在pom.xml文件先导入下面的依赖。

 <dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>

导入依赖后就创建包,创建包是为了更好的去管理Java类,创建好包之后就直接创建类,创建包和类的命名遵从Java命名规范即可。

创建好Student类后,然后在resources文件夹里面直接创建applicationContext.xml文件,最后在test下的java下创建一个包,在创建一个测试类,具体代码如下:

Student.java

package com.zzx.entity;

public class Student {
private Integer id;
private String name;
private Integer age;
private Integer sex;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", sex=" + sex +
", address='" + address + '\'' +
'}';
}
}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">
<!-- 把一个对象放进Spring容器 -->
<bean name="s1" class="com.zzx.entity.Student">
<property name="id" value="1"></property>
<property name="name" value="小红"></property>
<property name="age" value="18"></property>
<property name="sex" value="2"></property>
<property name="address" value="中国"></property>
</bean>
<!-- 把另一个对象也放到spring容器中,对象的名字不能重复,否则运行会报错 -->
<!-- 用property设置对象的属性,那该对象要有setter方法,还要有一个无参数的构造方法 -->
<bean name="s2" class="com.zzx.entity.Student">
<property name="id" value="2"></property>
<property name="name" value="小白"></property>
<property name="age" value="16"></property>
<property name="sex" value="1"></property>
<property name="address" value="中国"></property>
</bean>
</beans>

Test01.java

package com.zzx.ioc;

import com.zzx.entity.Student;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test01 {
@Test
public void studentTest(){
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Student s1 = applicationContext.getBean("s1", Student.class);
Student s2 = applicationContext.getBean("s2", Student.class);
System.out.println(s1);
System.out.println(s2);
}

}

最后,直接运行程序,这样一个简单的Spring IOC结合Maven的项目就完成了。

结尾

我是一个Java程序员,一个向往技术的小白,以后我会陆续将自己学习到的Java或者其他的知识会以博客的形式分享出来,希望能对大家有帮助。

喜欢小编的就给我一个关注吧!

如果有哪些问题、有哪些不妥或者侵犯到您的权益的地方,可以联系我,我马上修改。

如何创建Maven项目和Spring IOC例子的更多相关文章

  1. spring 学习(一):使用 intellijIDEA 创建 maven 工程进行 Spring ioc 测试

    spring学习(一):使用 intellijIDEA 创建 maven 工程进行 Spring ioc 测试 ioc 概念 控制反转(Inversion of Control,缩写为IOC),是面向 ...

  2. 通过idea创建Maven项目整合Spring+spring mvc+mybatis

    创建项目 File→new→project             然后就不断next直到项目面板出来 设置文件夹         注意:这里我个人习惯,在java下还建了ssm文件夹,然后再cont ...

  3. sts创建maven项目 引入spring,报错

    症状: Missing artifact org.springframework:spring-core:jar:5.0.0.RC3 原因: 在引入之前没有设置spring版本号 和spring ur ...

  4. IDEA一步步创建Maven管理的Spring入门程序

    目前,做Java开发的很多人都在使用IDEA了,而有些人也选择用Eclipse,我这里介绍一下IDEA一步步创建Maven项目的步骤,并创建一个Spring的入门程序(Java项目,非Web项目),讲 ...

  5. IDEA下创建Maven项目,并整合使用Spring、Spring MVC、Mybatis框架

    项目创建 本项目使用的是IDEA 2016创建. 首先电脑安装Maven,接着打开IDEA新建一个project,选择Maven,选择图中所选项,下一步. 填写好GroupId和ArtifactId, ...

  6. eclipse 创建maven 项目 动态web工程完整示例

    需求表均同springmvc案例 此处只是使用maven 注意,以下所有需要建立在你的eclipse等已经集成配置好了maven了,说白了就是新建项目的时候已经可以找到maven了 没有的话需要安装m ...

  7. eclipse 创建maven 项目 动态web工程完整示例 maven 整合springmvc整合mybatis

    接上一篇: eclipse 创建maven 项目 动态web工程完整示例 eclipse maven工程自动添加依赖设置 maven工程可以在线搜索依赖的jar包,还是非常方便的 但是有的时候可能还需 ...

  8. 【MAVEN】如何在Eclipse中创建MAVEN项目

    目录结构: contents structure [+] 1,Maven简介 2,Maven安装 2.1,下载Maven 2.2,配置环境变量 2.3,测试 3,Maven仓库 3.1,Maven仓库 ...

  9. 创建Maven项目出错

    有时候创建maven项目的时候会出错,例如在创建Spring cloud 2 项目的时候,会出现org.apache.maven.archiver.MavenArchiver.getManifest( ...

随机推荐

  1. Hackintosh相关资源站

    猫叔博客 https://www.maoshu.cc/ 大神RehabMan https://bitbucket.org/RehabMan/ dsdt/ssdt打补丁译文 https://blog.c ...

  2. Mysql多实例数据库安装应用

    第1章 MySQL多实例数据库企业级应用实践 1.1 MySQL多实例介绍 前文已经讲了为什么选择MySQL数据库,以及MySQL数据库在Linux系统下的多种安装方式,同时以单实例讲解了编译方式安装 ...

  3. matplotlib 画封闭图像并填充

    1.画矩形 这个费了我半天劲,不知怎么就可以了. 复制来自:https://www.cnblogs.com/ymjyqsx/p/7390288.html import  matplotlib.pypl ...

  4. 使用fetch进行数据请求时报json错误

    使用fetch进行数据请求返回response对象,response.json报错.原因是response中含有特殊字符. fetch(url).then(response => respons ...

  5. umi ui 构建时出现 spawn sh ENOENT 报错的解决方法

    在使用umi可视化界面构建项目的时候,如果出现spawn sh ENOENT错误,一般都是由于环境变量里没有设置git的环境变量导致的.在环境变量的path里加上"C:\Program Fi ...

  6. mysql 查询时间戳格式化 和thinkphp查询时间戳转换

    我在网上看了好多写的,都差不多,甚至好多都是一个人写的被别人转载啥的,哎 我写一个比较简单的 1.mysql语句 格式化时间戳 select id,name,FROM_UNIXTIME(time,'% ...

  7. Matlab filter常用函数

    Filtering and Analysis Functions Filtering Function Description fftfilt Filters a signal with a digi ...

  8. 【C语言】请输入一个n(n<=10)并输出一个n行n列的杨辉三角

    应用二维数组的知识 杨辉三角特点: 1.第一列和对角线的元素全部为1 2.其他元素等于上一行的当前列的值和上一行中当前列前边一列的值之和 #include<stdio.h> #define ...

  9. 【转载】Java多线程

    转自:http://www.jianshu.com/p/40d4c7aebd66 引 如果对什么是线程.什么是进程仍存有疑惑,请先Google之,因为这两个概念不在本文的范围之内. 用多线程只有一个目 ...

  10. jmeter+influxdb+granfana+collectd监控cpu+mem+TPS

    1.安装grafana #####gafana过期安装包安装报错 Error unpacking rpm package grafana-5.1.4-1.x86_64error: unpacking ...