1.Bean管理-创建对象.md

zhanglei 2022年05月30日 333次浏览

1.Bean管理-创建对象

User类

package Spring5;

public class User {
    private String name;
    public void add(){
        System.out.println("add...");
    }
}

bean1.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="user" class="Spring5.User">

</bean>

Test类

public class TEST {
    @Test
    public void TestAdd(){
        ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");
        User user = context.getBean("user", User.class);
        System.out.println(user);
        user.add();
    }
}

控制台测试结果

Spring5.User@4f83df68
add...