当前位置: 代码迷 >> Java相关 >> java框架篇-spring 依赖注入
  详细解决方案

java框架篇-spring 依赖注入

热度:61   发布时间:2016-04-22 20:16:56.0
java框架篇---spring 依赖注入

spring依赖注入的方式有4种

  • 构造方法注入
  • 属性注入
  • 工厂注入
  • 注解注入

下面通过一个实例统一讲解:

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!");    }}

工厂类

第一个: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.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"       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();    }}

结果:

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

注解方式在后面详细介绍

依赖注入—自动装配

Spring中提供了自动装配依赖对象的机制,但是在实际应用中并不推荐使用自动装配,因为自动装配会产生未知情况,开发人员无法预见最终的装配结果。

自动装配是在配置文件中实现的,如下:

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

只需要配置一个autowire属性即可完成自动装配,不用再配置文件中写<property>,但是在类中还是要生成依赖对象的setter方法。

Autowire的属性值有如下几个:

· byType 按类型装配  可以根据属性类型,在容器中寻找该类型匹配的bean,如有多个,则会抛出异常,如果没有找到,则属性值为null;

· byName 按名称装配  可以根据属性的名称在容器中查询与该属性名称相同的bean,如果没有找到,则属性值为null;

· constructor 与byType方式相似,不同之处在与它应用于构造器参数,如果在容器中没有找到与构造器参数类型一致的bean,那么将抛出异常;

· autodetect 通过bean类的自省机制(introspection)来决定是使用constructor还是byType的方式进行自动装配。如果发现默认的构造器,那么将使用byType的方式。

  相关解决方案