文章目录:
1.Spring容器创建对象的特点
1. 容器对象ApplicationContext:接口,通过ApplicationContext对象获取要使用的其他Java对象,执行getBean("<bean>的id")。
2. spring默认的是调用类的无参构造方法,创建对象。
3. spring读取配置文件,一次创建好所有的Java对象,都放到map中。
ApplicationContext容器,会在容器对象初始化时,将其中的所有对象一次性全部装配好。以后代码中若要使用到这些对象,只需从内存中直接获取即可。
1.1 Spring是在什么时候创建的对象?
首先,我们在spring配置文件(beans.xml)中加入下面这行代码:👇👇👇
<bean id="someService" class="com.bjpowernode.service.impl.SomeServiceImpl" />
对应的接口和接口的实现类如下: 👇👇👇
package com.bjpowernode.service; /** * */ public interface SomeService { void doSome(); }
package com.bjpowernode.service.impl; import com.bjpowernode.service.SomeService; public class SomeServiceImpl implements SomeService { /** * spring默认使用的是无参构造方法来创建对象 * 如果定义了有参构造方法,应该再定义一个无参构造方法 */ public SomeServiceImpl() { System.out.println("SomeServiceImpl的无参构造方法"); } @Override public void doSome() { System.out.println("执行了业务方法doSome()"); } }
编写相应的测试方法:👇👇👇
/* * spring是在什么时候创建的对象 * 创建spring容器对象的时候,会读取配置文件,创建文件中声明的Java对象 */ @Test public void test02() { String config="beans.xml"; //执行此行代码,会创建配置文件中的所有对象,调用了无参构造方法 ApplicationContext ctx=new ClassPathXmlApplicationContext(config); SomeService service=ctx.getBean(SomeService.class); service.doSome(); }
在spring配置文件(beans.xml)中加入下面这行代码:👇👇👇
<bean id="someService" class="com.bjpowernode.service.impl.SomeServiceImpl" /> <bean id="someService1" class="com.bjpowernode.service.impl.SomeServiceImpl" />
接口和实现类与上面的例子相同。
编写相应的测试方法:👇👇👇
/** * spring容器创建对象,一次创建几个 * 在创建容器对象时,会把配置文件中的所有对象都创建出来(spring的默认规则) */ @Test public void test03() { String config="beans.xml"; ApplicationContext ctx=new ClassPathXmlApplicationContext(config); // SomeService service=ctx.getBean(SomeService.class); // service.doSome(); }
1.3 获取Spring容器中定义的对象数量、名称
<bean id="someService" class="com.bjpowernode.service.impl.SomeServiceImpl" /> <bean id="someService1" class="com.bjpowernode.service.impl.SomeServiceImpl" />
@Test public void test04() { String config="beans.xml"; //执行此行代码,会创建配置文件中的所有对象,调用了无参构造方法 ApplicationContext ctx=new ClassPathXmlApplicationContext(config); //获取容器中定义对象的数量 int nums=ctx.getBeanDefinitionCount(); System.out.println("容器中定义对象的数量===" + nums); //获取容器中定义的对象名称 String names[]=ctx.getBeanDefinitionNames(); for(String name : names) { System.out.println("容器中定义的对象名称===" + name); } }
1.4 Spring创建非自定义类对象、没有接口的类对象
<bean id="someService" class="com.bjpowernode.service.impl.SomeServiceImpl" /> <bean id="someService1" class="com.bjpowernode.service.impl.SomeServiceImpl" /> <!-- 创建非自定义对象 --> <bean id="mydate" class="java.util.Date" /> <!-- 通过没有接口的类来创建对象 --> <bean id="otherService" class="com.bjpowernode.service.OtherService" />
其中Date类是Java中日期处理相关的类,之后我们又自定义了一个OtherService类,代码如下:👇👇👇
package com.bjpowernode.service; /** * */ public class OtherService { public void doOther() { System.out.println("执行OtherService的doOther()方法"); } }
//让spring创建非自定义对象 //有class就能让spring创建对象 @Test public void test05() { String config="beans.xml"; ApplicationContext ctx=new ClassPathXmlApplicationContext(config); Date date= (Date) ctx.getBean("mydate"); System.out.println("date===" + date); OtherService service= (OtherService) ctx.getBean("otherService"); service.doOther(); }