Spring Framework5.0 学习(3)—— spring配置文件的三种形式
Spring Framework 是 IOC (Inversion of Control 控制反转)原则的实践。 IoC is also known as dependency injection (DI 依赖注入)。
org.springframework.beans 和 org.springframework.context 两个包实现了Ioc 容器。 BeanFactory接口的子接口 ApplicationContext 定义了容器的基本功能。如果是web app 使用的是 WebApplicationContext 。

这个容器的作用正如上图, 把定义在配置文件中的 POJO的关系解析并实例化,应用业务系统。
POJO: Plain Old Java Object 。
spring配置文件有以下三种形式:
- 传统的XML
- Spring 2.5 以后新增注解配置
- Spring 3.0 以后新增 JavaConfig
1.0 传统xml配置:
build.gradle
apply plugin: 'java'
apply plugin: 'idea' // mainClassName 是 application的一个属性,否则会报错
apply plugin: 'application'
mainClassName = 'xmlConfig.HelloWorld' sourceCompatibility = 1.8
targetCompatibility = 1.8 repositories {
mavenCentral()
} dependencies {
compile "joda-time:joda-time:2.2"
compile 'org.springframework:spring-context:5.0.0.RELEASE'
} // 该项目生成的jar包的名字和版本,如 gs-gradle-0.1.0.jar
jar {
baseName = 'gs-gradle'
version = '0.1.0'
}
在src/main/resources目录下创建 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.xsd"> <bean id="helloWorld" name="helloWorld2" class="xmlConfig.HelloWorld"/> </beans>
HelloWorld.java
package xmlConfig; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.GenericApplicationContext; /**
* Created by sheting on 10/22/2017
*/
public class HelloWorld {
public void sayHello() {
System.out.println("Hello World");
} public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("helloWorld.xml");
HelloWorld helloWorld2 = context.getBean("helloWorld2", HelloWorld.class);
helloWorld2.sayHello(); HelloWorld helloWorld = context.getBean("helloWorld", HelloWorld.class);
helloWorld.sayHello(); GenericApplicationContext context2 = new GenericApplicationContext();
new XmlBeanDefinitionReader(context2).loadBeanDefinitions("helloWorld.xml");
context2.refresh();
HelloWorld context2Bean = context2.getBean("helloWorld", HelloWorld.class);
context2Bean.sayHello();
}
}
运行结果:

2.0 注解配置
gradle.build
apply plugin: 'java'
apply plugin: 'idea' // mainClassName 是 application的一个属性,否则会报错
apply plugin: 'application'
mainClassName = 'annotationConfig.Test' sourceCompatibility = 1.8
targetCompatibility = 1.8 repositories {
mavenCentral()
} dependencies {
compile "joda-time:joda-time:2.2"
compile 'org.springframework:spring-context:5.0.0.RELEASE'
} // 该项目生成的jar包的名字和版本,如 gs-gradle-0.1.0.jar
jar {
baseName = 'gs-gradle'
version = '0.1.0'
}
HelloWorld.java
package annotationConfig; import org.springframework.stereotype.Component; /**
* Created by sheting on 10/22/2017
*/ /**
* 如果属性名称是value,value可以省略。
* 如果不指定value,默认值是类名首先字母变为小写。
* @Component(value="beanId") 就是把当前类实例化。相当于<bean id="beanId">
*/
@Component
public class HelloWorld { public void sayHello() {
System.out.println("Hello World");
}
}
Test.java
package annotationConfig; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Created by sheting on 10/22/2017
*/ public class Test { public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("helloWorld.xml");
HelloWorld helloWorld = context.getBean("helloWorld", HelloWorld.class);
helloWorld.sayHello();
} }
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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 开启注解扫描 -->
<context:component-scan base-package="annotationConfig"/> </beans>
运行结果:

3.0 java config
gradle.build
apply plugin: 'java'
apply plugin: 'idea' // mainClassName 是 application的一个属性,否则会报错
apply plugin: 'application'
mainClassName = 'javaConfig.Test'
sourceCompatibility = 1.8
targetCompatibility = 1.8 repositories {
mavenCentral()
} dependencies {
compile "joda-time:joda-time:2.2"
compile 'org.springframework:spring-context:5.0.0.RELEASE'
} // 该项目生成的jar包的名字和版本,如 gs-gradle-0.1.0.jar
jar {
baseName = 'gs-gradle'
version = '0.1.0'
}
MyService.java
package javaConfig; /**
* Created by sheting on 10/22/2017
*/
public interface MyService {
void sayHello();
}
MyServiceImpl.java
package javaConfig; /**
* Created by sheting on 10/22/2017
*/
public class MyServiceImpl implements MyService {
public void sayHello() {
System.out.println("Hello World");
}
}
AppConfig.java
package javaConfig; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* Created by sheting on 10/22/2017
*
* 这个类和下面的配置类似
* <beans>
* <bean id="myService" class="com.acme.services.MyServiceImpl"/>
* </beans>
*
* Java配置是通过@Configuration和@Bean来实现的。
* @Configuartion 声明当前类是一个配置类,相当于一个spring配置的xml文件的<beans></beans>。
* @Bean 注解在方法上,声明当前方法的返回值为一个bean,bean的名称为方法名。
* @ComponentScan 自动扫描包名下所有的 @Service @Component @Repository @Controller的类,并注册为bean。
*
*/
@Configuration
public class AppConfig { @Bean
public MyService myService() {
return new MyServiceImpl();
}
}
Test.java
package javaConfig; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; /**
* Created by sheting on 10/22/2017
*/ public class Test { public static void main(String[] args) {
//加载配置
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
MyService myService = ctx.getBean(MyService.class);
myService.sayHello();
} }
运行结果:

