3.Spring IOC

1.IOC概述:java

所謂IOC:就是經過容器來控制業務對象之間的依賴關係,而非傳統實現中,web

由代碼操控。spring

控制權由應用代碼轉到了外部容器,控制權的轉移,就是反轉。緩存

 

BeanFactory:爲Spring框架最核心接口,提供了高級Ioc的配置機制app

ApplicationContext創建在BeanFactory之上,框架

BeanFactory爲IOC容器,ApplicationContext爲應用上下文函數

 

1.applicationContext.xmlui

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 7        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
 8        http://www.springframework.org/schema/context 
 9        http://www.springframework.org/schema/context/spring-context-3.1.xsd
10        http://www.springframework.org/schema/tx 
11        http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
12        http://www.springframework.org/schema/aop
13        http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
14        
15      
16     <bean id="car" class="com.asm.Car"
17     p:brand="紅旗"
18     p:color="red"
19     p:maxSpeed="200"
20     />
21     
22  </beans>

 

 

 1 package com.asm;
 2 
 3 import org.springframework.beans.BeansException;
 4 import org.springframework.beans.factory.BeanFactory;
 5 import org.springframework.beans.factory.BeanFactoryAware;
 6 import org.springframework.beans.factory.BeanNameAware;
 7 import org.springframework.beans.factory.DisposableBean;
 8 import org.springframework.beans.factory.InitializingBean;
 9 
10 public class Car implements BeanFactoryAware, BeanNameAware, InitializingBean,
11         DisposableBean {
12     private String brand;
13     private String color;
14     private int maxSpeed;
15     private String name;
16     private BeanFactory beanFactory;
17     private String beanName;
18 
19     public Car() {
20         System.out.println("調用Car()構造函數。");
21     }
22 
23     public String getBrand() {
24         return brand;
25     }
26 
27     public void setBrand(String brand) {
28         System.out.println("調用setBrand()設置屬性。");
29         this.brand = brand;
30     }
31 
32     public String getColor() {
33         return color;
34     }
35 
36     public String toString() {
37         return "brand:" + brand + "/color:" + color + "/maxSpeed:" + maxSpeed;
38     }
39 
40     public void setColor(String color) {
41         this.color = color;
42     }
43 
44     public int getMaxSpeed() {
45         return maxSpeed;
46     }
47 
48     public void setMaxSpeed(int maxSpeed) {
49         this.maxSpeed = maxSpeed;
50     }
51 
52     public void introduce() {
53         System.out.println("introduce:" + this.toString());
54     }
55 
56 
57     // BeanFactoryAware接口方法
58     public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
59         System.out.println("調用BeanFactoryAware.setBeanFactory()。");
60         this.beanFactory = beanFactory;
61     }
62 
63     // BeanNameAware接口方法
64     public void setBeanName(String beanName) {
65         System.out.println("調用BeanNameAware.setBeanName()。");
66         this.beanName = beanName;
67     }
68 
69     // InitializingBean接口方法
70     public void afterPropertiesSet() throws Exception {
71         System.out.println("調用InitializingBean.afterPropertiesSet()。");
72     }
73 
74     // DisposableBean接口方法
75     public void destroy() throws Exception {
76         System.out.println("調用DisposableBean.destory()。");
77     }
78 
79     public void myInit() {
80         System.out.println("調用myInit(),將maxSpeed設置爲240。");
81         this.maxSpeed = 240;
82     }
83 
84     public void myDestory() {
85         System.out.println("調用myDestroy()。");
86     }
87 
88 }

 

 

