Spring配置文件

Spring配置文件

Bean实例化的三种方式

  • 无参构造方法实例化
  • 工厂静态方法实例化
  • 工厂实例方法实例化

无参构造方法实例化

Bean中需要有无参构造方法,没有则失败

<bean id="userDao" class="org.example.dao.impl.UserDaoImpl"></bean>

工厂静态方法实例化

工厂的静态方法返回Bean实例

<bean id="userDao" class="org.example.factory.BeanFactory" factory-method="getUserDao"></bean>
public class BeanFactory {

    public static UserDao getUserDao(){
        return new UserDaoImpl();
    }
}

工厂实例方法实例化

工厂的非静态方法返回Bean实例

<bean id="factory" class="org.example.factory.BeanFactory"></bean>
<bean id="userDao" factory-bean="factory" factory-method="getUserDao2"></bean>
public class BeanFactory {
    public UserDao getUserDao2(){
        return new UserDaoImpl();
    }
}

Spring的依赖注入方式

  • set方法注入
  • 构造方法注入

set方法注入

UserServiceImpl注入UserDao,其中,xml中的配置name="userDao"对应setUserDao方法中的UserDao,将UserDao中的U变成小写

<bean id="userService" class="org.example.service.impl.UserServiceImpl">
    <property name="userDao" ref="userDao"/>
</bean>
public class UserServiceImpl implements UserService {
    private UserDao userDao;

    // set方法注入
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
}

构造方法注入

xml中name="userDao"对应构造方法中形参userDao

<bean id="userDao" class="org.example.dao.impl.UserDaoImpl"></bean>

<bean id="userService" class="org.example.service.impl.UserServiceImpl">
    <constructor-arg name="userDao" ref="userDao"></constructor-arg>
</bean>
public class UserServiceImpl implements UserService {
    private UserDao userDao;

    public UserServiceImpl(UserDao userDao){
        this.userDao = userDao;
    }
}

Bean的依赖注入的数据类型

  • 普通数据类型
  • 引用数据类型
  • 集合数据类型

普通数据类型注入

<bean id="userDao" class="org.example.dao.impl.UserDaoImpl">
    <property name="name" value="zhangshan"></property>
</bean>
public class UserDaoImpl implements UserDao {
    private String name;

    public void setName(String name) {
        this.name = name;
    }
}

集合数据类型

集合数据类型(List)的注入

<bean id="userDao" class="org.example.dao.impl.UserDaoImpl">
    <property name="stringList">
        <list>
            <value>fdf</value>
            <value>dsfjdos</value>
            <value>fdjio</value>
        </list>
    </property>
</bean>
public class UserDaoImpl implements UserDao {

    private List<String> stringList;

    public void setStringList(List<String> stringList) {
        this.stringList = stringList;
    }
}

集合数据类型(Map<String, User>)的注入

<bean id="user1" class="org.example.domain.User">
    <property name="name" value="kangkang"></property>
    <property name="address" value="beijing"></property>
</bean>

<bean id="user2" class="org.example.domain.User">
    <property name="name" value="xiaohong"></property>
    <property name="address" value="shanghai"></property>
</bean>

<bean id="userDao" class="org.example.dao.impl.UserDaoImpl">
    <property name="userMap">
        <map>
            <entry key="k1" value-ref="user1"></entry>
            <entry key="k2" value-ref="user2"></entry>
        </map>
    </property>
</bean>
public class UserDaoImpl implements UserDao {

    private Map<String, User> userMap;

    public void setUserMap(Map<String, User> userMap) {
        this.userMap = userMap;
    }
}

集合数据类型(Properties)的注入

<bean id="userDao" class="org.example.dao.impl.UserDaoImpl">
    <property name="properties">
        <props>
            <prop key="aff">fdsf</prop>
            <prop key="fdfd">dsfaf</prop>
        </props>
    </property>
</bean>
public class UserDaoImpl implements UserDao {

    private Properties properties;

    public void setProperties(Properties properties) {
        this.properties = properties;
    }
}
posted @ 2022-04-20 22:56  Z_Y_L  阅读(41)  评论(0编辑  收藏  举报