学习笔记之Spring

重要概念

Spring:容器框架,配置bean,并维护bean之间的关系

IOC:控制反转(inverseof control),把创建bean和维护他们之间的关系的权利,从程序中转移到spring容器,程序不再维护。

DI:依赖注入(dependencyinjection),和ioc是同一个概念。

使用Spring

1.导入jar包

Spring.jar、commons-logging.jar

2.编写配置文件

applicationContext.xml,该文件一般放在/src目录下。 (hibernate配置文件是hibernate.cfg.xml,struts的配置文件是struts-config.xml)

<pre name="code" class="html"><?xmlversion="1.0"encoding="utf-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"
        xmlns:jee="http://www.springframework.org/schema/jee"xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd
            http://www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee-2.5.xsd
            http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
</beans>

 
   

3.配置bean

部分属性

【id】:相当于bean实例的变量名

【class】:指明类的全路径

【parent】:指明父类,体现继承

【init-method】:指定一个方法,在实例化该bean时做一些初始化工作

【destroy-method】:指定一个方法,在销毁该bean时做一些回收工作

【scope】:决定bean的作用域

singleton:整个容器只创建一个实例bean(默认)

prototype:每次调用getBean()方法都会重新创建一个实例,性能低。

request:一次请求创建一个实例

session:一次会话创建一个实例

global session:一个全局会话对应一个实例

子元素

【property】:为当前的bean注入属性,可以是基本类型的数据、对象、内部bean

当bean的某个属性为数组、集合、map时,如何注入

(1)给数组注入值

<propertyname="empName">
         <list>
                   <value>小明</value>
                   <value>小明小明</value>
                   <value>小明小明小明小明</value>
         </list>
</property>

(2)给list注入值 list 中可以有相同的对象

<propertyname="empList">
         <list>
                   <refbean="emp2"/>
                   <refbean="emp1"/>
                   <refbean="emp1"/>
                   <refbean="emp1"/>
                   <refbean="emp1"/>
                   <refbean="emp1"/>
                   <refbean="emp1"/>
         </list>
</property>


(3)给set注入值 set不能有相同的对象

<propertyname="empsets">
         <set>
                   <refbean="emp1"/>
                   <refbean="emp2"/>
                   <refbean="emp2"/>
                   <refbean="emp2"/>
                   <refbean="emp2"/>
         </set>
</property>


(4)给map注入值 map只有key不一样,就可以装配value

<propertyname="empMaps">
         <map>
                   <entrykey="11"value-ref="emp1"/>
                   <entrykey="22"value-ref="emp2"/>
                   <entrykey="22"value-ref="emp1"/>
         </map>
</property>


(5)给属性集合配置

<propertyname="pp">
<span style="white-space:pre">	</span></span><props>
                   <propkey="pp1">abcd</prop>
                   <propkey="pp2">hello</prop>
         </props>
</property> 

 
                   
    

4.使用bean

有两种方法获得bean实例:

方法一:通过ApplicationContext

ApplicationContextac=new ClassPathXmlAppicationContext(“XXX.xml”);
Ac.getBean(“XXX”);

方法二:通过Bean工厂

BeanFactory bf=newXmlBeanFactory(new ClassPathResource(“XXX.xml”));
Bf.getBean(“XXX”);

比较:

a)  方法1会预先生成bean的实例,耗内存,但是程序运行速度快。

b)  方法2到用的时候才创建bean实例,节约内存,但程序运行速度慢,适用于低配设备、移动设备等。


AOP编程

Aspect oriented programming(面向切面编程\面向方面编程),是比面向对象编程更高层次的编程思想。

1.定义接口

2.编写对象(实现上面的接口)

3.编写Advise(通知):要继承相应的通知接口

4.配置代理

总结

1.不再new类的实例,依赖spring框架自动产生实例。

2.Bean的属性值可以在applicationContext.xml中配置。

3.Spring实际上是一个容器框架,可以配置各种bean,并且维护他们之间的关系。

4.在J2ee中,可以接管各个层(web层、service层、dao层、持久层、数据源等)。

 

发布了36 篇原创文章 · 获赞 23 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/Cmainlove/article/details/41680875
今日推荐