Spring初始化机制

一、main的运行入口

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");

说明:调用这个程序后,程序会扫描spring.xml文件,并加载其中包含的类,会执行其中的构造函数。

二、配置spring.xml文件

有两种:指定类名称 和 指定标签名称

1. 指定类名称:

<bean id="one" class="spring_test.One">
        <constructor-arg name="one" value="one"/>
</bean>

2. 指定标签名称:

<context:component-scan base-package="spring_test"/>

说明:

第二种方式会很简洁,但需要在对应的类前面加上这个标签:@Component

三、原理

Spring本身就是采用了IOC和AOP的结合,其中IOC指的是反射,AOP指的是动态代理。

猜你喜欢

转载自www.cnblogs.com/xiatianyu/p/9094961.html