<bean id="adminBean" class="com.service.AdminBean">
<property name="name" value="lel"></property>
<property name="id" value="1"></property>
2.IOC是什么?(面试题)
ioc(inverse of control )控制反转:所谓控制反转就是把对象(bean)对象和维护对象(bean)之间的关系的权利转移到Sqring容器中去了(ApplicationContext.xml)而程序本身不在维护了
3.DI是什么?(面试题)
di(dependency injection)依赖注入:实际上DI和IOC是同一个概念,因为在ApplicationContext.xml配置文件中bean和bean之间通过ref来维护的时候是相互依赖的,所以又叫做依赖注入。也就是控制反转。
4.Spring开发提倡接口编程,配合di技术,可以减少层与层的耦合。
因为ApplicationContext是非常消耗内存的,所以必须保证一个项目里只有一个ApplicationContext实例:
那么如何保证这有一个实例呢,就需要把ApplicationContext对象做成单例形式,如何提取单例:???
----------------------------------------------------------------------------------
package com.util;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
final public class ApplicationContextUtil {
private static ApplicationContext ac = null;
//提供一个构造方法
private ApplicationContextUtil(){
}
static {
ac = new ClassPathXmlApplicationContext("applicationContext.xml");
}
//提供一个方法供外面使用
public static ApplicationContext getApplicationContext(){
return ac;
}
}
----------------------------------------------------------------------------------
<!-- 这里就是注入的概念 通过注入设置name and id 相当于set方法在使用的时候用get方法-->
<bean id="userBean" class="com.bean.UserBean">
<property name="id" value="1"></property>
<property name="name" value="leilei"></property>
<property name="age" value="25"></property>
</bean>
<!-- 这里可以配置多个Bean类,main方法里使用的时候直接用ac.getBean()-->
<bean id="adminBean" class="com.bean.AdminBean">
<property name="id" value="2"></property>
<property name="name" value="leilei"></property>
<property name="password" value=""></property>
<!-- 这里是在一个Bean中引用另外一个Bean 需要配置属性注意ref是你在配置文件里配置的那个对应的Bean的id
name是你bean类里的getset的字段名-->
<property name="userBean" ref="userBean"></property>
</bean>
----------------------------------------------------------------------------------
public static void main(String[] args) {
//这里调用要实例化applicationContext
/**
* 1.首先得到Spring的容器对象,叫做applecationContext();
*/
ApplicationContext ac = ApplicationContextUtil.getApplicationContext();
/**
* 2.利用ApplicationContext的getBean方法得到Bean类对象,返回一个object,赋给UserService自己。
*/
AdminBean ad = (AdminBean) ac.getBean("adminBean");//这里填写的是"applicationContext.xml"里bean的id
ad.adminInfo();
}
-----------------------------------------------------------------------------------------------------------------------------------------------------
5.ApplicationContext应用上下文获取bean和从BeanFactory获取bean的区别
ApplicationContext获取bean会在加载xml文件时就实例化bean类,缺点是消耗内存
BeanFactory不会马上实例化,只有在factory.getBean("adminBean")后才会实例化。
使用方法:
BeanFactory bf = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
AdminBean ab = (AdminBean) bf.getBean("adminBean");
ab.adminInfo();
大部分项目还是用第一个方法,因为可以提前实例化,速度快,对于内存不差钱。在差内存的条件下使用第二种方法。
本文介绍了Spring框架的基本概念,包括其作为容器的角色、依赖注入(DI)和控制反转(IOC)的含义,以及如何通过配置文件管理和创建Bean实例。此外,还探讨了ApplicationContext与BeanFactory的区别。
&spm=1001.2101.3001.5002&articleId=16827645&d=1&t=3&u=ec18b5965ca641f784cf4e52c5e95032)
1129

被折叠的 条评论
为什么被折叠?



