@Configuration,@ImportResource,@PropertySources使用方法

准备工作

仅仅只需要pom.xml文件里面的配置下对应的jar包
特此声明: 下面提到的configClass是公用的类文件

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

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
    </dependency>
  </dependencies>

第一种

使用最简单的方式,没有外部的配置文件

Student类

public class Student {

    String name;
    String age;
    String sex;

    public Student() {

    }

    public Student(String name, String age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

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

configClass类配置文件

@Configuration
public class ConfigClass {

    /*
    创建方法,方法的返回值是对象,在方法的上面加入@bean

    @bean 把对象注入到容器中
    作用相当于bean标签

    说明:@bean不指定名称,默认就是方法名
    */
    @Bean
    public Student studentconfig(){
        /*
        Student student=new Student();
        student.setAge("18");
        student.setName("南香香");
        student.setSex("男");
        */
        Student student1=new Student("南香香","18","男");
        return student1;
    }

    /*
     指定对象在容器的名称(指定<bean>标签中的id属性)
     */

    @Bean(name = "BeanId")
    public Student studentconfigbean(){
        Student student=new Student("南香香","120","男");
        return student;
    }
}

测试类

@Test
    public void test01(){
        String config= "student.xml";
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext(config);
        Student student= (Student) applicationContext.getBean("mystudent");
        System.out.println("容器对象为"+student);
}

第二种

public class Cat {

    String cardid;

    String name;

    public Cat(String cardid, String name) {
        this.cardid = cardid;
        this.name = name;
    }

    public String getCardid() {
        return cardid;
    }

    public void setCardid(String cardid) {
        this.cardid = cardid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

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

    public Cat() {
    }
}

cat.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.xiaonan.pojo.Cat">
        <property name="name" value="小花"></property>
        <property name="cardid" value="111"></property>
    </bean>

</beans>

configClass类配置文件

@Configuration
@ImportResource(value = "classpath:cat.xml")
public class ConfigClass {

    /*
    创建方法,方法的返回值是对象,在方法的上面加入@bean

    @bean 把对象注入到容器中
    作用相当于bean标签

    说明:@bean不指定名称,默认就是方法名
    */
    @Bean
    public Student studentconfig(){
        /*
        Student student=new Student();
        student.setAge("18");
        student.setName("南香香");
        student.setSex("男");
        */
        Student student1=new Student("南香香","18","男");
        return student1;
    }

    /*
     指定对象在容器的名称(指定<bean>标签中的id属性)
     */

    @Bean(name = "BeanId")
    public Student studentconfigbean(){
        Student student=new Student("南香香","120","男");
        return student;
    }

}

测试类文件

    @Test
    public  void configcat(){
        ApplicationContext applicationContext=new AnnotationConfigApplicationContext(ConfigClass.class);
        Cat cat= (Cat) applicationContext.getBean("mycat");
        System.out.println("容器对象为"+cat);

    }

第三种

tiger.properties

tiger.name=东北老虎
tiger.age=16

configClass

@Configuration
@PropertySource(value = "classpath:tiger.properties")
@ComponentScan(basePackages = "com.xiaonan.pojo")
public class ConfigClass {

    /*
    创建方法,方法的返回值是对象,在方法的上面加入@bean

    @bean 把对象注入到容器中
    作用相当于bean标签

    说明:@bean不指定名称,默认就是方法名
    */
    @Bean
    public Student studentconfig(){
        /*
        Student student=new Student();
        student.setAge("18");
        student.setName("南香香");
        student.setSex("男");
        */
        Student student1=new Student("南香香","18","男");
        return student1;
    }

    /*
     指定对象在容器的名称(指定<bean>标签中的id属性)
     */

    @Bean(name = "BeanId")
    public Student studentconfigbean(){
        Student student=new Student("南香香","120","男");
        return student;
    }

}

tiger类文件

@Component("tiger")
public class Tiger {

    @Value("${tiger.name}")
    String name;

    @Value("${tiger.age}")
    String age;

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

    public Tiger() {

    }

    public Tiger(String name, String age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
}

测试类

    @Test
    public  void configtiger(){
        ApplicationContext applicationContext=new AnnotationConfigApplicationContext(ConfigClass.class);
        Tiger tiger= (Tiger) applicationContext.getBean("tiger");
        System.out.println("容器对象为"+tiger);
    }

注意!!!

那就多看看configClass类文件,看你使用的那种方式,然后在去增减对应的注解

文章全部是本人原创,请勿转发,谢谢配合,版权所有-南香香-你会喜欢我吗

评论区
头像