Xml 和 JavaConfig

  Spring 使用 Xml 作为容器配置文件,在 3.0 以后加入了 JavaConfig. 使用 java 类做配置文件使用。

JavaConfig: 是 Spring 提供的使用 java 类配置容器。配置 Spring IOC 容器的纯 Java方法。
优点:
1.可以使用面像对象的方式, 一个配置类可以继承配置类,可以重写方法
2.避免繁琐的 xml 配置

Xml 配置容器

创建项目

SpringBoot001

pom.xml

 <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.19</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.26</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- 编译插件 -->
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <!-- 插件的版本 -->
                <version>3.5.1</version>
                <!-- 编译级别 -->
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <!-- 编码格式 -->
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

创建数据类 Student

@Data
public class Student {

    private String name;

    private Integer age;

    private String sex;

}

resources 目录下创建 Spring 的配置文件 applicationContext.xml

  <!--声明bean对象-->
    <bean id="myStudent" class="com.kuangziyao.vo.Student">
        <property name="name" value="碍事梨"/>
        <property name="age" value="21"/>
        <property name="sex" value="女"/>
    </bean>

JavaConfig

JavaConfig: 使用java类作为xml配置文件的替代, 是配置spring容器的纯java的方式。 在这个java类这可以创建java对象,把对象放入spring容器中(注入到容器),

使用到了两个注解

  1. @Configuration : 放在一个类的上面,表示这个类是作为配置文件使用的。
  2. @Bean:声明对象,把对象注入到容器中。

例子:

package com.kuangziyao.config;

import com.kuangziyao.vo.Student;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * Configuration:表示当前类是作为配置文件使用的。 就是用来配置容器的
 *       位置:在类的上面
 *
 *  SpringConfig这个类就相当于beans.xml
 */
@Configuration
public class SpringConfig {

    /**
     * 创建方法,方法的返回值是对象。 在方法的上面加入@Bean
     * 方法的返回值对象就注入到容器中。
     *
     * @Bean: 把对象注入到spring容器中。 作用相当于<bean>
     *
     *     位置:方法的上面
     *
     *     说明:@Bean,不指定对象的名称,默认是方法名是 id
     *
     */
    @Bean
    public Student createStudent() {
        Student s1  = new Student();
        s1.setName("张三");
        s1.setAge(26);
        s1.setSex("男");
        return s1;
    }

    /***
     * 指定对象在容器中的名称(指定<bean>的id属性)
     * @Bean的name属性,指定对象的名称(id)
     */
    @Bean(name = "lisiStudent")
    public Student makeStudent(){
        Student s2  = new Student();
        s2.setName("李四");
        s2.setAge(22);
        s2.setSex("男");
        return s2;
    }
}

测试方法:

package com.kuangziyao;

import com.kuangziyao.config.SpringConfig;
import com.kuangziyao.vo.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {

    /**
     * 使用xml作为容器配置文件
     */
    @Test
    public void test01() {
        String config = "beans.xml";
        ApplicationContext context = new ClassPathXmlApplicationContext(config);
        Student student = (Student) context.getBean("myStudent");
        System.out.println("student=" + student);
    }


    /**
     * 使用JavaConfig
     */
    @Test
    public void test02() {
        // 没有 xml 配置文件,使用 java 类代替 xml 配置文件 的作用
        ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
        Student student = (Student) ctx.getBean("createStudent");
        System.out.println("使用JavaConfig创建的bean对象" + student);
    }


    /**
     * 指定Bean对象的名字
     */
    @Test
    public void test03() {
        // 没有 xml 配置文件,使用 java 类代替 xml 配置文件 的作用
        ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
        Student student = (Student) ctx.getBean("lisiStudent");
        System.out.println("使用JavaConfig创建的bean对象" + student);
    }
}

@ImporResource

@ImportResource 作用导入其他的xml配置文件, 等于 在xml

<import resources="其他配置文件"/>
eg:

@Configuration
@ImportResource(value ={ "classpath:applicationContext.xml","classpath:beans.xml"})
public class SpringConfig {
}

@PropertyResource

@PropertyResource: 读取properties属性配置文件。 使用属性配置文件可以实现外部化配置 ,在程序代码之外提供数据。

步骤:

1. 在resources目录下,创建properties文件, 使用k=v的格式提供数据
2. 在PropertyResource 指定properties文件的位置
3. 使用@Value(value="${key}")
@Configuration
@ImportResource(value ={ "classpath:applicationContext.xml","classpath:beans.xml"})
@PropertySource(value = "classpath:config.properties")
@ComponentScan(basePackages = "com.bjpowernode.vo")
public class SpringConfig {
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注