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配置文件的三种形式的更多相关文章

  1. Spring Framework5.0 学习(4)—— Bean的命名id和name区别

    Spring中Bean的命名 1.每个Bean可以有一个id属性,并可以根据该id在IoC容器中查找该Bean,该id属性值必须在IoC容器中唯一: 2.可以不指定id属性,只指定全限定类名,如: & ...

  2. Spring Framework5.0 学习(4)—— 基本概念

    1.0  控制反转(IOC)/依赖注入(DI) 通过依赖注入(DI),对象的依赖关系将由负责协调系统关系中各个对象的第三方组件在创建对象是设定.对象无需自行创建或管理它们的依赖关系——依赖关系将被自动 ...

  3. Spring Framework5.0 学习(2)-- Quick Start

    官网:https://projects.spring.io/spring-framework/ Spring Framework 5.x 要求 JDK 1.8以上 1.0   在(1)的基础上,给bu ...

  4. Spring Framework5.0 学习(1)—— 用Gradle构建一个Java Project

    1.0  安装Gradle,参考之前文章<Gradle入门实战(Windows版)> 2.0  使用gradle 快速生成一个Java project gradle init --type ...

  5. java 学习笔记 读取配置文件的三种方式

    package com.itheima.servlet.cfg; import java.io.FileInputStream; import java.io.FileNotFoundExceptio ...

  6. Spring Boot 项目学习 (四) Spring Boot整合Swagger2自动生成API文档

    0 引言 在做服务端开发的时候,难免会涉及到API 接口文档的编写,可以经历过手写API 文档的过程,就会发现,一个自动生成API文档可以提高多少的效率. 以下列举几个手写API 文档的痛点: 文档需 ...

  7. 第04项目:淘淘商城(SpringMvc+Spring+Mybatis) 的学习实践总结【第三天】

    淘淘商城(SpringMVC+Spring+Mybatis)  是传智播客在2015年9月份录制的,几年过去了.由于视频里课上老师敲的代码和项目笔记有些细节上存在出入,只有根据日志报错信息作出适当的调 ...

  8. spring Bean配置的三种形式

    Spring Bean配置有以下三种形式: 传统的xml配置 Spring 2.5 以后新增注解配置 Spring3.0以后新增JavaConfig 1. 传统的xml配置 <?xml vers ...

  9. spring对事务支持的三种形式

    spring对事务支持的三种形式: 1.通过spring配置文件进行切面配置 <bean id="***Manager" class="org.springfram ...

随机推荐

  1. 关于Map集合的遍历总结

    Map集合的遍历经常遇到,今天在这里总结一下Map集合遍历的几种方式: public static void main(String[] args){ Map<String,String> ...

  2. Asp.Net MVC4通过id更新表单

    用户需求是:一个表单一旦创建完,其中大部分的字段便不可再编辑.只能编辑其中部分字段. 而不可编辑是通过对input输入框设置disabled属性实现的,那么这时候直接向数据库中submit表单中的内容 ...

  3. [javaEE] 三层架构案例-用户模块(二)

    使用junit测试框架,测试查找用户和添加用户功能 com.tsh.test.xmlUserDaoTest package com.tsh.test; import org.junit.Test; i ...

  4. HTML DOM status 属性

    <!DOCTYPE html><html> <head>HTML DOM status 属性</head><body><script ...

  5. 互联网轻量级框架SSM-查缺补漏第四天

    简言:昨天第四章没看完,今天接着记吧. 4.5 typeHandler 类型转换器 顾名思义呀,就是将数据库中数据类型与Java数据类型做相互转换的处理器.在typeHandler中,分为jdbcTy ...

  6. Effective C++ .13使用智能指针来引用资源

    #include <iostream> #include <cstdlib> #include <memory> using namespace std; clas ...

  7. JavaEE之HttpServletRequest

    HttpServletRequest //要下载的这个文件的类型--客户端会通过文件的MIME类型去区分类型 response.setContentType( getServletContext(). ...

  8. js-原始类型和声明变量

    ** Java的基本数据类型:byte.short.int.long.float.double.char.boolean ** 定义变量 都是用关键字 var(ES6中可以使用const和let来定义 ...

  9. Bzoj1492: [NOI2007]货币兑换Cash(不单调的斜率优化)

    题面 传送门 Sol 题目都说了 必然存在一种最优的买卖方案满足: 每次买进操作使用完所有的人民币: 每次卖出操作卖出所有的金券. 设\(f[i]\)表示第\(i\)天可以有的最大钱数 枚举\(j&l ...

  10. window下Jekyll建站过程

    > 前言 最近决定要写一个博客,先后注册了博客园和CSND的博客,但是他们的界面主题都不是很符合自己的要求,还没有足够个性化的发挥空间,遂决定自己建一个博客. 网上找了一下教程,感觉都不太详细, ...