Spring Framework5.0 学习(3)—— spring配置文件的三种形式的更多相关文章
- Spring Framework5.0 学习(4)—— Bean的命名id和name区别
Spring中Bean的命名 1.每个Bean可以有一个id属性,并可以根据该id在IoC容器中查找该Bean,该id属性值必须在IoC容器中唯一: 2.可以不指定id属性,只指定全限定类名,如: & ...
- Spring Framework5.0 学习(4)—— 基本概念
1.0 控制反转(IOC)/依赖注入(DI) 通过依赖注入(DI),对象的依赖关系将由负责协调系统关系中各个对象的第三方组件在创建对象是设定.对象无需自行创建或管理它们的依赖关系——依赖关系将被自动 ...
- Spring Framework5.0 学习(2)-- Quick Start
官网:https://projects.spring.io/spring-framework/ Spring Framework 5.x 要求 JDK 1.8以上 1.0 在(1)的基础上,给bu ...
- Spring Framework5.0 学习(1)—— 用Gradle构建一个Java Project
1.0 安装Gradle,参考之前文章<Gradle入门实战(Windows版)> 2.0 使用gradle 快速生成一个Java project gradle init --type ...
- java 学习笔记 读取配置文件的三种方式
package com.itheima.servlet.cfg; import java.io.FileInputStream; import java.io.FileNotFoundExceptio ...
- Spring Boot 项目学习 (四) Spring Boot整合Swagger2自动生成API文档
0 引言 在做服务端开发的时候,难免会涉及到API 接口文档的编写,可以经历过手写API 文档的过程,就会发现,一个自动生成API文档可以提高多少的效率. 以下列举几个手写API 文档的痛点: 文档需 ...
- 第04项目:淘淘商城(SpringMvc+Spring+Mybatis) 的学习实践总结【第三天】
淘淘商城(SpringMVC+Spring+Mybatis) 是传智播客在2015年9月份录制的,几年过去了.由于视频里课上老师敲的代码和项目笔记有些细节上存在出入,只有根据日志报错信息作出适当的调 ...
- spring Bean配置的三种形式
Spring Bean配置有以下三种形式: 传统的xml配置 Spring 2.5 以后新增注解配置 Spring3.0以后新增JavaConfig 1. 传统的xml配置 <?xml vers ...
- spring对事务支持的三种形式
spring对事务支持的三种形式: 1.通过spring配置文件进行切面配置 <bean id="***Manager" class="org.springfram ...
随机推荐
- Windows加密API的功能分类
本地数据加密保护本地数据加密保护机制提供了简单的DAPI调用接口,密钥管理等等一概由系统来处理.DAPI的数据加密保护机制在用户登录会话范围或者本地计算范围,使用操作系统设计的方式加密保护数据和解密还 ...
- [Git & GitHub] 利用Git Bash进行第一次提交文件
转载:https://blog.csdn.net/dietime1943/article/details/72420042 利用Git Bash进行第一次提交文件 快下班的时候,MD群里有人问怎么向g ...
- FZU 2139——久违的月赛之二——————【贪心】
久违的月赛之二 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Stat ...
- java 如何下载jar包
随着maven工具的使用,我们已经不再需要辛苦的找jar包,也不需要再买会员去下载jar包,但是还有一些同学,不知道怎么下载jar包,下面我给大家介绍一下,如何潇洒的找到自己想要的jar包. 首先,访 ...
- spring整合struts2和hibernate
1.spring 1.1 jar包 1.2 spring.xml <?xml version="1.0" encoding="UTF-8"?> &l ...
- python4
列表的常用操作 创建列表 1.创建空列表 列表变量 = [] 2.创建单个数据的列表 列表变量 = [值] ...
- 读ios开发有感——建立APP开发体系
前言:ios开发和现在的大前端.跨端开发在底层上的道理是相通的,因此通过学习ios开发,可以形成对APP开发体系的理解. 一.app开发的知识体系 基础 应用开发 原理 原生与前端 二.基础模块 按照 ...
- JS之原型式的继承
创建一个Person的构造器 function Person(first, last, age, gender, interests) { this.name = { first, last }; t ...
- JavaScript 事件委托
JavaScript事件委托,或者叫事件代理,是利用事件冒泡,只指定一个事件处理程序,就可以管理某一类型的所有事件. 借花献佛的例子(取快递): 有三个同事预计会在周一收到快递.为签收快递,有两种办法 ...
- Linux下mongodb
Linux下mongodb安装: 新建mongodb文件夹 下载安装包 curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3. ...