前言
SSM一般来讲就是指的S(spring)S(springMVC)M(mybatis)
mybatis就不再多讲了,之前已经写过,mybatis在我看来是封装了sql简化了程序的复杂度
springMVC架构是一种设计思想,拦截用户请求,并处理请求
spring框架的优点就是控制反转、依赖注入,能够使代码耦合度降低
一、准备
1.1新建一个Maven工程(maven project)
在pom文件中引入相关jar包
<!-- 集中定义依赖版本号 -->
<properties>
<junit.version>4.10</junit.version>
<spring.version>4.1.3.RELEASE</spring.version>
</properties>
<dependencies>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
下载完成后如下
1.2创建spring配置文件
applicationContext.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-4.0.xsd">
</beans>
接着创建java类
package com.springstudy;
public class HelloWorld {
public void hello() {
System.out.println("hello");
}
}
然后再xml文件中补充
<bean id="HelloWorld" class="com.springstudy.HelloWorld"></bean>
二、创建测试类进行测试
package com.springstudy.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.springstudy.HelloWorld;
public class SpringTest {
public static void main(String[] args) {
//载入配置文件
ClassPathXmlApplicationContext cac = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过配置文件利用反射创建bean id为HelloWorld的对象
HelloWorld hw = (HelloWorld)cac.getBean("HelloWorld");
//执行该对象的方法
hw.hello();
}
}
进行测试后:
其中常见的可配置项为
<bean scope="?">
这其中,scope的可配置项为
singleton
配置为该选项时,则默认该bean为单例模式,此时只会创建一个该对象,不存在并发,但是很节省内存, 应用于只需要创建一个对象
prototype
配置为该选项时,则默认该bean为多例模式,此时会创建多个对象,能够承受一定范围的并发,但是消耗内存
2.1DI(依赖)注入
常规注入是在创建好对象或者在创建对象的同时,通过set()方法,或者有参构造方法进行注入
即
对象.set属性();和new 构造(参数);
接下来我们新建一个类:
package com.springstudy;
public class Person {
private String name;
private Integer id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Override
public String toString() {
return "Person [name=" + name + ", id=" + id + "]";
}
}
spring set方法注入
接下来在配置文件中插入bean
<bean id="Person" class="com.springstudy.Person">
<property name="name" value="criss"></property>
<property name="id" value="2"></property>
</bean>
在测试类中创建该对象,并输出,结果是:
接下来通过spring注入,为对象属性赋值
在Person类中添加:
private HelloWorld helloWorld;
并重新生成set get 和toString 方法
回到applicanContext.xml文件。
在bean id为Person放中添加
<property name="helloWorld" ref="HelloWorld"></property>
回到刚才的测试方法,测试结果:
spring 的构造方法注入:
构造方法注入,当然需要在原来的类中添加构造方法拉,打开Person类,添加全参构造方法:
public Person(String name, Integer id, HelloWorld helloWorld) {
super();
this.name = name;
this.id = id;
this.helloWorld = helloWorld;
}
然后修改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-4.0.xsd">
<bean id="HelloWorld" class="com.springstudy.HelloWorld">
</bean>
<bean id="Person" class="com.springstudy.Person">
<!-- <property name="name" value="criss"></property> <property name="id"
value="2"></property> <property name="helloWorld" ref="HelloWorld"></property> -->
<constructor-arg name="name" value="criss"></constructor-arg>
<constructor-arg name="id" value="2"></constructor-arg>
<constructor-arg name="helloWorld" ref="HelloWorld"></constructor-arg>
</bean>
</beans>
回到测试方法进行测试:
三、总结
spring的初步学习还是很简单的,从配置文件到反射创建对象,东西很少。下一章将会记录springMVC的学习笔记。