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();
    }

}

 运行结果:

转载于:https://www.cnblogs.com/zheting/p/7712269.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值