Spring框架的學習(第二天)

xml文件導入其他xml文件配置

如果我們在spring框架中配置了多個xml文件,我們可以在讀取配置文件的時候把這些xml文件一下全都讀取,也可以只讀一個總的xml文件,在這個總的xml文件中把其他的xml全都都導入進來。
例如:

student.xml文件:

<bean name="student" class="com.briup.bean.Student">
    <property name="id">
        <value>25</value>
    </property>
</bean>

teacher.xml文件:

<bean name="teacher" class="com.briup.bean.Teacher">
        <property name="student" ref="student"></property>//ref=""手動注入哪個bean
</bean>

import.xml文件:

<import resource="teacher.xml"/>
<import resource="student.xml"/>

main:

String[] path = {"com/briup/ioc/imp/import.xml"};
ApplicationContext container = new ClassPathXmlApplicationContext(path);

Teacher t = (Teacher) container.getBean("teacher");
System.out.println(t.getStudent());

創建bean實例的方式

xml文件中有bean的配置,而且這個bean所對應的java類中存在一個無參構造器,那麼這個時候spring容器就可以使用反射調用無參構造器來創建實例了

通過工廠類獲得實例(工廠類實現了接口FactoryBean<?>)

注意spring中的PropertyPlaceholderConfigurer類的使用,在htmlsingle中直接搜索類名即可

例如:
工廠類實現指定接口並且實現接口中的三個抽象方法:

    public class ConnectionFactory implements FactoryBean<Connection>{
    private String driver;
    private String url;
    private String username;
    private String password;

    @Override
    public Connection getObject() throws Exception {
        Class.forName(driver);
        Connection conn = 
            DriverManager.getConnection(url,username,password);
        return conn;
    }

    @Override
    public boolean isSingleton() {//判斷它是否是單例
        // TODO Auto-generated method stub
        return false;
    }
    
    @Override
    public Class<Connection> getObjectType() {
        // TODO Auto-generated method stub
        return Connection.class;
    }
    set/get
    ....
  }

xml文件:

因爲這個類是一個工廠類,所以我們用名字conn在容器中拿對象的時候,拿到並不是這個工廠類對象,而是這個工廠類對象調用完工廠方法後所返回的對象.

  <bean name="conn" class="com.briup.ioc.factory.ConnectionFactory">
    <property name="driver">
        <value>${driver}</value>//從一個配置文件中以key—value的形式拿value
    </property>
    
    <property name="url">
        <value>${url}</value>
    </property>
    
    <property name="username">
        <value>${username}</value>
    </property>
    
    <property name="password">
        <value>${password}</value>
    </property>
  </bean>
  
  <!-- 
    下面配置的這個類,可以自動的幫我們去讀取指定的properties文件的
    內容,文件中用key-value的形式存放數據,讀完之後我們就可以用
    ${key}這種形式去拿文件中的value值了。
    classpath指的是從src下面找.
  -->
  <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>classpath:oracle.properties</value>
    </property>
  </bean>

main:

String path = "com/briup/ioc/factory/factory.xml";
ApplicationContext container = 
        new ClassPathXmlApplicationContext(path);
Connection conn = (Connection) container.getBean("conn");
System.out.println(conn);

通過實例工廠獲得實例(不需要實現或者繼承任何接口或者父類)

注意spring中的PropertyPlaceholderConfigurer類的使用,在htmlsingle中直接搜索類名即可

例如:一個普通的工程類

  public class ConnectionFactory{
    private String driver;
    private String url;
    private String username;
    private String password;
    
    public Object getConnection() throws Exception {
        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url,username,password);
        return conn;
    }
    get/set
    ....
  }

  xml文件:
 <bean name="factory" class="com.briup.ioc.instanceFactory.ConnectionFactory">
    <property name="driver">
        <value>${driver}</value>
    </property>
    
    <property name="url">
        <value>${url}</value>
    </property>
    
    <property name="username">
        <value>${username}</value>
    </property>
    
    <property name="password">
        <value>${password}</value>
    </property>
 </bean>
    
 <!-- 
    將來通過這個conn來拿對象,拿到的是名字爲factory的工廠類調用完
    名字爲getConnection方法之後所返回的對象。
 -->
 <bean name="conn" factory-bean="factory" factory-method="getConnection"></bean>
 
 <!-- 讀取properties文件 -->
 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>classpath:oracle.properties</value>
    </property>
 </bean>

