1. SpringBoot快速回顾(分别使用注解和xml方式去声明Bean,获取Bean)

创建一个Maven项目,配置依赖环境,再编程测试

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zjh</groupId>
    <artifactId>001-pre</artifactId>
    <version>1.0.0</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

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

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!-- 控制配置maven构建项目的参数设置,设置jdk版本 -->
    <build>
        <!-- 配置插件 -->
        <plugins>
            <!-- 配置具体插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <!-- 插件的名称 -->
                <artifactId>maven-compiler-plugin</artifactId>
                <!-- 插件的版本 -->
                <version>3.8.1</version>
                <!-- 配置插件的信息 -->
                <configuration>
                    <!-- 告诉maven,项目是在jdk1.8上编译的 -->
                    <source>1.8</source>
                    <!-- 程序应该运行在jdk1.8上 -->
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

1. 使用xml声明Bean

1.1 创建测试实体类

在这里插入图片描述

package com.zjh.vo;

public class Cat {

    private String Name;
    private String Sex;

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String getSex() {
        return Sex;
    }

    public void setSex(String sex) {
        Sex = sex;
    }

    @Override
    public String toString() {
        return "Cat{" +
                "Name='" + Name + '\'' +
                ", Sex='" + Sex + '\'' +
                '}';
    }
}

package com.zjh.vo;

public class Student {

    private String Name;
    private String Sex;

    public void setName(String name) {
        Name = name;
    }

    public void setSex(String sex) {
        Sex = sex;
    }

    public String getName() {
        return Name;
    }

    public String getSex() {
        return Sex;
    }

    @Override
    public String toString() {
        return "Student{" +
                "Name='" + Name + '\'' +
                ", Sex='" + Sex + '\'' +
                '}';
    }
}
package com.zjh.vo;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

//利用注解生成Dog类的对象dog
@Component("dog")
public class Dog {

    @Value("dog.name")
    private String name;

    @Value("dog.sex")
    private String sex;

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }
}

1.2 创建xml文件(目的:将实体类声明成Bean)

目录框架
在这里插入图片描述

文件名:beans.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-->
    <bean id="myStudent" class="com.zjh.vo.Student">
        <property name="name" value="Ove"/>
        <property name="sex" value=""/>
    </bean>
</beans>

文件名:ApplicationContext.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="myCat" class="com.zjh.vo.Cat">
        <property name="name" value="狸猫"/>
        <property name="sex" value=""/>
    </bean>
</beans>

1.3 测试

目录结构
在这里插入图片描述

public class MyTest {

    //XML测试案例
    @Test
    public void test1(){
        String config = "beans.xml";
        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
        Object stu = ctx.getBean("myStudent");
        System.out.println(stu);
    }
}

控制台测试结果:
在这里插入图片描述

总结:先创建实体类student----->再创建beans.xml (将实体类声明为Bean)------>获取beans.xml文件-------->取出声明的Bean


注意:打印的数据 是在 beans.xml 中定义的

2. 使用注解@configuration声明Bean

@configuration注解可以代替xml文件,但是对于如何将一个实体类声明为Bean,这里使用的仍然是@Bean注解。原本是在 .xml 文件中使用@Bean。现在是在 .java 文件中使用@Bean。

2.1 已经声明过实体类Student

2.2 定义配置类

目录框架
在这里插入图片描述


//@Configuration表明这个类是一个配置类
@Configuration
public class springConfig {

    /*
        创建一个方法,方法的返回值是该类的对象,使用注解Bean注入到容器里
        可以修改bean名称:@Bean(name = "DIY")\也可不修改,默认为方法名
    */
     @Bean(name = "DIY_Student")
    public Student createStudent(){
        Student s1 = new Student();
        s1.setName("佳");
        s1.setSex("男");
        return s1;
    }

    @Bean(name = "DIY_Cat")
    public Cat creatCat(){
        Cat c1 = new Cat();
        c1.setName("木偶猫");
        c1.setSex("公");
        return c1;
    }
}

