1. 程式人生 > >Spring學習2--IOC應用

Spring學習2--IOC應用

Spring容器使用了依賴注入DI的方式實現了IOC控制

Setter注入方式

示例如下:
JDBCDataSource類封裝了連結資料庫的方法和一些必要引數,現在要通過setter注入方式把這些引數傳入JDBCDataSource類中。

package com.test.dao;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JDBCDataSource implements
Serializable{
private String driver; private String url; private String user; private String pwd; public String getDriver() { return driver; } public void setDriver(String driver) { try { //註冊資料庫驅動 Class.forName(driver); this
.driver = driver; } catch (ClassNotFoundException e) { e.printStackTrace(); } } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getUser() { return user; } public
void setUser(String user) { this.user = user; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public Connection getConnection() throws SQLException{ Connection conn = DriverManager.getConnection(url, user, pwd); return conn; } public void close(Connection conn){ if(conn != null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }

applicationContext.xml中的配置如下:

<?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:util="http://www.springframework.org/schema/util"  
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    <!-- setter注入 -->
    <bean id="dataSource" class="com.test.dao.JDBCDataSource">
        <property name="driver" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost/test?characterEncoding=UTF8&amp;zeroDateTimeBehavior=convertToNull"></property>
        <property name="user" value="root"></property>
        <property name="pwd" value="123"></property>
    </bean>
</beans>

這樣,就可以通過spring的setter注入方式將引數傳入該類中了。

構造器注入方式

示例:
MysqlUserDao類中的findByName方法,從資料庫中查詢物件,需要依賴於JDBCDataSource的屬性,利用JDBCDataSource 獲得資料庫的連線,進行查詢。

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.test.entity.User;
public class MysqlUserDao implements UserDao{
    private JDBCDataSource dataSource;
    //建立JDBCDataSource物件必須依賴於JDBCDataSource例項,進行構造器注入!
    public MysqlUserDao(JDBCDataSource dataSource){
        this.dataSource = dataSource;
    }
    public User findByName(String name) {
        System.out.println("利用JDBC技術查詢資訊");
        String sql = "select * from Users where name=?";
        Connection conn = null;
        try {
            conn = dataSource.getConnection();
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, name);
            ResultSet rs = ps.executeQuery();
            User user = null;
            while(rs.next()){
                user = new User();
                user.setId(rs.getInt("id"));
                user.setName(rs.getString("name"));
                user.setPwd(rs.getString("pwd"));
                user.setPhone(rs.getString("phone"));
            }
            rs.close();
            ps.close();
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            dataSource.close(conn);
        }
        return null;
    }
}

applicationContext.xml的檔案中新增配置如下:

    <!-- setter注入 -->
    <bean id="dataSource" class="com.test.dao.JDBCDataSource">
        <property name="driver" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost/test?characterEncoding=UTF8&amp;zeroDateTimeBehavior=convertToNull"></property>
        <property name="user" value="root"></property>
        <property name="pwd" value="123"></property>
    </bean>
    <!-- 構造器注入 -->
    <bean id="userDao" class="com.test.dao.MysqlUserDao">
        <!-- 利用構造器的引數注入bean的屬性 -->
        <constructor-arg index="0" ref="dataSource"></constructor-arg>
    </bean>

以上配置完成了dataSource的構造器注入。