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 ...
随机推荐
- 一个Java小菜鸟的实习之路
博主今年大四,六月份毕业,之前一直对编程感兴趣,于是在大学里自学了Java,(本专业是通信工程).在今年过年的时候,父母让来南方过年,于是博主自己也想着能不能在南方找份java的实习先干着,了解一下行 ...
- Delphi下OpenGL2d绘图(01)-初始化
一.前言: Delphi默认支持OpenGl,可以uses OpenGL单元进行引用,便可以使用OpenGL的函数.OpenGl是跨平台的,而且Windows很早就支持并集成在系统中,存在于syste ...
- git 检查是否有commit到本地但还没push的代码
使用 git status 命令可以得到以下结果 $ git status On branch dev_getTicketCnt Your branch is ahead of 'origin/mas ...
- WEB下渗透测试经验技巧(全)[转载]
Nuclear’Atk 整理的: 上传漏洞拿shell: 1.直接上传asp.asa.jsp.cer.php.aspx.htr.cdx….之类的马,拿到shell.2.就是在上传时在后缀后面加空格或者 ...
- 将forme表单转换为Json对象
//将Form 表单转换为Json字符串 $.fn.serializeObject = function () { var o = {}; var a = this.serializeArray(); ...
- JavaScript中Undefined 和 Null的区别
Undefined 这个值表示变量不含有值. 可以通过将变量的值设置为 null 来清空变量. 例如: <script> var person; var car="Volvo&q ...
- 手机UA识别
整理手机UA识别如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...
- [android] 练习使用ListView(二)
主要练习异步任务和LruCache缓存 package com.android.test; import java.io.InputStream; import java.net.HttpURLCon ...
- SQL语句整理(一) 数据库查询语言DQL
前言: 这是我学数据库时整理的学习资料,基本上包括了所以的SQL语句的知识点. 我的教材是人大王珊老师的<数据库系统概论>. 因为是手打的,所以会用一些细节打错了,但都挺明显也不多(考完试 ...
- select获取选中项的值与文本
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...