Spring Framework5.0 学习(3)—— spring配置文件的三种形式

时间:2021-11-10 04:34:16

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 。

Spring Framework5.0 学习(3)—— spring配置文件的三种形式

这个容器的作用正如上图, 把定义在配置文件中的 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();
}
}

运行结果:

Spring Framework5.0 学习(3)—— spring配置文件的三种形式

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>

运行结果:

Spring Framework5.0 学习(3)—— spring配置文件的三种形式

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 学习(3)—— spring配置文件的三种形式的更多相关文章

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

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

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

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

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

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

  4. Spring Framework5&period;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 项目学习 &lpar;四&rpar; Spring Boot整合Swagger2自动生成API文档

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

  7. 第04项目:淘淘商城&lpar;SpringMvc&plus;Spring&plus;Mybatis&rpar; 的学习实践总结【第三天】

    淘淘商城(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. 关于C&num;操作防火墙,阻止程序联网

    //开启服务.开启防火墙 public void OpenFileWall() { // 1. 判断当前系统为XP或Win7 RegistryKey rk = Registry.LocalMachin ...

  2. VMware安装centos虚拟机

    vm中安装虚拟机,步骤,图片截取 VM中 --> 文件 --> 新建虚拟机 此处若是提示CPU不支持虚拟机安装 重启计算机,进入BIOS,修改cpu配置 点击next 下一步 .... 等 ...

  3. jQuery事件笔记

    bind(eventType[,data],hanlder):eventType表示要创建的处理器指定事件类型的名称.可以使用空格分隔的列表指定多个事件类型.data(对象)调用者提供的数据,用来附加 ...

  4. window平台下的MySQL快速安装。&lpar;不好意思,未完成待续,请飘过&rpar;

    MySQL安装方式 MSI安装(Windows Installer) ZIP安装 最好选择ZIP安装,比较干净,也快速搞好. 下载链接:http://pan.baidu.com/s/1sjFZZul ...

  5. HDU4738 Caocao&&num;39&semi;s Bridges 无向图的桥

    一眼题:找所有的桥,然后求最小权值 但是有很多坑点 1:如果本来不联通 输出0,(这个坑我知道) 2:但是还有一个坑,就是当整个连通,最小桥的权值是0时,也必须派一个人去,wa了无数遍(还是太年轻) ...

  6. CDH5 集群安装教程

    一.虚拟机的安装和网络配置. 1.虚拟机安装. 2.安装CentOS-6.5 64位版本. 桥接模式: Master: 内存:3G: 硬盘容量40G: 4核: Slave: 内存2G: 硬盘容量30G ...

  7. iOS发展- 文件共享&lpar;使用iTunes导入文件&comma; 并显示现有文件&rpar;

    到今天实现功能, 由iTunes导入文件的应用程序, 并在此文档进行编辑的应用. 就像我们平时经常使用 PDF阅读这样的事情, 们能够自己导入我们的电子书. 源代码下载:https://github. ...

  8. Eureka的基本功能和用法

    1.基础架构 eueka按逻辑上可以划分为3个模块,eureka-server,service-provider,service-consumereureka-server:服务端,提供服务注册和发现 ...

  9. 【转】FFmpeg获取DirectShow设备数据(摄像头,录屏)

    这两天研究了FFmpeg获取DirectShow设备数据的方法,在此简单记录一下以作备忘.本文所述的方法主要是对应Windows平台的. 1.       列设备 ffmpeg -list_devic ...

  10. Collections&period;sort 给集合排序

    List<MenuVo> child_menus = new ArrayList<MenuVo>(); for (MenuVo menuVo : child_menus) { ...