Sping

来源:互联网 发布:编程就业培训学校 编辑:程序博客网 时间:2024/06/02 19:25

1.  Spring 的作用及优势  

我们在使用Spring 框架时,主要是使用Spring 容器的两个特性:IoC 和AoP。 

IoC 全称Inverse of Control(反向控制戒控制反转)。 
在类和类乊间存在控制权,控制权指的是对象的创建和使用, 
比如有类A 和类B,我们乊前的做法是在A 中调用B,那么控制权就在A 中,这样做的耦合度较高
如果修改了B,A 也要做相应修改。 
 
引入Spring 框架后,控制权由spring 容器来负责。当A 想使用B 时,需要由Spirng 容器通过 
配置文件迚行注入。这种思想就是IoC(为了更好的理解,我们可以这样认为,对象创建和使用 
的控制权转移到了Spring 容器,由Spring 容器来控制)。 
 
AOP 为Aspect Oriented Programming 的缩写,意为:面向切面编程(也叫面向方面), 
可以通过预编译方式和运行期劢态代理实现在丌修改源代码的前提下给程序劢态统一添加功能的一
种技术。 

Struts2 中的拦截器,就是使用AOP 的思想。使用AOP 思想编写程序,会是程序更加灵活。 

 
一般而言,使用Spring 框架的主要作用: 
我们会使用IoC 整合组件(各种Bean),使用AOP 来管理事务。
 

2.  Spring 容器对Bean 组件的管理  ** 

1)  Bean 对象创建的时机 
 默认是随着容器创建,可以使用lazy-init=true(在调用getBean 创建)延迟创建 
也可以用<beans default-lazy-init="true"/>批量延迟创建 。
2)  Bean 对象的创建模式  

默认是单例,可以使用scope 属性改变。

 singleton单例,每次调用getBean 返回同一个 。
Spring 容器是通过单例模式创建Bean 对象的。使用单例模式有风险,风险在于多线程并发访问时会有一些状况。prototype原型,每次调用getBean 返回一个新的.。
设置创建bean 的模式为原型模式(prototype)即可以取消容器默认单例模式创建对象request仅限于Web 环境,表示bean 对象生命周期和request 生命周期相同。session仅限于Web 环境 ,同session 。global session仅限于Web 环境,相当于application 。  

<?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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd           http://www.springframework.org/schema/aop            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd           http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd           http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context-3.0.xsd"default-lazy-init="false">            <bean id="chinese" lazy-init="true"scope="prototype"class="com.demo.bean.Chinese" /><bean id="english" class="com.demo.bean.English" /></beans>



3)  Bean 对象初始化和销毁 
init-method 属性用于指定初始化方法 
destroy-method 属性用于指定销毁方法,仅适用于singleton 模式 

<?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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd           http://www.springframework.org/schema/aop            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd           http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd           http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context-3.0.xsd"default-lazy-init="false">            <bean id="chinese" lazy-init="true"scope="prototype" class="com.demo.bean.Chinese" /><bean id="english"init-method="initBean"scope="prototype"destroy-method="destoryBean" class="com.demo.bean.English" /> </beans>

public class English implements HelloBean {public English(){System.out.println("创建MyBean对象"); }@Overridepublic void salHello() { System.out.println("English:Hello...");}public void initBean(){System.out.println("执行MyBean对象的初始化!"); }public void destoryBean(){System.out.println("执行MyBean对象的销毁!释放资源!"); }}

 

3.IOC

3.1 DI 依赖注入

DI(依赖注入)是IoC 实现的重要技术,有如下2 中方式: 
1)  setter 方式注入 
2)  构造方式注入 

注入类型有如下几种:简单值、集合、bean 对象。

实现IoC 是由Spring 容器来完成的,Spring 容器通过依赖注入DI 建立起对象(组件、Bean) 之间的关系。 
我们可以这样理解:DI 是IoC 实现的一种手段,Ioc 通过DI 来实现。 

 