main:

String path = "com/briup/ioc/instanceFactory/instanceFactory.xml";
ApplicationContext container = 
    new ClassPathXmlApplicationContext(path);
Connection conn = (Connection) container.getBean("conn");
System.out.println(conn);

通過靜態工廠獲得實例

例如:含義靜態方法的工廠類

  public class ConnectionFactory{
    private static String driver = "oracle.jdbc.driver.OracleDriver";
    private static String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
    private static String username = "briup";
    private static String password = "briup";
    
    public static Object getConnection() throws Exception {
        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url,username,password);
        return conn;
    }
  }

  xml文件:
  <!-- 這樣配置一定要求getConnection方法是靜態方法 -->
  <bean name="conn" class="com.briup.ioc.staticFactory.ConnectionFactory" factory-method="getConnection"></bean>

main:

String path = "com/briup/ioc/staticFactory/staticFactory.xml";
ApplicationContext container = new ClassPathXmlApplicationContext(path);
Connection conn = (Connection) container.getBean("conn");
System.out.println(conn);

自定義屬性編輯器 PropertyEditor

Spring中我們可以使用屬性編輯器來將特定的字符串轉換爲對象 :String-->object java.beans.PropertyEditor(JDK中的接口)用於將xml文件中字符串轉換爲特定的類型
同時JDK爲我們提供一個實現類java.beans.PropertyEditorSupport
Spring在注入時,如果遇到類型不一致(例如需要Address類型但是用戶傳了個String)則會去調用相應的屬性編輯器進行轉換.
spring會調用屬性編輯器的setAsText(String str)進行處理用戶傳的字符串,並調用getValue()方法獲取處理後得到的對象,所以我們在代碼中處理完後記得調用setValue方法,要不然spring調用getValue方法拿不到你處理後的對象

自定義屬性編輯器示例

  1. //自定義編輯器類

     public class AddressEditor extends PropertyEditorSupport {
     @Override
     public String getAsText() {
         return super.getAsText();
     }
    
  2. //Spring遇到數據類型不一致並且不能自己處理的時候會調用這個方法處理字符串

     @Override
     public void setAsText(String text) throws IllegalArgumentException {
         String[] str = text.split(",");
         String city = str[0];
         String street = str[1];
         String country = str[2];
         Address add = new Address(city, street, country);
         setValue(add);
     }
    
     }
     //Address類
     public class Address {
     private String city;
     private String street;
     private String country;
     set/get
     .....
     }
    
     //Student類
     public class Student {
     private long id;
     private String name;
     private boolean gender;
     private int age;
     private Address address;
     get/set
     ...
     }
    

xml文件:

    <!-- 這個配置指明哪個類型對應哪個自定義編輯器 -->
    <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
    <property name="customEditors">
     <map>
    <entry key="com.briup.ioc.proEdit.Address"  value="com.briup.ioc.proEdit.AddressEditor"/>
</map>
    </property>
    </bean>
 <!-- spring發現address的值不能注入的時候(類型不對),就會調用對應的屬性編輯器處理了 -->
  <bean id="student" class="com.briup.ioc.proEdit.Student">
    <property name="id" value="1"/>
    <property name="name" value="tom"/>
    <property name="age" value="45"/>
    <property name="gender" value="true"/>
    <property name="address">
        <value>kunshan,xueyuan,China</value>
    </property>
 </bean>

自定義事件

在spring中我們可以自定義事件,並且可以使用ApplicationContext類型對象(就是spring容器container)來發布這個事件,事件發佈之後,所有的ApplicaitonListener(監聽器)實例都會被觸發並調用指定方法onApplicationEvent()來處理.

例如:
自定義事件類RainEvent:

public class RainEvent extends ApplicationEvent {
public RainEvent(Object source) {
    super(source);
}
}

監聽器類RainListener1

