Spring—— Spring Core
来源:互联网 发布:亲宝宝软件下载 编辑:程序博客网 时间:2024/06/08 14:20
IoC(inversion of control)控制反转,也就是说,控制权由对象转移,对象自己变成了被动的接受。
DI(dependency injection)依赖注入
AOP(aspect oriented programming)面向切面编程
先简单的说说这个AOP
AOP中利用面向切面的编程理论,把每个方法或者函数当做一个切面,在这个切面前后都可以执行不同的方法。
1 <bean id="service">2 <property name="interceptorNames" value="advisor"></property>3 <property name="target">目标</property>4 </bean>
这里定义了一个拦截器,一个目标bean。
1 <bean id="advisor" class="...">2 <property name="advice">//拦截器对象3 </property>4 <property name="mappedName">//拦截的方法5 </property>6 </bean>
这个目标bean在执行时,会根据配置来确定切面,精确到某个方法。而拦截的方法由上面的mappedName确定,可以通过名字或者通过正则表达式来确定。拦截器对象确定了拦截时触发的函数。
可以进行方法前拦截、方法后拦截、异常拦截。
可以看到Spring Core是spring框架中最低层核心的模块。提供了最基本的Ioc思想框架。
下面讲解一下如何使用!
由于spring是把一个类配置在xml配置文件中,从而进行一些相关的依赖或者注入等等。那么这个配置文件,肯定要被本地化存储并且进行读取。
因此在我们使用前,要进行三步:
读取spring配置文件
bean定义
注入到Ioc容器
之后我们就可以通过bean工厂的getBean获取指定的bean了。
1 最常见的读取配置文件的三种方法:
1 利用ClassPathResource读取资源文件,传入XmlBeanFactory中
1 ClassPathResource res = new ClassPathResource("application.xml")2 XmlBeanFactory factory - new XmlBeanFactory(res);3 factory.getBean("testBean");
2 利用输入流,传入到XmlBeanFactory中
1 InputStream is = new FileInputStream("C:\\ApplicationContext.xml");2 XmlBeanFactory factory - new XmlBeanFactory(is);
3 直接通过ClassPathXmlApplicationContext读取资源文件
ClassPathXmlAppliationContext appContext = new ClassPathXmlApplicationContext("aaplicationContext.xml");
4 通过在web.xml中指定配置文件的目录,自动读取。关于配置文件的路径有很多种写法,通过指定ClassPath也可以。
1 <listener> 2 <listener-class> 3 org.springframework.web.context.ContextLoaderListener 4 </listener-class> 5 </listener> 6 7 <context-param> 8 <param-name> contextConfigLocation</param-name> 9 <param-value>10 /WEB-INF/classes/bean.xml11 </param-value>12 </context-param>
2 Bean的定义这部分就没什么好说的了,就是读取配置文件,解析里面的文件定义,抽取成数据模型。
3 而注入到Ioc容器中,就是把这个数据模型读取到一个总的BeanFactory中,统一的进行管理。
我们就可以通过这个BeanFactory进行一些操作,比如GetBean()或者containsBean()等等。
杂七杂八
下面简单的罗列了一些配置的内容:
<bean id="examples.MyFactoryBean" factory-method="createInstance"/>
2 构造函数
<bean id="testBean" class="examples.ExampleBean"> <constructor-arg><ref bean="testArgs1"/></construtor-arg> <constructor-arg><ref bean="testArgs2"/></construtor-arg></bean> 相当于class testBean{ public testBean(testArgs1,testArgs2);}
3 单例模式
<bean id="testBean" class="test.testBean" singleton="false"> 每次请求生成的都是同一个bean
4 <idef> 与 <ref>作用相同,但是idef会在xml加载时 检查 bean是否存在
destroy-method 销毁,通常都是配置数据源、JDBC、输入输入流时,需要在close时销毁对象depends-on 先后顺序实例化init-method 初始化调用方法
BeanNameAware 让类知道自己bean在xml中的id名字BeanFactoryAware 获得beanFactoryInitializingBean接口执行初始化方法Disposablebean 执行销毁方法
9 属性覆盖,这个通常是在配置了一些属性,但是有其他的一些文件修改了这个属性时,使用。
比如说,我们在项目中通过spring的配置文件配置了数据源的相关信息,但是额外有一个xxx.properties进行信息的重写。当有这个文件时,可以让客户自己修改其连接信息,而不用每次都去修改繁琐的spring配置文件。
org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
- Spring—— Spring Core
- Spring——core工具包
- Spring core
- Spring core
- Spring JDBC模板类—org.springframework.jdbc.core.JdbcTemplate
- Spring JDBC模板类—org.springframework.jdbc.core.JdbcTemplate .
- Spring JDBC模板类—org.springframework.jdbc.core.JdbcTemplate .
- Spring JDBC模板类—org.springframework.jdbc.core.JdbcTemplate
- Spring Core模块
- Spring-core-io-Resource
- Spring之Core模块
- Spring之Core模块
- Spring core组件
- Spring-Core之AntPathMatcher
- Spring Core配置笔记
- Spring-framework-core-1.3
- Spring Data Core
- Spring in action--Part1-Core Spring
- android studio常用快捷键
- 整理---从手机相册获取图片的方法
- imbue
- 剑指offer之面试题23:从上往下打印二叉树
- vijos p1233
- Spring—— Spring Core
- zoj3946 Highway Project(单源最短路径)
- Xmanager4.0注册码
- 只顾眼前者,才是大赢家 -----摘自罗辑思维
- nodejs base64编码/解码
- 有参数无返回值的函数
- iOS - 验证码,倒计时按钮
- 字符串反转(reverse实现)
- hdu 5666 Segment(BC规律题)