Spring初步学习(一)
2017-10-11 本文已影响13人
大炮对着虫子
spring的特点为:轻量级
,无侵入式设计
功能模块:
test:提供单元测试的支持
core container:IOC,DI
aop:面向切面编码,
data access:操作数据库,事务控制
web:集成SpringMVC struts2
第一个简单的小例子
1、导jar(test core container)
我们可以通过Maven去加载对应需要的jar包
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xinge</groupId>
<artifactId>spring-01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<!-- 配置中央仓库 -->
<repositories>
<repository>
<id>mvnrepository</id>
<url>http://mvnrepository.com/</url>
</repository>
</repositories>
<!-- 配置属性值 -->
<properties>
<jdk.version>1.7</jdk.version>
<charset>utf-8</charset>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.1.0.RELEASE</spring.version>
</properties>
<!-- 配置依赖信息 -->
<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- mysql数据库驱动jar -->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<!-- 配置spring的jar的依赖信息 -->
<!-- spring测试模块的jar -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
<!-- <exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
</exclusions> -->
</dependency>
<!-- spring的核心容器模块的jar;beans core context expressionlanguage -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<!-- 配置插件信息 -->
<build>
<plugins>
<!-- 工程编译插件 ;指定使用的jdk的版本-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
<encoding>${charset}</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、创建一个普通的java类
即写一个bean对象
public class Dog {
private String dogName;
public String getDogName() {
return dogName;
}
public void setDogName(String dogName) {
this.dogName = dogName;
}
public void shout(){
System.out.println("汪汪!");
}
public Dog(){
System.out.println("dog对象初始化成功!");
}
}
public class Boy {
private Dog dog;
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "Boy [dog=" + dog + "]";
}
}
3、在spring配置文件中注册java类
<?xml version="1.0" encoding="UTF-8"?>
<!-- 命名空间、指定标签满足w3c的规范、指定约束文件的路径(命名空间+约束文件的路径) -->
<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">
<!-- 将java类注册到spring容器中,由spring容器管理该对象 -->
<!-- class指定java类的全路径;id给该对象命名,唯一标识符 -->
<bean id="dog" class="com.icss.model.Dog">
<property name="dogName" value="小汪"></property>
</bean>
<bean id="boy" class="com.icss.model.Boy">
<property name="dog" ref="dog"></property>
</bean>
</beans>
4、写测试类,测试spring容器管理的对象是否实例化成功
public class DogTest {
@Test
public void testD(){
// 获取spring容器对象
ApplicationContext ac =
new ClassPathXmlApplicationContext("applicationContext.xml");
// 获取容器中管理的对象,getBean方法中的参数即为bean的id属性值
Dog dog = (Dog) ac.getBean("dog");
// 测试容器实例化的对象是否成功
dog.shout();
System.out.println(dog.getDogName());
}
}
package com.icss.model;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BoyTest {
@Test
public void testD(){
// 获取spring容器对象
ApplicationContext ac =
new ClassPathXmlApplicationContext("applicationContext.xml");
// 获取容器中管理的对象,getBean方法中的参数即为bean的id属性值
Boy boy = (Boy) ac.getBean("boy");
// 测试容器实例化的对象是否成功
System.out.println(boy.getDog().getDogName());
}
}
值得注意的是,当第一次加载spring容器对象时,spring容器对象会全部实例化里面的所有bean,即所有bean的构造函数都执行了。
构造器方式,普通工厂的方式,静态工厂的方式进行实例化
先建立一个person类的bean
public class Person {
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public void eat()
{
System.out.println("人类必须要吃食物,否则挂掉");
}
}
1、构造器方式
容器配置文件:
<bean id="person" class="com.icss.model.Person">
</bean>
测试方法:
@Test
public void testD(){
// 获取spring容器对象
ApplicationContext ac =
new ClassPathXmlApplicationContext("applicationContext.xml");
// 获取容器中管理的对象,getBean方法中的参数即为bean的id属性值
Person person = (Person) ac.getBean("person");
// 测试容器实例化的对象是否成功
person.eat();
//System.out.println(boy.getDog().getDogName());
}
2、静态工厂方式
public class PersonFactory {
public static Person getPerson()
{
return new Person();
}
}
<bean id="static_person" class="com.icss.model.PersonFactory" factory-method="getPerson">
</bean>
3、普通工厂方式
public class PersonFactory2 {
public Person getPerson()
{
return new Person();
}
}
<bean id="nostatic_person" class="com.icss.model.PersonFactory2">
</bean>
<bean id="person2" factory-bean="nostatic_person" factory-method="getPerson">
</bean>