如何创建Maven项目和Spring IOC例子
把如何创建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例子的更多相关文章
- spring 学习(一):使用 intellijIDEA 创建 maven 工程进行 Spring ioc 测试
spring学习(一):使用 intellijIDEA 创建 maven 工程进行 Spring ioc 测试 ioc 概念 控制反转(Inversion of Control,缩写为IOC),是面向 ...
- 通过idea创建Maven项目整合Spring+spring mvc+mybatis
创建项目 File→new→project 然后就不断next直到项目面板出来 设置文件夹 注意:这里我个人习惯,在java下还建了ssm文件夹,然后再cont ...
- sts创建maven项目 引入spring,报错
症状: Missing artifact org.springframework:spring-core:jar:5.0.0.RC3 原因: 在引入之前没有设置spring版本号 和spring ur ...
- IDEA一步步创建Maven管理的Spring入门程序
目前,做Java开发的很多人都在使用IDEA了,而有些人也选择用Eclipse,我这里介绍一下IDEA一步步创建Maven项目的步骤,并创建一个Spring的入门程序(Java项目,非Web项目),讲 ...
- IDEA下创建Maven项目,并整合使用Spring、Spring MVC、Mybatis框架
项目创建 本项目使用的是IDEA 2016创建. 首先电脑安装Maven,接着打开IDEA新建一个project,选择Maven,选择图中所选项,下一步. 填写好GroupId和ArtifactId, ...
- eclipse 创建maven 项目 动态web工程完整示例
需求表均同springmvc案例 此处只是使用maven 注意,以下所有需要建立在你的eclipse等已经集成配置好了maven了,说白了就是新建项目的时候已经可以找到maven了 没有的话需要安装m ...
- eclipse 创建maven 项目 动态web工程完整示例 maven 整合springmvc整合mybatis
接上一篇: eclipse 创建maven 项目 动态web工程完整示例 eclipse maven工程自动添加依赖设置 maven工程可以在线搜索依赖的jar包,还是非常方便的 但是有的时候可能还需 ...
- 【MAVEN】如何在Eclipse中创建MAVEN项目
目录结构: contents structure [+] 1,Maven简介 2,Maven安装 2.1,下载Maven 2.2,配置环境变量 2.3,测试 3,Maven仓库 3.1,Maven仓库 ...
- 创建Maven项目出错
有时候创建maven项目的时候会出错,例如在创建Spring cloud 2 项目的时候,会出现org.apache.maven.archiver.MavenArchiver.getManifest( ...
随机推荐
- hzq84621巨佬的语录
摘自诸中培训讲图论时: 1.光图论考不出什么东西,一般作为DP的附庸出现. 2.如果不是骗骗不承认SPFA的外国人,一般能用dij就用dij. 3.那个东西(指bellman-ford)除了判负环没什 ...
- python笔记20(面向对象课程二)
今日内容 类成员 成员修饰符 内容回顾 & 补充 三大特性 封装 函数封装到类 数据封装到对象 * class Foo: def __init__(self,name,age): self.n ...
- python面试的100题(15)
41.super函数的具体用法和场景 为了调用父类(超类)的一个方法,可以使用 super() 函数,比如: class A: def spam(self): print('A.spam') clas ...
- 栈的简单应用之中缀表达式转后缀表达式(C语言实现逆波兰式)
一.前言 普通人在书写计算式时会选择中缀表达式,这样符合人脑的认知习惯.可计算机处理时后缀表达式才能使处理速度更快,其原因是利用堆栈结构减少计算机内存访问.同时它也是一个很好锻炼栈这个数据结构的应 ...
- FactoryBean的作用
Spring 中有两种类型的Bean,一种是普通Bean,另一种是工厂Bean 即 FactoryBean.FactoryBean跟普通Bean不同,其返回的对象不是指定类的一个实例,而是该Facto ...
- python,装饰器带参数,原理
import time # 函数装饰器传参 def zhuang1(hao): def zhuang2(func1): def funczhuang(*args, **kw): print(time. ...
- codeforces 1282 E. The Cake Is a Lie (dfs+构造)
链接:https://codeforces.com/contest/1282/problem/E 题意:给的是一张平面图,是一个n边形,每次可以切一刀,切出一个三角形,最终切成n-2个三角形.题目给出 ...
- js -- 车牌号对应的归属地js文件
/*车牌号对应的归属地*/ let cardCallerloc = new Map(); // 北京市(京) cardCallerloc.set("京A", "北京市&q ...
- 九、c++容器
9.1 简介 容器库是类模板与算法的汇集,允许程序员简单地访问常见数据结构,例如队列.链表和栈. 有三类容器--顺序容器.关联容器和无序关联容器--每种都被设计为支持不同组的操作. 顺序容器:顺序容器 ...
- CompletableFuture--给future调用增加回调或聚合操作
CompletableFuture--增大内存节省时间.整合多个future调用,来减少时间 例如:第一个future 返回时1s,第二个返回时2s,第三个返回是3s CompletableFut ...