spring bean实现的两种方式

spring bean实例化的两种方式:


xml注册bean方式:

配置xml:
<bean id="beanDemoTest" class="com.springdemo.springbeens.BeanDemoTest" init-method="testInitMethod" destroy-method="testDestroyMethod" >
        <property name="age" value="18" ></property>
        <property name="name" value="mr.monster.liu"></property>
</bean>

配置对应的类:

package com.springdemo.springbeens;
/**
 * @description: 测试xml生成bean
 * @author: Mr.monster.liu
 * @create: 2021-04-13 14:21
 **/
public class BeanDemoTest {
    private  String name;
    private  String age;
    public String getName() {
        System.out.println("获取属性值:name");
        return name;
    }
    public void testInitMethod(){
        System.out.println("初始化bean");
    }

    public void testDestroyMethod(){
        System.out.println("销毁bean");
    }
    public void setName(String name) {
        System.out.println("注入bean属性:name");
        this.name = name;
    }
    public String getAge() {
        System.out.println("获取属性值:age");
        return age;
    }
    public void setAge(String age) {
        System.out.println("注入bean属性:age");
        this.age = age;
    }
    @Override
    public String toString() {
        return "BeanDemoTest{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                '}';
    }
}

测试xml实例化过程:

	@Test
    public void xmlBeanDemo(){
        //把bean.xml 文件初始化到容器中
        ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("BeanDemo.xml");
        BeanDemoTest beanDemoTest =(BeanDemoTest) app.getBean("beanDemoTest");
        System.out.println(beanDemoTest.toString());
        //关闭容器
        ((AbstractApplicationContext) app).close();
    }

打印结果:

注入bean属性:age
注入bean属性:name
初始化bean
BeanDemoTest{name='mr.monster.liu', age='18'}
销毁bean

注:init-method和destroy-method是初始化bean实例的前置后置方法


@bean注册bean方式:

配置实现的类:
package com.springdemo.springbeens;
/**
 * @description: 测试@bean生成bean
 * @author: Mr.monster.liu
 * @create: 2021-04-13 14:21
 **/
public class BeanDemoTest {
    private  String name;
    private  String age;
    public String getName() {
        System.out.println("获取属性值:name");
        return name;
    }
    public void testInitMethod(){
        System.out.println("初始化bean");
    }
    public void testDestroyMethod(){
        System.out.println("销毁bean");
    }
    public void setName(String name) {
        System.out.println("注入bean属性:name");
        this.name = name;
    }
    public String getAge() {
        System.out.println("获取属性值:age");
        return age;
    }
    public void setAge(String age) {
        System.out.println("注入bean属性:age");
        this.age = age;
    }
    @Override
    public String toString() {
        return "BeanDemoTest{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                '}';
    }
}

测试注册:

package com.beandemo;
import com.springdemo.springbeens.BeanDemoAMethod;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
 * @description: 测试@bean方式注册bean
 * @author: Mr.monster.liu
 * @create: 2021-04-14 10:00
 **/
public class BeanTestSecond {
    @Test
    public void test(){
        //把bean.xml 文件初始化到容器中
        AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(BeanDemoAMethod.class);
        // todo @Bean 如果没设置名字  方法名默认为key
        Object beanDemoAMethod = app.getBean("beanDemoAMethod");
        System.out.println(beanDemoAMethod.toString());
        app.close();
    }
}

测试结果:

注入bean属性:age
注入bean属性:name
初始化bean
com.springdemo.springbeens.BeanDemoAMethod$$EnhancerBySpringCGLIB$$35840e98@6b4a4e18
销毁bean


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值