public interface HelloBean {abstract void salHello();}public class English implements HelloBean {public English(){System.out.println("创建MyBean对象"); }@Overridepublic void salHello() { System.out.println("English:Hello...");}}public class Chinese implements HelloBean {@Overridepublic void salHello() { System.out.println("Chinese 你好...");}}
public class Test {private static final String CONFIG="applicationContext.xml";private static final String [] CONFIGS={"applicationContext.xml"};public static void main(String[] args) {//1.//ApplicationContext ac1=new ClassPathXmlApplicationContext(CONFIG);ApplicationContext ac1=new ClassPathXmlApplicationContext(CONFIGS);//2.ApplicationContext ac2=new FileSystemXmlApplicationContext(CONFIG);//3.Resource resource=new FileSystemResource(CONFIG);BeanFactory ac3=new XmlBeanFactory(resource);UseBean bean=(UseBean)ac1.getBean("useBean");bean.show();}}
1)setter 方式注入 
<?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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd           http://www.springframework.org/schema/aop            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd           http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd           http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context-3.0.xsd"default-lazy-init="false">            <bean id="chinese" lazy-init="true"scope="prototype" class="com.demo.bean.Chinese" /><bean id="english"init-method="initBean"scope="prototype"destroy-method="destoryBean" class="com.demo.bean.English" /><bean id="useBean" class="com.demo.bean.UseBean"><property name="helloBean" ref="chinese" /><property name="name" value="张三" /><!--
<property name="name"><value>张三</value></property> 
--></bean></beans>

 
public class UseBean {private HelloBean helloBean;private String name;public void show(){helloBean.salHello();} /** * @param helloBean the helloBean to set */public void setHelloBean(HelloBean helloBean) {this.helloBean = helloBean;} /** * @param name the name to set */public void setName(String name) {this.name = name;}} 
 CollectionBean
<?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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd           http://www.springframework.org/schema/aop            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd           http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd           http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context-3.0.xsd"default-lazy-init="false"><bean id="collectionBean" class="com.demo.bean.collection。CollectionBean"><property name="city"><list><value>北京</value><value>上海</value><value>深圳</value></list></property><property name="name"><set><value>tom</value><value>jack</value><value>rose</value></set></property><property name="books"><map><entry key="ISBN1" value="STRUTS框架开发"></entry><entry key="ISBN2" value="Hibernate"></entry><entry key="ISBN3" value="Spring"></entry></map></property><property name="params"><props><prop key="username">root</prop><prop key="pass">pass</prop><prop key="driverClass">com.mysql.jdbc.Driver</prop></props></property></bean></beans>

public class CollectionBean {private List<String> city;private Set<String> name;private Map<String,Object> books;private Properties params;public void show(){System.out.println("##List城市信息##"); for(String s:city){   System.out.println(s); }  System.out.println("##Set朋友信息##"); for(String s:name){   System.out.println(s); }  System.out.println("##Map图书信息##"); Set<String> keys = books.keySet(); for(String key:keys){   System.out.println(key+" : "+books.get(key)); }  System.out.println("##Properties数据库连接参数信息##"); Set<Object> ids = params.keySet(); for(Object id:ids){   System.out.println(     id+" : "+params.getProperty(id.toString())); } } public void setCity(List<String> city) {this.city = city;} public void setName(Set<String> name) {this.name = name;} public void setBooks(Map<String, Object> books) {this.books = books;} public void setParams(Properties params) {this.params = params;}  } 
2)构造方式注入
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd           http://www.springframework.org/schema/aop            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd           http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd           http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context-3.0.xsd"default-lazy-init="false">            <bean id="chinese" lazy-init="true"scope="prototype" class="com.demo.bean.Chinese" /><bean id="english"init-method="initBean"scope="prototype"destroy-method="destoryBean" class="com.demo.bean.English" /><bean id="useBean" class="com.demo.bean.UseBean"><!-- <property name="helloBean" ref="chinese" /> --><constructor-arg index="0" ref="chinese"></constructor-arg><constructor-arg index="1" ref="张三"></constructor-arg></bean></beans>
public class UseBean {private HelloBean helloBean;private String name;public UseBean(HelloBean helloBean,String name){this.helloBean=helloBean;this.name=name;}public void show(){helloBean.salHello();}}

 

4.注解方式配置  *

常用配置方式有XML 文档配置,还有一种是通过注解方式配置。 
采用注解方式的目的就是为了简化XML 配置文件。 
注解方式(也叫注释)是JDK5 版本提供的,之前的版本不支持。 
Spring2.5 版本后支持注解方式,之前的版本不支持。
 

4.1  组件自动扫描功能  ** 

  1)  扫描Bean 组件的注解,替代xml 中的<bean>元素的定义。   @Service  用于Service 业务组件 
  @Control  用于Action 控制组件 
  @Respository  用于DAO 数据访问组件 
  @Component  用于其他组件 
  Bean 组件扫描到容器后, 
默认名字为类名(首字母小写)如果需要自定义名称可以使用@Service("id 名") 2)  依赖注入的注解标记   @Resource  按名称@Resource(name="id 名") 
  @AutoWired  按名称 
  @Autowired 
  @Qualifier("id 名") 3)  其他注解   @Scope  等价于<bean scope=""> 
  @PostConstruct  等价于<bean init-method=""> 
  @PreDestroy  等价于<bean destroy-method=""> 


0 0
原创粉丝点击