3.this

 1 package com.asm;
 2 
 3 import org.springframework.beans.factory.BeanFactory;
 4 import org.springframework.beans.factory.xml.XmlBeanFactory;
 5 import org.springframework.core.io.Resource;
 6 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
 7 import org.springframework.core.io.support.ResourcePatternResolver;
 8 
 9 public class BeanFactoryTest {
10     
11     public static void main(String[] args) {
12         
13         ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
14         Resource res = resolver.getResource("classpath:/applicationContext.xml");
15         
16         BeanFactory bf = new XmlBeanFactory(res);
17         System.out.println("init BeanFactory");
//初始化動做發生時,對於單例的bean來講,BeanFactory會緩存Bean實例。
18 Car car = bf.getBean("car",Car.class); 19 System.out.println("car bean is ready for user!"); 20 } 21 }

 

 

ApplicationContextspa

1.初始化:

若是配置文件在類路徑下,則能夠優先使用ClassPathXmlApplicationContext實現類

ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:com/asm/context/beans.xml");

 

若是配置文件放在文件系統下,則能夠使用FileSystemXmlApplicationContext

ApplicationContext ctx = new FileSystemXmlApplicationContext("file:com/asm/context/beans.xml");

 

2.Spring3.0 支持基於類註解的配置方式

 1 package com.asm;
 2 
 3 import org.springframework.context.annotation.Bean;
 4 import org.springframework.context.annotation.Configuration;
 5 
 6 @Configuration //表示一個配置信息提供類
 7 public class Beans {
 8 
 9     @Bean(name="car") //定義一個bean
10     public Car buildCar(){
11         Car car = new Car();
12         car.setBrand("奧迪");
13         car.setMaxSpeed(110);
14         return car;
15     }
16 }
1 public class AnnotationApplicationContext {
2     
3     public static void main(String[] args) {
4         
5         ApplicationContext ctx = new AnnotationConfigApplicationContext(Beans.class);
6         Car car = ctx.getBean("car",Car.class);
7     }
8 }
 1 package com.asm;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 5 
 6 public class AnnotationApplicationContext {
 7     
 8     public static void main(String[] args) {
 9         
10         ApplicationContext ctx = new AnnotationConfigApplicationContext(Beans.class);
11         Car car = ctx.getBean("car",Car.class);
12         System.out.println(car);
13     }
14 }

 

 

 

5.WebApplicationContext:

1.初始化:

在Web.xml中配置啓動Servlet或定義Web容器監聽器,藉助二者,完成Spring Web應用上下文的工做

Spring提供了啓動WebApplicationContext的Servlet和Web容器的監聽器

org.springframework.web.context.ContextLoaderServlet

org.springframework.web.context.ContextLoaderListener

 

ContextLoaderListener經過contextConfigLocation獲取Spring配置文件位置
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
 3   <display-name>SpringTest</display-name>
 4 
 5     <!-- 指定配置文件 -->
 6      <context-param>
 7          <param-name>contextConfigLocation</param-name>
 8          <param-value>
 9              /WEB-INF/viewspace-dao.xml,/WEB-INF/viewspace-service.xml
10          </param-value>
11      </context-param>
12      
13      <!-- 聲明Web容器的監聽器 -->
14      <listener>
15          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
16      </listener>
17 </web-app>

 

 

若是使用@Configuration的java類提供配置信息的配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 5     id="WebApp_ID" version="2.5">
 6     <display-name>SpringTest</display-name>
 7     
 8     <!-- 經過Context,讓CotextLoaderLister發現contextClass
 9     用AnnotationConfigWebApplicationContext啓動容器-->
10     <context-param>
11         <param-name>contextClass</param-name>
12         <param-value>
13              org.springframework.web.context.support.AnnotationConfigWebApplicationContext
14          </param-value>
15     </context-param>
16 
17     <!-- 指明@Configuration的配置類 -->    
18     <context-param>
19         <param-name>contextConfigLocation</param-name>
20         <param-value>
21             com.asm.AppConfig1,com.asm.AppConfig2
22          </param-value>
23     </context-param>
24     
25 
26     <!-- 聲明Web容器的監聽器 -->
27     <listener>
28         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
29     </listener>
30 </web-app>

 

 

classpath:/ 加載第一個包下查找

classpath*:/掃描全部

相關文章
相關標籤/搜索