spring的BeanFactory和AplicationContext
org.springframework.aop ——Spring的面向切面编程,提供AOP(面向切面编程)的实现
org.springframework.asm——spring 2.5.6的时候需要asm jar包,spring3.0开始提供它自己独立的asm jar包
org.springframework.aspects——Spring提供的对AspectJ框架的整合
org.springframework.beans——所有应用都用到,包含访问配置文件,创建和管理bean等,是Spring IOC的基础实现。
org.springframework.context.support——Spring context的扩展支持,用于MVC方面
org.springframework.context——提供在基础IOC功能上的扩展服务,此外还提供许多企业级服务的支持,有邮件服务、任务调度、JNDI定位,EJB集成、远程访问、缓存以及多种视图层框架的支持。
org.springframework.core——Spring的核心工具包,其他包依赖此包
org.springframework.expression——Spring表达式语言
org.springframework.instrument.tomcat——Spring对tomcat连接池的集成
org.springframework.instrument——Spring对服务器的代理接口
org.springframework.jdbc——对JDBC 的简单封装
org.springframework.jms——为简化jms api的使用而做的简单封装
org.springframework.orm——整合第三方的orm实现,如hibernate,ibatis,jdo以及spring 的jpa实现
org.springframework.oxm——Spring对于object/xml映射的支持,可以让JAVA与XML之间来回切换
org.springframework.test——对JUNIT等测试框架的简单封装
org.springframework.transaction——为JDBC,HIBERNATE,JDO和JPA提供的一致性的声明式和简单编程式事务管理
org.springframework.web.portlet——Spring MVC的增强
org.springframework.web.servlet——对J2EE6.0 servlet3.0的支持
org.springframework.web.struts——整合struts框架的支持,可以更方便更容易的集成Struts框架。
org.springframework.web——包含Web应用开发时,用到Spring框架时所需的核心类,包括自动载入WebApplicationContext特性的类、Struts与JSF集成类、文件上传的支持类、Filter类和大量工具辅助类。
?
?
1.spring的7个模块
spring core模块:包含BeanFactory,BeanFactory是spring框架的核心,实现依赖注入【使个组件的依赖关系从代码中独立出来,使用配置文件即可实现这种依赖关系】和bean声明周期的管理。spring context模块:该模块使spring成为框架,它通过配置文件将各个组件组合在一起,使组件之间可以正常访问。包括ApplicationContext类,ApplicationContext继承BeanFactory接口并扩展功能。此外还有JSM,JNDI访问,远程调用等内容。DAO模块、ORM模块、AOP模块、web模块和MVC模块。2.spring的管理容器--BeanFactory和ApplicationContext
?????? 1.BeanFactory接口:采用工厂模式,实例化和分发各种组件。实例化时自动根据配置文件applicationContext.xml(也可以其它名字)创建bean之间的依赖关系,<ref>来实现bean之间的依赖。
BeanFactory接口的5个方法,主要实现类:XMLBeanFactory
boolean containsBean(String str)Object getBean(String str)Object getBean(String str,Class)boolean isSingleton(String str)String[] getAliases(String str) //别名加载BeanFactory
InputStream is=new FileInputStream("WEB-INF\\applicationContext.xml");XMLBeanFactory bf=new XMLBeanFctory(is);
MyBean mb=(MyBean)bf.getBean("mybean");
??????
????? ?2.?ApplicationContext接口:覆盖了BeanFactory的所有功能,一般使用ApplicationContext,资源少的时候才用BeanFactory.
实现类:ClassPathXmlApplication?? FileSystemXmlApplication? XmlWebApplicationContext
其它功能
国家化支持、事件传播、对资源的访问【spring可以通过Application.getResource()方法对资源(文件、web页面)进行访问】
加载ApplicationContext容器
ApplicationContext context=new FileSystemApplicationContext("applicationContext.xml");MyBean mb=(MyBean)contxt.getBean("mybean");
?
?在web.xml文件中配置
Listener接口方式
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value></context-param><listener> <listener-class> org.springframework.web.context.ContextLoadLisener</listener-class></listener>
?servlet接口方式
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value></context-param><servlet> <servlet-name>context</servlet-name> <servlet-class> org.springframework.web.context.ContextLoaderServlet </servlet-class><load-on-startup>1</load-on-startup></servlet>
?WebApplicationContextUtils.getWebApplicationContext()方法获得ApplicationContext的引用
?
???? ? 3.依赖注入的两种方式: 设值注入? ?构造函数注入
设值注入
??? 备案中包含setter getter方法,配置
?
<bean id="mybean" singleton="true" init-mehod="startInit" destroy-method="closeBean" depends-on="DealAction"><property name="name"> <value>林建科</value></property><property name="password"> <value>null</value></property><property name="department"><ref bean="department"><!--对应另一个bean的id--></property><property name="friedns"> <list> <value>林建科1</value> <value>林建科2</value> </list></property><property name="friedns"> <set> <value>林建科1</value> <value>林建科2</value> </set></property><property name="favorite"> <map> <entry key="sport"> <value>篮球</value> </entry> <entry key="music"> <value>因为爱情</value> </entry> </map></property><property name="roommate"> <props> <prop key="张">张教授</prop> <!--值为string类型--> <prop key="林">林教授</prop> <!--值为string类型--> </props></property>
构造函数注入?
public class MyBean{private String name;private String sex;private Department department;public MyBean(String name,String sex,Department department){ //构造函数 this.name=name;.......}}
?配置如下,用 constructor-arg?? 【type参数类型和index指定参数顺序】
<bean id="mybean" singleton="true" init-mehod="startInit" destroy-method="closeBean" depends-on="DealAction"><constructor-arg type="java.lang.String" index="0"> <value>林科</value></constructor-arg><constructor-arg type="java.lang.String" index="1"> <value>男</value></constructor-arg><constructor-arg> <ref bean="department"></constructor-arg></bean><bean id="mybean" > <!--另一个bean的配置--><property name="name"><value>林克</value></property></bean>
?
?
?