1.Core模块的功能实现的反向控制与依赖注入、Bean配置与加载。Core模块中有Beans、BeanFactory、BeanDefinitions、ApplicationContext等几个重要概念。
2.Beans为Spring里的各种对象,一般都要配置在Spring的配置文件中;BeanFactory是为创建Bean的Factory;Spring通过BeanFactory加载各种Bean;BeanDifinition为Bean在配置文件中的定义,一般要定义id和class;ApplicationContext代表配置文件。
BeanFactory工厂
a)首先来说下BeanFactory:
BeanFactory是实例化、配置、管理众多Bean的容器。BeanFactory根据配置实例化Bean对象,并设置相互的依赖性。
BeanFactory可用接口org.springframework.factory.BeanFactory表示。BeanFactory有多种实现,最常用的为XmlBeanFactory,XmlBeanFactory能够加载XML格式的配置文件。
在Web应用程序中,我们不需要实例化BeanFactory,Web程序加载时会自动实例化BeanFactory,并加载所有的Bean;但是在Java桌面程序中,需要从BeanFactory中获取Bean,所以要实例化BeanFactory。
//获取配置资源并取得对象工厂 XmlBeanFactory factory = new XmlBeanFactory( new ClassPathResource("applicationContext.xml")); //获取Bean IService hello =(IService) factory.getBean("proxy"); hello.service("HelloBean"); factory.destroySingletons();
讯享网
参数applicationContext.xml默认在classpath下面,如果需要加载多个配置文件的话,可以用ClassPathXmlApplicationContext加载多个配置文件
讯享网ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(new String []{"...","..."}); BeanFactory factory1 = appContext;
b)XmlBeanFactory配置格式:在xmlBeanFactory中,配置文件的根节点为<beans>,里面定义了多个一个或者多个<bean>子节点,每个<bean>代表一个Bean,格式如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 配置拦截器对象 --> <bean id="methodBeforeAdviceImpl" class="com.spring.impl.MethodBeforeAdviceImpl"></bean> <bean id="theAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"> <property name="advice"> <ref local="methodBeforeAdviceImpl"/> </property> <property name="mappedName" value="*"></property><!-- 拦截所有的方法 --> </bean> <bean id="daoImpl" class="com.spring.impl.DaoImpl"></bean> <bean id="serviceImpl" class="com.spring.service.ServiceImpl"> <property name="idao" ref="daoImpl"></property> </bean> <bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="interceptorNames" value="theAdvisor"></property> <property name="target"> <ref local="serviceImpl"/> </property> </bean> </beans>
配置JavaBean
a)基本配置<bean>:通常需要定义id与class属性,class属性是必须的,如果有多个bean,id也是必须的
b)工厂模式factory-method:如果一个Bean不能直接被实例化,而是通过工厂类的某个方法创建的,需要吧<bean>的class属性设置为工厂类(或者吧factory-bean的属性设置为工厂类),factory-method属性设置为产生实例的方法:
c)构造函数<construct-tag>:如果Java Bean的构造函数带有参数,需要指定构造函数的参数,<construct-tag>指定构造参数所用的值
d).单态模式singleton:Bean可以定义为单态模式,Spring默认为单例模式,如果想使用非单例模式(称为Protootype模式),需要把singleton设置为false
讯享网<bean id="test1" class="路径" singleton=“false” >
e).配置属性<property>及设置对象属性<ref>:
bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="interceptorNames" value="theAdvisor"></property> <property name="target"> <ref local="serviceImpl"/> </property> </bean>

<password>
<value></value><!--默认是“”-->
</password>
<password>
<null/><!--设置为null-->
</password>
f)配置list
讯享网<bean id="test1" class=""> <property name="someList"> <list> <value>String /Integer/Double/Boolean等类型对象</value><!--普通类型可以直接用字符串 --> <ref bean="xxx"/><!-- Java对象 可以用ref,也可以用bean重新定义--> </list> </property> </bean>
h).配置map属性:<entry>配置map里的元素,key指定索引,alue指定值。如果value为Java对象,则使用<ref>指定,也可以通过<bean>定义,如果key为Java对象,使用key-ref属性
<bean id="test" class="...."> <map> <entry key="..."> <value>好孩子</value> </entry> <entry key-ref="...."> <ref bean="...."/> </entry> </map> </bean>
i).配置Properties属性<props>:<props>配置一个Properties对象,<prop>配置一条属性,属性key配置索引:
讯享网<property> <props> <prop key="url">http://....</prop> <prop key="name">xxx</prop> </props> </property>
j)<idref>与ref的区别:<idref>与<ref>的作用是一样,都是配置Java对象的。<idref>的用法与<ref>的用法基本相同,不同的是<idref>只有Bean的local属性,没有parent属性,例如:
<idref local="...."></idref>
Spring加载xml配置文件时,会检查<idref>配置的Bean存在不存在,而<ref>只会在第一次调用时才会检查。换句话说,如果Bean不存在,<idref>启动程序的时候就抛出错误,而<ref>只会在运行中抛出错误。
l).设置depends-on依赖对象:Spring会默认按照配置文件里的Bean的设置的先后顺序实例化Bean,但是有时候在实例化A对象之前需要先实例化后面的B对象。这时候可以使用depends-on,强制先实例化B对象:
讯享网<bean id="a" class="...." depends-on="b"></bean> <bean id="b" class="...."></bean>
m)。初始化方法init-method
n)还有属性的自动装配autoWire,依赖检查dependency,以及Bean的一些高级属性就不详细列举了

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/65192.html