基础理论
实战之前,我们先大概了解一下Spring是什么,我摘录了尚硅谷中的一些讲解
•Spring 是一个开源框架.
•Spring 为简化企业级应用开发而生. 使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能.
•Spring 是一个 IOC(DI) 和 AOP 容器框架.IOC(反转控制)DI(依赖注入)
•具体描述 Spring:
–轻量级:Spring 是非侵入性的 - 基于 Spring 开发的应用中的对象可以不依赖于 Spring 的 API
–依赖注入(DI --- dependency injection、IOC)
–面向切面编程(AOP --- aspect oriented programming)
–容器: Spring 是一个容器, 因为它包含并且管理应用对象的生命周期
–框架: Spring 实现了使用简单的组件配置组合成一个复杂的应用. 在 Spring 中可以使用 XML 和 Java 注解组合这些对象
–一站式:在 IOC 和 AOP 的基础上可以整合各种企业应用的开源框架和优秀的第三方类库 (实际上 Spring 自身也提供了展现层的 SpringMVC 和 持久层的 Spring JDBC)
依赖注入和面向切面编程是Spring的核心
Spring模块
以上就是Spring的大概知识,不理解也没关系,再通过项目实战中会慢慢理解
由于大部分网上Spring开发工具都是使用eclipse,个人比较喜欢IDEA,也深深被IDEA的强大所吸引,所以尝试使用IEDA去开发Spring,整个过程都是个人进行摸索完成的,也建议大家去尝试使用IDEA,对于IDEA的安装和破解可以参考网上教程。
实战
创建工程
操作流程:
1、Create New Project
2、选择Spring,如图
选择Download,IDEA将自动下载Spring所依赖的jar包
3、工程名和工程目录
4、在src下创建一个包,如该工程包为com.spring,在该包下新建一个HelloWorld类
package com.spring;
public class HelloWorld {
private String userName;
public void setUserName(String userName) {
this.userName = userName;
}
public void hello(){
System.out.println("Hello: " + userName);
}
}
5、创建一个Main类,我先用传统的java方式(不用Spring框架)编写,以便和使用框架之后的代码比较
package com.spring;
public class Main {
public static void main(String[] args) {
//创建HelloWorld的一个对象
HelloWorld helloWorld = new HelloWorld();
//为name属性赋值
helloWorld.setUserName("Tom");
//调用hello方法
helloWorld.hello();
}
}
运行之后控制台输出
现在我利用Spring框架重写这段代码
我们可以将创建对象和为属性赋值交给Spring完成
6、在src下创建Spring 配置文件applicationContext.xml,如图所示
创建完成之后,我们可以看到它提示
我们现在只是完成了配置文件的创建,并没有对其进行配置,这时我们可以利用IDEA的强大,让其自动为我们配置
点击上述图片齿轮,选择Fix.....如图
并选择create new application context,这时配置文件才真正创建成功
编写配置文件
<?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-->
<bean id="helloWorld" class="com.spring.HelloWorld">
<property name="userName" value="Spring"></property>
</bean>
</beans>
我们可以从配置文件中看出,配置文件与我们之前的HelloWorld类属于映射关系,id=“helloWorld”唯一标识HelloWorld类,property为类中属性赋值
7、根据我们写好的配置文件,我们现在重写Main类
package com.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
/*//创建HelloWorld的一个对象
HelloWorld helloWorld = new HelloWorld();
//为name属性赋值
helloWorld.setUserName("Tom");
*/
//1、创建Spring的IOC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//2、从IOC容器中获取Bean
HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
//3、调用hello方法
helloWorld.hello();
}
}
我们现在在看看运行结果
运行结果会和原来稍微不同打印出了日志
我们现在把上述代码中的2、从IOC容器中获取bean和3、调用hello方法 注释掉,我们看看Spring会帮我们干什么
为了可以看的详细,我们在HelloWorld类中添加一段代码
package com.spring;
public class HelloWorld {
private String userName;
public void setUserName(String userName) {
System.out.println("SetName: " + userName);
this.userName = userName;
}
public void hello(){
System.out.println("Hello: " + userName);
}
public HelloWorld(){
System.out.println("HelloWorld's Constructor...");
}
}
Main类中,我们只创建了容器
package com.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
/*//创建HelloWorld的一个对象
HelloWorld helloWorld = new HelloWorld();
//为name属性赋值
helloWorld.setUserName("Tom");
*/
//1、创建Spring的IOC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//2、从IOC容器中获取Bean
//HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
//3、调用hello方法
//helloWorld.hello();
}
}
看一下运行结果
Main类中,只有创建容器代码,在创建容器的时候,会调用构造器HelloWorld()方法,对配置文件中的Bean进行初始化,同时调用set方法进行赋值,赋值是通过配置文件进行赋值
以上就是一个使用Spring框架的HelloWorld简单实例