1、先配置spring、springmvc通用xml文件的头部信息

spring、springmvc通用xml文件的模板内容如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:aop="http://www.springframework.org/schema/aop"
5 xmlns:context="http://www.springframework.org/schema/context"
6 xmlns:tx="http://www.springframework.org/schema/tx"
7 xmlns:mvc="http://www.springframework.org/schema/mvc"
8 xsi:schemaLocation="http://www.springframework.org/schema/beans
9 http://www.springframework.org/schema/beans/spring-beans.xsd
10 http://www.springframework.org/schema/aop
11 http://www.springframework.org/schema/aop/spring-aop.xsd
12 http://www.springframework.org/schema/context
13 http://www.springframework.org/schema/context/spring-context.xsd
14 http://www.springframework.org/schema/tx
15 http://www.springframework.org/schema/tx/spring-tx.xsd
16 http://www.springframework.org/schema/mvc
17 http://www.springframework.org/schema/mvc/spring-mvc.xsd
18 ">
19
20 </beans>

spring、springmvc文件通用内容

spring、spring

2、构建spring的第一个工程

3、配置项目中的pom.xml文件依赖

  1 <?xml version="1.0" encoding="UTF-8"?>
2
3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5 <modelVersion>4.0.0</modelVersion>
6
7 <groupId>com</groupId>
8 <artifactId>springmybatis_day44_02</artifactId>
9 <version>1.0-SNAPSHOT</version>
10 <packaging>war</packaging>
11
12 <name>springmybatis_day44_02 Maven Webapp</name>
13 <!-- FIXME change it to the project's website -->
14 <url>http://www.example.com</url>
15
16 <properties>
17 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18 <maven.compiler.source>1.7</maven.compiler.source>
19 <maven.compiler.target>1.7</maven.compiler.target>
20 </properties>
21
22
23 <dependencies>
24
25 <dependency>
26 <groupId>junit</groupId>
27 <artifactId>junit</artifactId>
28 <version>4.11</version>
29 </dependency>
30
31 <!--1 https://mvnrepository.com/artifact/org.springframework/spring-beans -->
32 <dependency>
33 <groupId>org.springframework</groupId>
34 <artifactId>spring-beans</artifactId>
35 <version>4.3.6.RELEASE</version>
36 </dependency>
37
38
39 <!--2 https://mvnrepository.com/artifact/org.springframework/spring-context -->
40 <dependency>
41 <groupId>org.springframework</groupId>
42 <artifactId>spring-context</artifactId>
43 <version>4.3.6.RELEASE</version>
44 </dependency>
45
46
47 <!-- 3https://mvnrepository.com/artifact/org.springframework/spring-core -->
48 <dependency>
49 <groupId>org.springframework</groupId>
50 <artifactId>spring-core</artifactId>
51 <version>4.3.6.RELEASE</version>
52 </dependency>
53
54
55 <!--4 https://mvnrepository.com/artifact/org.springframework/spring-expression -->
56 <dependency>
57 <groupId>org.springframework</groupId>
58 <artifactId>spring-expression</artifactId>
59 <version>4.3.6.RELEASE</version>
60 </dependency>
61
62
63 <!--5 https://mvnrepository.com/artifact/org.springframework/spring-aop -->
64 <dependency>
65 <groupId>org.springframework</groupId>
66 <artifactId>spring-aop</artifactId>
67 <version>4.3.6.RELEASE</version>
68 </dependency>
69
70
71 <!--6 https://mvnrepository.com/artifact/aopalliance/aopalliance -->
72 <dependency>
73 <groupId>aopalliance</groupId>
74 <artifactId>aopalliance</artifactId>
75 <version>1.0</version>
76 </dependency>
77
78
79 <!--7 https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
80 <dependency>
81 <groupId>org.aspectj</groupId>
82 <artifactId>aspectjweaver</artifactId>
83 <version>1.8.10</version>
84 </dependency>
85
86
87 <!--8 https://mvnrepository.com/artifact/log4j/log4j -->
88 <dependency>
89 <groupId>log4j</groupId>
90 <artifactId>log4j</artifactId>
91 <version>1.2.17</version>
92 </dependency>
93
94
95 <!--9 https://mvnrepository.com/artifact/commons-logging/commons-logging -->
96 <dependency>
97 <groupId>commons-logging</groupId>
98 <artifactId>commons-logging</artifactId>
99 <version>1.2</version>
100 </dependency>
101
102
103 <!--10 https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
104 <dependency>
105 <groupId>org.springframework</groupId>
106 <artifactId>spring-jdbc</artifactId>
107 <version>4.3.6.RELEASE</version>
108 </dependency>
109
110
111 <!--11 https://mvnrepository.com/artifact/org.springframework/spring-orm -->
112 <dependency>
113 <groupId>org.springframework</groupId>
114 <artifactId>spring-orm</artifactId>
115 <version>4.3.6.RELEASE</version>
116 </dependency>
117
118
119 <!--12 https://mvnrepository.com/artifact/org.springframework/spring-tx -->
120 <dependency>
121 <groupId>org.springframework</groupId>
122 <artifactId>spring-tx</artifactId>
123 <version>4.3.6.RELEASE</version>
124 </dependency>
125
126
127 <!--13 https://mvnrepository.com/artifact/org.springframework/spring-web -->
128 <dependency>
129 <groupId>org.springframework</groupId>
130 <artifactId>spring-web</artifactId>
131 <version>4.3.6.RELEASE</version>
132 </dependency>
133
134
135 <!--14 https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
136 <dependency>
137 <groupId>org.springframework</groupId>
138 <artifactId>spring-webmvc</artifactId>
139 <version>4.3.6.RELEASE</version>
140 </dependency>
141
142
143 <!--15 https://mvnrepository.com/artifact/org.mybatis/mybatis -->
144 <dependency>
145 <groupId>org.mybatis</groupId>
146 <artifactId>mybatis</artifactId>
147 <version>3.4.6</version>
148 </dependency>
149
150 <!--17 https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
151 <dependency>
152 <groupId>org.mybatis</groupId>
153 <artifactId>mybatis-spring</artifactId>
154 <version>1.3.2</version>
155 </dependency>
156
157
158 <!--16 https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
159 <dependency>
160 <groupId>mysql</groupId>
161 <artifactId>mysql-connector-java</artifactId>
162 <version>5.1.38</version>
163 </dependency>
164
165
166 <!--18 https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
167 <dependency>
168 <groupId>javax.servlet</groupId>
169 <artifactId>javax.servlet-api</artifactId>
170 <version>3.1.0</version>
171 </dependency>
172
173 <!--19 https://mvnrepository.com/artifact/javax.servlet/jstl -->
174 <dependency>
175 <groupId>javax.servlet</groupId>
176 <artifactId>jstl</artifactId>
177 <version>1.2</version>
178 </dependency>
179
180
181 <!--20 https://mvnrepository.com/artifact/taglibs/standard -->
182 <dependency>
183 <groupId>taglibs</groupId>
184 <artifactId>standard</artifactId>
185 <version>1.1.2</version>
186 </dependency>
187
188 </dependencies>
189
190
191 </project>