2.3 测试

public class MyTest {

    //config测试案例
    @Test
    public void test2(){

        ApplicationContext ctx = new AnnotationConfigApplicationContext(springConfig.class);
        Object stu = ctx.getBean("DIY_Student");
        Object cat = ctx.getBean("DIY_Cat");
        System.out.println(stu);
        System.out.println(cat);
    }
 }

控制台输入结果
在这里插入图片描述

总结:先创建实体类student----->创建配置类,将Bean注入到容器里------------>最后获取配置类------->取出声明的Bean


注意:最大的区别 就是不需要创建xml文件,直接使用写类和方法的方式代替

3 使用@Component声明Bean

第二章说到是在.java(使用@configuration注解声明的 .java 类型的配置文件)文件中使用@Bean的方式。
实际上我们一般不这样声明一个bean
最常用的还是使用@Component注解直接写在实体类上面(参考Dog类),这样就不需要在配置类中写@Bean声明了

3.1已经声明过的Dog实体类

package com.zjh.vo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

//利用注解生成Dog类的对象dog
@Component("dog")
public class Dog {
    private String name;
    private String sex;

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }
}

3.2 定义配置类

//@Configuration表明这个类是一个配置类
@Configuration
//使用组建扫描器扫描这个路径下的包,看哪里使用了注解component---原来是Dog类中使用了注解,生成了对象dog
@ComponentScan(basePackages = "com.zjh.vo")
public class DogConfig {
    public void DogConfig(){
       System.out.println("这里不用声明bean");
  }

}

3.3 测试

public class MyTest {
    //DogConfig测试案例
    @Test
    public void test5(){
        ApplicationContext ctx = new AnnotationConfigApplicationContext(DogConfig.class);
        Dog ctx_dog = (Dog) ctx.getBean("dog");
        System.out.println(ctx_dog);
    }
 }

打印结果,可以看出获取到了bean
在这里插入图片描述

4. 使用注解@ImportResource将xml文件导入config

有一种情况下,我们已经创建了一些xml文件,需要读取xml文件。与此同时,我们还有配置类。
此时代码就有些混乱,因为读取这两个方式定义的Bean,方式不一样(参考两个测试代码)。
为了规范统一,将xml导入到配置类,获取Bean的时候就使用一个方式就可以。

4.1 借助注解@ImportRecource导入配置类


//@Configuration表明这个类是一个配置类
@Configuration
//@ImportResource表明需要导入其他的xml配置文件
@ImportResource(value = {"classpath:ApplicationContext.xml","classpath:beans.xml"})
//容器读取到配置文件,但无法找到文件里面写的对象dog,这个dog对象是使用注解@component生成的
@PropertySource("classpath:Dog.properties")
    //使用组建扫描器扫描这个路径下的包,看哪里使用了注解component---原来是Dog类中使用了注解,生成了对象dog
@ComponentScan(basePackages = "com.zjh.vo")
public class springConfig {

    /*
        创建一个方法,方法的返回值是该类的对象,使用注解Bean注入到容器里
        可以修改bean名称:@Bean(name = "DIY")\也可不修改,默认为方法名
    */
    @Bean(name = "DIY_Student")
    public Student createStudent(){
        Student s1 = new Student();
        s1.setName("佳");
        s1.setSex("男");
        return s1;
    }

    @Bean(name = "DIY_Cat")
    public Cat creatCat(){
        Cat c1 = new Cat();
        c1.setName("木偶猫");
        c1.setSex("公");
        return c1;
    }
}

在这里插入图片描述
在这里插入图片描述

4.2 测试

 //importResource测试案例
    @Test
    public void test3(){

        ApplicationContext ctx = new AnnotationConfigApplicationContext(springConfig.class);
        Object cat = ctx.getBean("myCat");
        Object student = ctx.getBean("myStudent");
        System.out.println(cat);
        System.out.println(student);
    }

控制台打印结果
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值