java框架篇---spring IOC依賴注入

spring依賴注入的方式有4種java

  • 構造方法注入
  • 屬性注入
  • 工廠注入
  • 註解注入

下面經過一個實例統一講解:spring

User.java
package com.bjsxt.model;

public class User {
    private String username;
    private String password;
    public User(){}
    public User(String username, String password) {
        super();
        this.username = username;
        this.password = password;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String toString() {
        return "User [username=" + username + ", password=" + password + "]";
    }
}
UserDAO.java
package com.bjsxt.dao;
import com.bjsxt.model.User;


public interface UserDAO {
    public void save(User user);
}
UserDAO實現類UserDAOImpl
package com.bjsxt.dao.impl;

import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;


public class UserDAOImpl implements UserDAO {

    public void save(User user) {
        //Hibernate
        //JDBC
        //XML
        //NetWork
        System.out.println("user saved!");
    }

}

工廠類app

第一個:UserFactory
package com.bjsxt.factory;

import com.bjsxt.model.User;

public class UserFactory {
    public User CreatUser () {
        User user=new User();
        user.setUsername("周1");
        user.setPassword("oumyye");
        return user;
    }
}
第二個:UserFactory2 
package com.bjsxt.factory;

import com.bjsxt.model.User;

public class UserFactory2 {
    public static User CreatUser () {
        User user=new User();
        user.setUsername("偶my耶1");
        user.setPassword("oumyye");
        return user;
    }
}
控制層:UserService
package com.bjsxt.service;
import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;



public class UserService {
    
    private UserDAO userDAO;  
    
    public void init() {
        System.out.println("###########init");
    }
    
    public void save(User user) {
        userDAO.save(user);
    }
    public UserDAO getUserDAO() {
        return userDAO;
    }
    public void setUserDAO(UserDAO userDAO) {
        this.userDAO = userDAO;
    }
    
    public UserService(UserDAO userDAO) {
        super();
        this.userDAO = userDAO;
    }
    
    public void destroy() {
        System.out.println("destroy");
    }
}

配置文件:bean.xmlide

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    <!--        構造方法注入 -->
    <bean id="user" class="com.bjsxt.model.User" >
    <constructor-arg index="0" type="String" value="zhou"></constructor-arg>
    <constructor-arg index="1" type="String" value="password"></constructor-arg>
    </bean>
    
    <!-- 屬性注入 -->
    <bean id="user1" class="com.bjsxt.model.User" >
    <property name="username" value="zhang"></property>
    <property name="password" value="oumyye"></property>
    </bean>
    <!-- 工廠注入 -->
    <bean  id= "userFactory" class="com.bjsxt.factory.UserFactory" > </bean>
    <bean id="user2" factory-bean="userFactory" factory-method="CreatUser"></bean>
    
    <!-- 靜態工廠注入 -->
    <bean  id= "user3" class="com.bjsxt.factory.UserFactory2" factory-method="CreatUser"> </bean>
    
      <bean id="u" class="com.bjsxt.dao.impl.UserDAOImpl"></bean> 
    
      <bean id="userService" class="com.bjsxt.service.UserService" init-method="init" destroy-method="destroy" scope="prototype">
      
      <!-- <property name="userDAO" ref="u" /> -->
      
<!--        構造方法注入 -->
       <constructor-arg>
           <ref bean="u"/>
       </constructor-arg>
  </bean>
  

</beans>

測試類:測試

package com.bjsxt.service;

import static org.junit.Assert.*;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bjsxt.model.User;

public class UserServiceTest {

    @Test
    public void test() {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");
        UserService userService=(UserService) applicationContext.getBean("userService");
        //構造方法注入
        User user=(User) applicationContext.getBean("user");
        //屬性注入
        User user1=(User) applicationContext.getBean("user1");
        //工廠注入
        User user2=(User) applicationContext.getBean("user2");
        //工廠注入
        User user3=(User) applicationContext.getBean("user3");
        
        System.out.println(user);
        System.out.println(user1);
        System.out.println(user2);
        System.out.println(user3);
        userService.save(user);
        userService.destroy();
    }

}

結果:this

###########init
User [username=zhou, password=password]
User [username=zhang, password=oumyye]
User [username=周1, password=oumyye]
User [username=偶my耶1, password=oumyye]
user saved!
destroy

註解方式在後面詳細介紹spa

依賴注入—自動裝配

Spring中提供了自動裝配依賴對象的機制,可是在實際應用中並不推薦使用自動裝配,由於自動裝配會產生未知狀況,開發人員沒法預見最終的裝配結果。prototype

自動裝配是在配置文件中實現的,以下:code

<bean id="***" class="***" autowire="byType">xml

只須要配置一個autowire屬性便可完成自動裝配,不用再配置文件中寫<property>,可是在類中仍是要生成依賴對象的setter方法。

Autowire的屬性值有以下幾個:

· byType 按類型裝配  能夠根據屬性類型,在容器中尋找該類型匹配的bean,若有多個,則會拋出異常,若是沒有找到,則屬性值爲null;

· byName 按名稱裝配  能夠根據屬性的名稱在容器中查詢與該屬性名稱相同的bean,若是沒有找到,則屬性值爲null;

· constructor 與byType方式類似,不一樣之處在與它應用於構造器參數,若是在容器中沒有找到與構造器參數類型一致的bean,那麼將拋出異常;

· autodetect 經過bean類的自省機制(introspection)來決定是使用constructor仍是byType的方式進行自動裝配。若是發現默認的構造器,那麼將使用byType的方式。

相關文章
相關標籤/搜索