一、什么是Spring?
二、Spring框架组件都有哪些内容?
三、Spring HelloWorld。
1、创建一个maven工程,选择new-》project-》maven-》maven-archetype-quickstart;
2、输入GroupId:如com.monkey.yb ,ArtifactId:如spring,一直next到finish(注意设定maven配置文件和本地仓库地址);
3、按通常写法,先写一普通的HelloWorld,再在此基础上进行Spring扩展,先在src下新建两个类 HelloWorld.java和Main.java,具体内容如下:
package com.monkey.yb;
public class HelloWorld {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void say(){
System.out.println("say: HelloWorld " + name);
}
}
package com.monkey.yb;
public class Main
{
public static void main( String[] args )
{
HelloWorld hw = new HelloWorld();
hw.setName("tester");
hw.say();
}
}
运行main方法,此时输出的内容为:say: HelloWorld tester
4、那如何改成Spring工程呢?第一步我们需要在maven pom.xml文件中引入spring相关依赖,如下:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<spring.version>4.3.25.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.25.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</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-expression</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
接着第二步在src文件夹下新增applicationContext配置文件,配置Spring bean相关内容,此处我们可以将HelloWorld当作spring中的一个Bean,具体内容如下(id为唯一标志,class为类全名,property 为类属性和属性对应的值):
<?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.xsd">
<bean id="helloWorld" class="com.monkey.yb.HelloWorld">
<property name="name" value="yibao"></property>
</bean>
</beans>
第三步:则是修改Main类,引入applicationContext配置文件,如下:
package com.monkey.yb;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Hello world!
*
*/
public class Main
{
public static void main( String[] args )
{
/**
引入配置文件后不需要再次创建实例对象,可直接通过getBean方式直接获取到对象
*/
// HelloWorld hw = new HelloWorld();
// hw.setName("tester");
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld hw = (HelloWorld)context.getBean("helloWorld");
hw.say();
}
}
再次运行main方法,发现输出的内容依然为:say: HelloWorld tester 那说明你的第一个HelloWorld是成功了的。
那什么我们使用ClassPathXmlApplicationContext加载applicationConext.xml后不需要再次创建实例对象呢?我们可以在HelloWorld中生成一个打印一条内容的构造器,如下:
public HelloWorld() {
System.out.println("this is a constructor");
}
再到Main类中把getBean和调用say()方法注释掉,重新运行Main类,如下:
public static void main( String[] args )
{
// HelloWorld hw = new HelloWorld();
// hw.setName("tester");
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// HelloWorld hw = (HelloWorld)context.getBean("helloWorld");
// hw.say();
}
那此时输出的内容则为:this is a constructor