2. Spring 创建对象
2022年9月6日
2. Spring 创建对象
spring 是使用的 di 实现了 ioc 的功能, spring 底层创建对象,使用的是反射机制
spring 是一个容器,管理对象,给属性赋值, 底层是反射创建对象
bean 实例:
@Data @AllArgsConstructor @NoArgsConstructor public class User { private Integer id; private String username; private String password; }
接口
public interface SomeService { void doSome(); }
实现类
public class SomeServiceImpl implements SomeService { @Override public void doSome() { System.out.println("执行doSome()方法"); } }
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"> <!--告诉spring创建对象 声明bean , 就是告诉spring要创建某个类的对象 id:bean标识符,相当于对象名称 class:类的全限定名称(不能是接口,因为spring是反射机制创建对象,必须使用类) name:别名 可以多个,可以以空格,逗号,或分号隔开 (id值会映射到name) spring就完成 SomeService someService = new SomeServiceImpl(); spring是把创建好的对象放入到map中, spring框架有一个map存放对象的 springMap.put(id的值, 对象); 例如 springMap.put("someService", new SomeServiceImpl()); 一个bean标签声明一个对象 --> <bean id="user" class="com.demo.bean.User" name="u u1,u2;u3"> <!-- value:基本类型或字符串 ref:对象类型 --> <property name="username" value="张三"/> <property name="password" value="123"/> </bean> <bean id="helloSpring" class="com.demo.service.SomeServiceImpl"/> <!--别名--> <alias name="user" alias="user2"/> </beans>
测试类
@Test public void Test() { /* * 使用Spring容器创建对象 * 1. 指定Spring配置文件的名称 * 2. 创建表示Spring容器的对象, ApplicationContext * ApplicationContext就是表示Spring容器, 通过容器获取对象 * 3. ClassPathXmlApplicationContext表示从类路径中加载Spring的配置文件 * 4. 从容器中获取某个对象, 你要调用的对象的方法 getBean("配置文件中的bean的id值"); * 5. 使用Spring创建好的对象 * */ String config = "beans.xml"; // ① ApplicationContext context // ② = new ClassPathXmlApplicationContext(config); // ③ SomeService someService = context.getBean("someService", SomeService.class); // ④ someService.doSome(); // ⑤ User user = context.getBean("user", User.class); System.out.println(user); }
Spring 在创建容器时,会创建配置文件中所有的对象,默认调用的是无参构造方法.
添加实现类的构造方法
public class SomeServiceImpl implements SomeService { public SomeServiceImpl() { System.out.println("SomeServiceImpl无参构造"); } @Override public void doSome() { System.out.println("执行doSome()方法"); } }
配置文件
<bean id="someService" class="com.demo.service.impl.SomeServiceImpl"/> <bean id="someService1" class="com.demo.service.impl.SomeServiceImpl"/>
测试
@Test public void Test() { String config = "beans.xml"; ApplicationContext context = new ClassPathXmlApplicationContext(config); context.getBean("someService", SomeService.class); }
输出结果
获取容器中对象信息的 api
使用 Spring 提供的方法,获取容器中定义的对象数量
获取容器中的对象数量
getBeanDefinitionCount()
获取全部对象的名称
getBeanDefinitionNames()
测试:
@Test
public void Test() {
String config = "beans.xml";
ApplicationContext context = new ClassPathXmlApplicationContext(config);
int i = context.getBeanDefinitionCount();
System.out.println(i);
String[] names = context.getBeanDefinitionNames();
for (String name : names) {
System.out.println(name);
}
}
结果: