spring ioc和Aop
ioc也叫控制反转,在spring框架中创建对象的工作不再由开发者完成,而是交给ioc容器创建,我们直接获取即可
ioc容器
实现步骤
- 在pom.xml中添加spring依赖
- 创建配置文件,可以自定义文件名spring.xml
- 在spring.xml中配置bean标签,ioc容器通过加载bean标签来创建对象
- 调用API获取ioc创建的对象
无参构造
<!-- 配置 student 对象-->
<bean id="stu" class="com.southwind.entity.Student"</bean>
配置一个bean标签:
- id,对象名
- class,对象的模板类
接下来通过API获取对象,spring提供了两种方法:id或者运行时类;
1.通过id获取对象
//1.加载spring.xml配置文件
ApplicationContext applicationgContext=new ClassPathXmlApplicationgContext("spring.xml");
//2.通过id值获取对象
Student stu=(Student) applicationgContext.getBean("stu");
System.out.print(stu);
第一步:加载spring.xml配置文件,生成ApplicationC