pom.xml

4、在项目的main下创建java和resouces目录

5、将java目录变为编译目录

6、将resources目录变为存放xml脚本的目录

7、在java的com.pojo包下构建Student和Classes两个类

 1 package com.pojo;
2
3 public class Classes {
4 private Integer cid;
5 private String cname;
6
7 public Classes() {
8 }
9
10 public Classes(Integer cid, String cname) {
11 this.cid = cid;
12 this.cname = cname;
13 }
14
15 public Integer getCid() {
16 return cid;
17 }
18
19 public void setCid(Integer cid) {
20 this.cid = cid;
21 }
22
23 public String getCname() {
24 return cname;
25 }
26
27 public void setCname(String cname) {
28 this.cname = cname;
29 }
30
31 @Override
32 public String toString() {
33 return "Classes{" +
34 "cid=" + cid +
35 ", cname='" + cname + '\'' +
36 '}';
37 }
38 }

Classes.java

 1 package com.pojo;
2
3 public class Student {
4 private Integer sid;
5 private String sname;
6 private String course;
7 private Double score;
8
9 private Classes classes;
10
11 public Student() {
12 }
13
14 public Student(Integer sid, String sname, String course, Double score, Classes classes) {
15 this.sid = sid;
16 this.sname = sname;
17 this.course = course;
18 this.score = score;
19 this.classes = classes;
20 }
21
22 public Integer getSid() {
23 return sid;
24 }
25
26 public void setSid(Integer sid) {
27 this.sid = sid;
28 }
29
30 public String getSname() {
31 return sname;
32 }
33
34 public void setSname(String sname) {
35 this.sname = sname;
36 }
37
38 public String getCourse() {
39 return course;
40 }
41
42 public void setCourse(String course) {
43 this.course = course;
44 }
45
46 public Double getScore() {
47 return score;
48 }
49
50 public void setScore(Double score) {
51 this.score = score;
52 }
53
54 public Classes getClasses() {
55 return classes;
56 }
57
58 public void setClasses(Classes classes) {
59 this.classes = classes;
60 }
61
62 @Override
63 public String toString() {
64 return "Student{" +
65 "sid=" + sid +
66 ", sname='" + sname + '\'' +
67 ", course='" + course + '\'' +
68 ", score=" + score +
69 ", classes=" + classes +
70 '}';
71 }
72 }

