其他分享
首页 > 其他分享> > Spring学习(一)

Spring学习(一)

作者:互联网

Spring体系结构

Spring开发步骤

业务层(service)

DAO层(DAO)

  1. 导入Spring基本包
  2. 编写DAO接口和Impl类
  3. 创建Spring核心配置文件
  4. 在Spring配置文件中配置Impl
  5. 使用SpringAPI获取Bean实例

例子

public interface UserDAO {
    public void save();
}

public class UserDaoImpl implements UserDAO {
    @Override
    public void save() {
        System.out.println("save running");
    }
}

///
<?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.xsd">
    <bean id="userDao" class = "zk.dao.impl.UserDaoImpl"></bean>

</beans>
///

package zk.demo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import zk.dao.UserDAO;

public class UserDemo {
    public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserDAO dao = (UserDAO) app.getBean("userDao");
        dao.save();
    }
}

Bean标签范围配置

scope

生命周期的配置

Bean实例化方式

Bean依赖注入

把业务层和持久层的依赖关系交给Spring去管理

普通数据注入

    <bean id="userDao" class = "zk.dao.impl.UserDaoImpl">
        <property name="name" value="zhangsan"></property>
        <property name="age" value="1"></property>
    </bean>

集合注入

    <bean id="userDao" class = "zk.dao.impl.UserDaoImpl">
        <property name="strList">
            <list>
                <value>aaa</value>
                <value>bbb</value>
                <value>ccc</value>
            </list>
        </property>
        <property name="stringUserMap">
            <map>
                <entry key="user1" value-ref="user1"/>
                <entry key="user2" value-ref="user2"/>
            </map>
        </property>
        <property name="properties">
            <props>
                <prop key="p1">ppp1</prop>
                <prop key="p2">ppp2</prop>
                <prop key="p3">ppp3</prop>
            </props>
        </property>
    </bean>

引入其他模块

<import resource="applicationContext-user.xml"/>
<import resource="applicationContext-product.xml"/>

标签:UserDAO,Spring,class,学习,Bean,save,public
来源: https://www.cnblogs.com/destinyzk/p/16663127.html