public class RainListener1 implements ApplicationListener {
//這裏需要加類型判斷,如果不加類型判斷的話只要是事件發生都會執行監聽器類這個
//方法,所以我們必須加上類型判斷,當屬於我們自定義的那個類型的時候我們才進行處理。
public void onApplicationEvent(ApplicationEvent event) {
    if (event instanceof RainEvent) {
        System.out.println("唐僧大喊:" + event.getSource() + "趕快收衣服嘍!");
    }
 }

}
監聽器類RainListener2

public class RainListener2 implements ApplicationListener {
public void onApplicationEvent(ApplicationEvent event) {
    if (event instanceof RainEvent) {
        System.out.println("我們:" + event.getSource() + "太好了不用上課了!");
    }
 }

}

xml文件:

<!-- 只需要把這倆個監聽器類交給spring容器管理就可以了 -->
<bean class="com.briup.ioc.event.RainListener1"></bean>
<bean class="com.briup.ioc.event.RainListener2"></bean>

main:

String path = "com/briup/ioc/event/event.xml";
ApplicationContext container = new ClassPathXmlApplicationContext(path);
//觸發條件
container.publishEvent(new RainEvent("下雨了!"));

ioc中的annotation配置

  1. @Autowired

  2. @Autowired默認按照byType匹配的方式進行注入,如果沒有一個bean的類型是匹配的則會拋異常,
    如果有多個bean的類型都匹配成功了,那麼再按byName方式進行選擇

  3. @Autowired註解可以寫在成員變量、set/ger方法、構造器函數上面

  4. @Autowired如果最終匹配不成功(注意一定是一個都沒有找到的情況)則會拋出異常,
    但是如果設置爲@Autowired(required=false),則最終匹配不成功沒有不會拋出異常。

  5. @Autowired可以結合 @Qualifier("beanName")來使用,則可以達到byName的效果

  6. @Autowired使用後需要在xml文件加入以下配置才能生效:
    <context:annotation-config/>(高版本的就不需要加 )

  7. @Resource

  8. @Resource的作用和 @Autowired差不多,只不過@Resource是默認先用byname,
    如果找不到合適的就再用bytype來注入

  9. Resource有倆個屬性,name和type,使用name屬性則表示要byName匹配,使用type屬性則表示要byType匹配

  10. @Resource使用後需要在xml文件加入以下配置才能生效:
    <context:annotation-config/>

  11. @PostConstruct 和 @PreDestroy

單例的類是有ApplicationContext管理。所以當ac.destory()時,類也就銷燬了。

  1. 標註了 @PostConstruct 註釋的方法將在類實例化後調用。
  2. 標註了 @PreDestroy 的方法將在類銷燬之前調用。
  1. @Component

這個註釋相當於這樣的內容。<bean name="student" class="com.zts.ioctest.Student" scope="prototype">

  1. @Component註解可以直接定義bean,而無需在xml定義。但是若兩種定義同時存在,xml中的定義會覆蓋類中註解的Bean定義。
  2. @Component註解直接寫在類上面即可
  3. @Component有一個可選的參數,用於指定 bean 的名稱:@Component("boss")
  4. @Component容易不指定參數,則 bean 的名稱爲當前類的類名小寫
  5. @Component使用之後需要在xml文件配置一個標籤: <context:component-scan/>
  6. <context:component-scan base-package="com.briup.ioc.annotation" /> 可以表示spring需要檢查哪個包下的java類,看它們是否使用了 @Component註解
  7. @Component定義的bean默認情況下都是單例模式的,如果要讓這個bean變爲非單例,可以再結合這個 @Scope 註解來達到目標 @Scope("prototype")

@Component是Spring中所有bean組件的通用形式, @Repository @Service @Controller 則是 @Component的細化,用來表示更具體的用例,分別對應了持久化層、服務層和表現層。但是至少到現在爲止這個四種註解的實質區別很小(甚至幾乎沒有),都是把當前類註冊爲spring容器中的一個bean

注意:
component-scan標籤默認情況下自動掃描指定路徑下的包(含所有子包),將帶有 @Component @Repository @Service @Controller標籤的類自動註冊到spring容器。
對標記了 Spring中的 @Required @Autowired @PostConstruct @PreDestroy @Resource @WebServiceRef @EJB @PersistenceContext @PersistenceUnit等註解的類進行對應的操作使註解生效(包含了annotation-config標籤的作用)。

Spring框架的學習第一天
Spring框架的學習第三天

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章