Student.java

8、在resouces下创建spring的applicationContext.xml文件

 1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:aop="http://www.springframework.org/schema/aop"
5 xmlns:context="http://www.springframework.org/schema/context"
6 xmlns:tx="http://www.springframework.org/schema/tx"
7 xmlns:mvc="http://www.springframework.org/schema/mvc"
8 xsi:schemaLocation="http://www.springframework.org/schema/beans
9 http://www.springframework.org/schema/beans/spring-beans.xsd
10 http://www.springframework.org/schema/aop
11 http://www.springframework.org/schema/aop/spring-aop.xsd
12 http://www.springframework.org/schema/context
13 http://www.springframework.org/schema/context/spring-context.xsd
14 http://www.springframework.org/schema/tx
15 http://www.springframework.org/schema/tx/spring-tx.xsd
16 http://www.springframework.org/schema/mvc
17 http://www.springframework.org/schema/mvc/spring-mvc.xsd
18 ">
19 <!--bean节点功能等同于 Classes classes=new Classes();-->
20 <!--spring使用反射+代理的方式创建-->
21 <!--底层代理源码的实现原理:-->
22 <!--Class cla=Class.forName("com.pojo.Classes");-->
23 <!--Classes classes=cla.newInstance();-->
24 <!--id是spring底层构建对象的对象名,classes是构建对象的类的全路径-->
25 <!--id的值和vlaue值都是任意的-->
26 <bean id="classes" class="com.pojo.Classes">
27 <!--底层代理使用反射实现机制-->
28 <!--Field cidField= cla.getField(cid);-->
29 <!--cidField.set(classes,1);-->
30 <!--property类的属性名,value属性值-->
31 <property name="cid" value="1"/>
32 <property name="cname" value="教学部"/>
33 </bean>
34
35 <bean id="student" class="com.pojo.Student">
36 <property name="sid" value="10"/>
37 <property name="sname" value="holly"/>
38 <property name="course" value="java"/>
39 <property name="score" value="65.5"/>
40 <!--如果给引用类型变量(对象属性)赋值,使用ref-,ref的值是另一个bean的id值-->
41 <property name="classes" ref="classes"/>
42 </bean>
43 </beans>

applicationContext.xml

9、在java的com.test包下构建StudentTest.java测试类

 1 package com.test;
2
3 import com.pojo.Classes;
4 import com.pojo.Student;
5 import org.junit.Test;
6 import org.springframework.context.ApplicationContext;
7 import org.springframework.context.support.ClassPathXmlApplicationContext;
8
9 //常规:需要对象时,主动创建,主动赋值,
10 //springIOC:需要对象时,被动接受完整对象(spring对我们所需要的对象进行赋值)
11 public class StudentTest {
12 @Test
13 public void normal(){
14 Classes classes=new Classes();
15 classes.setCid(1);
16 classes.setCname("教学部");
17
18 Student student=new Student();
19 student.setSid(10);
20 student.setSname("holly");
21 student.setCourse("java");
22 student.setScore(65.5);
23 student.setClasses(classes);
24
25 System.out.println(classes);
26 System.out.println(student);
27 }
28
29 @Test
30 public void springIOC(){
31 //加载spring的xml文件
32 ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
33
34 //spring给我们对象注入需要的值,我们根据bean 的id去查找赋值
35 Classes classes=(Classes) ac.getBean("classes");
36 Student student =(Student) ac.getBean("student");
37
38 System.out.println(classes);
39 System.out.println(student);
40 }
41 }

StudentTest.java

10、运行我们的测试方法且查看我们的打印效果

声明:此为原创,源码+理论讲解视频+操作视频,请私信作者!

