Spring (五)注解 @Component,@Scope

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

单例代码

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"
       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 https://www.springframework.org/schema/context/spring-context.xsd">


    <!-- 扫描entity包 -->
    <context:component-scan base-package="entity" />

</beans>

People

import org.springframework.stereotype.Component;

// 相当于配置一个bean,bean默认id为首字母小写,即people
@Component
public class People {

    private String name;
    // 注意为Integer
    private Integer age;

    public People() {
        System.out.println("People无参构造函数");
    }

    public People(String name, Integer age) {
        this.name = name;
        this.age = age;
        System.out.println("People二参构造函数:name=" + name + "   age=" + age);
    }

	get,set,toString函数
}

Test

import entity.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

    public static void main(String[] args) {
        Test test = new Test();
        test.test1();
    }

    public void test1() {
        System.out.println("在加载xml文件时已经单例bean的对象new出来了");
        String xmlPath = "applicationContext.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        System.out.println("-------------------------------------");

        System.out.println("---------people:---------");
        People people = (People) applicationContext.getBean("people");
        System.out.println(people);

        System.out.println("---------people2:---------");
        People people2 = (People) applicationContext.getBean("people");
        System.out.println(people2);

        System.out.println("是否是同一个对象");
        System.out.println(people.equals(people2));

    }

}

结果输出

在加载xml文件时已经单例bean的对象new出来了
People无参构造函数
-------------------------------------
---------people:---------
People{name='null', age=null}
---------people2:---------
People{name='null', age=null}
是否是同一个对象
true

设置bean的id

People

import org.springframework.stereotype.Component;

// 设置bean的id为peopleId
@Component(value = "peopleId")
public class People {

    private String name;
    // 注意为Integer
    private Integer age;

    public People() {
        System.out.println("People无参构造函数");
    }

    public People(String name, Integer age) {
        this.name = name;
        this.age = age;
        System.out.println("People二参构造函数:name=" + name + "   age=" + age);
    }
	get,set,toString函数
}

Test

import entity.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

    public static void main(String[] args) {
        Test test = new Test();
        test.test1();
    }

    public void test1() {
        System.out.println("在加载xml文件时已经单例bean的对象new出来了");
        String xmlPath = "applicationContext.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        System.out.println("-------------------------------------");

        System.out.println("---------people:---------");
        People people = (People) applicationContext.getBean("peopleId");// id改成了peopleId
        System.out.println(people);

        System.out.println("---------people2:---------");
        People people2 = (People) applicationContext.getBean("peopleId");// id改成了peopleId
        System.out.println(people2);

        System.out.println("是否是同一个对象");
        System.out.println(people.equals(people2));

    }

}

结果输出

在加载xml文件时已经单例bean的对象new出来了
People无参构造函数
-------------------------------------
---------people:---------
People{name='null', age=null}
---------people2:---------
People{name='null', age=null}
是否是同一个对象
true

多例代码

添加@Scope(“prototype”)
People

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

// 相当于配置一个bean,bean默认id为首字母小写,即people
@Component
@Scope("prototype")
public class People {
    
    private String name;
    // 注意为Integer
    private Integer age;

    public People() {
        System.out.println("People无参构造函数");
    }

    public People(String name, Integer age) {
        this.name = name;
        this.age = age;
        System.out.println("People二参构造函数:name=" + name + "   age=" + age);
    }

	get,set,toString函数
}

测试结果

在加载xml文件时已经单例bean的对象new出来了
-------------------------------------
---------people:---------
People无参构造函数
People{name='null', age=null}
---------people2:---------
People无参构造函数
People{name='null', age=null}
是否是同一个对象
false

总结

@Component作用为实现bean的注入(需要在xml设置扫描)

  1. @Component相当于在xml中配置:(在不设置id的情况下默认id为类名首字母小写)
    <bean id=“people” class=“entity.People” >
  2. @Component(value = “peopleId”)相当于xml配置:(将id设为value值,即peopleId)
    <bean id=“peopleId” class=“entity.People” >

@Scope(“prototype”):多例

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值