1、spingIOC+IDEA+Maven的更多相关文章

  1. 170807、intellij idea maven集成lombok实例

    简介: lombok 通过简单注解方式简化java代码.(如消除实体对象的get/setter方法.日志对象声明等...) 安装步骤: 1.选择支持注解处理:Settings-->Build-- ...

  2. JsonObject没有fromObject、idea引入maven有红线没依赖、JsonObject maven 依赖包

    目录: 1.JsonObject maven 依赖包 2.idea引入maven有红线,没依赖 3.JsonObject没有fromObject \\\\\\\\\\\\\\\\\\\\\\\ 1.J ...

  3. 【IDEA使用技巧】(4) —— IDEA 构建Java Maven项目、导入Eclipse项目、多Module Maven项目

    1.IntelliJ IDEA构建Java Maven项目 1.1. IDEA构建Java Maven项目 ①选择Create New Project,选择创建Maven项目,并勾选Create fr ...

  4. 基于分布式、服务化的maven项目文件规划

    引言 此文不是纯粹介绍maven概念,而是介绍一个具体的maven项目文件规划 这个规划可能适合于研发比较复杂的业务,这些业务有分布式和服务化的需要. 这个规划能够解决因为分布式和服务化要求而引起的项 ...

  5. 3、eclipse和maven环境安装以及HDFS读写的demo

    安装eclipse和maven环境   $ mkdir /home/beifeng/.m2 $ tar zxf repository.tar.gz -C /home/beifeng/.m2 $ /co ...

  6. 二、 What's Maven,How to learning?

    1. 哈哈,什么是Maevn, ←_←|| ?我怎么知道,来看看官方解释, Apache Maven is a software project management and comprehensio ...

  7. 常用工具说明--mongodb、mysql解压版、IDEA配置maven

    Mongodb的安装.配置 1.去官网下载mongodb安装包,mongodb官网.点击右上角的 Download,下载对应的msi安装包 2.安装程序,选择 Custom,自定义安装路径,比如安装在 ...

  8. 170310、Jenkins部署Maven多环境项目(dev、beta、prod)的参数设置

    使用Jenkins配置Git+Maven的自动化构建: http://blog.csdn.net/xlgen157387/article/details/50353317 在一个多开发和生产环境的项目 ...

  9. 5、SpringBoot+MyBaits+Maven+Idea+pagehelper分页插件

    1.为了我们平时方便开发,我们可以在同一个idea窗口创建多个项目模块,创建方式如下 2.项目中pom.xm文件的内容如下 <?xml version="1.0" encod ...

随机推荐

  1. Java_Scanner的使用

    目录 Scanner对象 scanner.next()和scanner.nextln()的区别 scanner.hasNext()和scanner.hasNextln() Scanner拓展 视频课程 ...

  2. python求最大公约数和最小公倍数

    1 def gcd(x,y): 2 while(y): 3 t=x%y 4 x=y 5 y=t 6 #print("最小公倍数是:",x*y/x)#最小公倍数是两数之积除以最大公约 ...

  3. SpringBoot2.x整合Prometheus+Grafana【附源码+视频】

    图文并茂,新手入门教程,建议收藏 SpringBoot2.x整合Prometheus+Grafana[附源码+视频] 附源码+视频 目录 工程简介 简介 Prometheus grafana Spri ...

  4. [自制操作系统] 第05回 CPU的三种模式

    目录 一.前景回顾 二.实模式和保护模式 一.前景回顾 在之前我们说到,loader的作用才是读取加载操作系统内核,那么我们的重心就应该是loader.S文件,其实我们接下来也的确是会往loader. ...

  5. 【Redis】Redis Cluster初始化及PING消息的发送

    Cluster消息类型定义 #define CLUSTERMSG_TYPE_PING 0 /* Ping消息类型,节点间进行通信交换信息的消息 */ #define CLUSTERMSG_TYPE_P ...

  6. Servlet之Request和Response 解析

    原理 tomcat服务器会根据请求url中的资源路径,创建对应的Servlet的对象 tomcat服务器.会创建request和response对象,request对象中封装请求消息数据. tomca ...

  7. Pyinstaller打包pikepdf失败的问题排查

    问题 最近在项目里用到了pikepdf这个库,用于实现pdf水印插入的一个小功能,源码调试阶段运行一切OK但是在出包时报了如下异常. Traceback (most recent call last) ...

  8. PTA(BasicLevel)-1031 查验身份证

    一.问题定义 一个合法的身份证号码由17位地区.日期编号和顺序编号加1位校验码组成.校验码的计算规则如下:首先对前17位数字加权求和,权重分配为:{7,9,10,5,8,4,2,1,6,3,7,9,1 ...

  9. PoweJob高级特性-MapReduce完整示例

    由于网上搜索 PowerJob MapReduce 都是设计原理,demo也展示个空壳子,没有演示Map到Reduce结果怎么传递,对于没有MR开发经验的人来说并没有什么帮助,所以这里写了一个有完整计 ...

  10. @PostConstruct +getapplicationcontext.getbean springboot获取getBean

    Componentpublic class SpringContextUtils implements ApplicationContextAware { public static Applicat ...