IoC的实现方式有两种:XML配置文件、基于注解。

MVC开发模式:

  Controller层

  Service层

  Repository层

  

  Controller层调用Service,Service调用Repository

基于XML配置文件方式

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.xsd">

    <bean id="userController" class="com.sunjian.controller.UserController">
        <property name="userService" ref="userService"></property>
    </bean>

    <bean id="userService" class="com.sunjian.service.impl.UserServiceImpl">
        <property name="userRepository" ref="userRepository"></property>
    </bean>

    <bean id="userRepository" class="com.sunjian.repository.impl.UserRepositoryImpl"></bean>
</beans>

entity

package com.sunjian.entity;

public class User2 {
    private Integer id;
    private String name;

    public User2(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public User2(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "User2{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

repository

package com.sunjian.repository;

import com.sunjian.entity.User2;

/**
 * @author sunjian
 * @date 2020/3/14 15:44
 */
public interface UserRepository {
    public User2 findUserById(Integer id);
}

repositoryImpl

package com.sunjian.repository.impl;

import com.sunjian.entity.User2;
import com.sunjian.repository.UserRepository;

import java.util.HashMap;
import java.util.Map;

/**
 * @author sunjian
 * @date 2020/3/14 15:45
 */
public class UserRepositoryImpl implements UserRepository {
    private static Map<Integer, User2> userMap;

    static {
        userMap = new HashMap<Integer, User2>();
        userMap.put(1, new User2(1, "张三"));
        userMap.put(2, new User2(2, "李四"));
    }

    public User2 findUserById(Integer id) {
        return userMap.get(id);
    }
}

service

package com.sunjian.service;

import com.sunjian.entity.User2;

/**
 * @author sunjian
 * @date 2020/3/14 15:36
 */
public interface UserService {
    User2 findUserBuId(Integer id);
}

serviceImpl

package com.sunjian.service.impl;

import com.sunjian.entity.User2;
import com.sunjian.repository.UserRepository;
import com.sunjian.repository.impl.UserRepositoryImpl;
import com.sunjian.service.UserService;

/**
 * @author sunjian
 * @date 2020/3/14 15:38
 */
public class UserServiceImpl implements UserService {
    private UserRepository userRepository;

    public void setUserRepository(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public User2 findUserBuId(Integer id) {
        return userRepository.findUserById(id);
    }

    public void setUserRepository(UserRepositoryImpl userRepository) {
    }
}

controller

package com.sunjian.controller;

import com.sunjian.entity.User2;
import com.sunjian.service.UserService;
import com.sunjian.service.impl.UserServiceImpl;

/**
 * @author sunjian
 * @date 2020/3/14 15:56
 */
public class UserController {

    private UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public User2 findUserById(Integer id){
        return userService.findUserBuId(id);
    }
}

test class

package com.sunjian.test;

import com.sunjian.controller.UserController;
import com.sunjian.entity.User2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author sunjian
 * @date 2020/3/14 16:03
 */
public class Test4 {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring2.xml");
        UserController userController = (UserController) applicationContext.getBean("userController");
        User2 user = userController.findUserById(2);
        System.out.println(user);
    }
}
User2{id=2, name='李四'}

基于注解方式

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 将类扫描到IoC容器中 -->
    <context:component-scan base-package="com.sunjian"></context:component-scan>

</beans>

entity

package com.sunjian.entity;

public class User2 {
    private Integer id;
    private String name;

    public User2(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public User2(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "User2{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

repository

package com.sunjian.repository;

import com.sunjian.entity.User2;

/**
 * @author sunjian
 * @date 2020/3/14 15:44
 */
public interface UserRepository {
    public User2 findUserById(Integer id);
}

repositoryImpl

package com.sunjian.repository.impl;

import com.sunjian.entity.User2;
import com.sunjian.repository.UserRepository;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;

/**
 * @author sunjian
 * @date 2020/3/14 15:45
 */
@Repository
public class UserRepositoryImpl implements UserRepository {

    private static Map<Integer, User2> userMap;

    static {
        userMap = new HashMap<Integer, User2>();
        userMap.put(1, new User2(1, "张三"));
        userMap.put(2, new User2(2, "李四"));
    }

    public User2 findUserById(Integer id) {
        return userMap.get(id);
    }
}

service

package com.sunjian.service;

import com.sunjian.entity.User2;

/**
 * @author sunjian
 * @date 2020/3/14 15:36
 */
public interface UserService {
    User2 findUserBuId(Integer id);
}

serviceImpl

package com.sunjian.service.impl;

import com.sunjian.entity.User2;
import com.sunjian.repository.UserRepository;
import com.sunjian.repository.impl.UserRepositoryImpl;
import com.sunjian.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author sunjian
 * @date 2020/3/14 15:38
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserRepository userRepository;

    public void setUserRepository(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public User2 findUserBuId(Integer id) {
        return userRepository.findUserById(id);
    }

    public void setUserRepository(UserRepositoryImpl userRepository) {
    }
}

controller

package com.sunjian.controller;

import com.sunjian.entity.User2;
import com.sunjian.service.UserService;
import com.sunjian.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * @author sunjian
 * @date 2020/3/14 15:56
 */
@Controller
public class UserController {
    @Autowired
    private UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public User2 findUserById(Integer id){
        return userService.findUserBuId(id);
    }
}

test class

package com.sunjian.test;

import com.sunjian.controller.UserController;
import com.sunjian.entity.User2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author sunjian
 * @date 2020/3/14 16:24
 */
public class Test5 {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring3.xml");
        UserController userController = (UserController)applicationContext.getBean("userController");
        User2 user = userController.findUserById(1);
        System.out.println(user);
    }
}
User2{id=1, name='张三'}

OK.

Spring框架——基于XML/注解开发的更多相关文章

  1. Spring 框架的概述以及Spring中基于XML的IOC配置

    Spring 框架的概述以及Spring中基于XML的IOC配置 一.简介 Spring的两大核心:IOC(DI)与AOP,IOC是反转控制,DI依赖注入 特点:轻量级.依赖注入.面向切面编程.容器. ...

  2. Spring中基于xml的AOP

    1.Aop 全程是Aspect Oriented Programming 即面向切面编程,通过预编译方式和运行期动态代理实现程序功能的同一维护的一种技术.Aop是oop的延续,是软件开发中的 一个热点 ...

  3. 跟着刚哥学习Spring框架--通过XML方式配置Bean(三)

    Spring配置Bean有两种形式(XML和注解) 今天我们学习通过XML方式配置Bean 1. Bean的配置方式 通过全类名(反射)的方式   √ id:标识容器中的bean.id唯一. √ cl ...

  4. 基于XML的开发

    基于XML的开发 1.定义一个切面类 /** * Created by zejian on 2017/2/20.*/ public class MyAspectXML { public void be ...

  5. spring的基于xml的AOP配置案例和切入点表达式的一些写法

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  6. spring 框架的xml文件如何读取properties文件数据

    spring 框架的xml文件如何读取properties文件数据 第一步:在spring配置文件中 注意:value可以多配置几个properties文件 <bean id="pro ...

  7. 10 Spring框架--基于注解和xml的配置的应用案例

    1.项目结构 2.基于xml配置的项目 <1>账户的业务层接口及其实现类 IAccountService.java package lucky.service; import lucky. ...

  8. 10 Spring框架--基于注解的IOC配置

    1.工程环境搭建 2.基于注解的IOC配置 IOC注解的分类 (1)用于创建对象的 他们的作用就和在XML配置文件中编写一个<bean>标签实现的功能是一样的@Component: 作用: ...

  9. Spring框架之使用JdbcTemplate开发Dao层程序

    简介: JdbcTemplate开发dao层程序     由Spring框架给我们提供,Spring提供的很多操作数据源(关系型数据库,二维表格模型,有明确的行和列(mysql/orcal等) 非关系 ...

随机推荐

  1. JAVA递归、非递归遍历二叉树

    前序遍历:1.访问根节点 2.前序遍历左子树 3.前序遍历右子树 中序遍历:1.中序遍历左子树 2.访问根节点 3.中序遍历右子树 后序遍历:1.后序遍历左子树 2.后序遍历右子树 3.访问根节点-- ...

  2. 自定义一个简单的SegmentedControl

    先大概看下我们想实现简单的效果 源码 // // DSegmentedControl.swift // IOS学习之自定义UISegmentedControl // // Created by din ...

  3. Proto3:Arena分配指南

    Arena分配是仅C++有的功能,在使用Protocol Buffer时,它可以帮助你优化你的内存使用,提高性能.在.proto文件中启用Arena分配会在生成的C++代码中添加处理Arena分配的额 ...

  4. 一月七笔千万美元投资!国内VR行业在刮什么风?

    虽然直到现在仍然没有一款真正能够彻底普及并改变大众操控方式的虚拟现实设备出现,但其已经被认定是未来人类社会中不可或缺的重要组成部分和工作.生活.娱乐.休闲载体.而虚拟现实设备.内容在今年年初CES展会 ...

  5. 强制迁移、合区 APP太强势伤害用户同时是否违法?

    APP太强势伤害用户同时是否违法?" title="强制迁移.合区 APP太强势伤害用户同时是否违法?"> 对于经常混迹在国内各大手游的玩家来说,"合区& ...

  6. IP 多播

    IP 多播 一.IP 多播的基本概念 1.1.简介 不使用多播时需要发送 90 次单播: 使用多播时只需要发送 1 次多播: 1.2.IP 多播的一些特点 多播使用组地址:D 类IP地址支持多播.多播 ...

  7. Git 程序员篇

    关于 Git Git 背后的故事 伟大的作品总是诞生于伟大的时代,正如 Git 同样诞生于一个英雄辈出.极富纷争的年代. 2005 年,Linux 内核开发社区正面临严峻的挑战:他们不能继续使用 Bi ...

  8. 达拉草201771010105《面向对象程序设计(java)》第四周学习总结

    实验四类与对象的定义及使用 实验时间 2018-9-20 第一部分:理论知识 1.类与对象概念 (1)类是具有相同属性和方法的一类事物的抽象,是构造对象的模板或蓝图,由类构造对象的过程称为创建类的实例 ...

  9. Lambda表达式(JDK8)

    在说Lambda表达式之前,先介绍一下函数式接口 函数式接口 就是只定义了一个抽象方法的接口,我们可以使用注解@Functionallnterface,来强约束这种接口为函数式接口.如Runnable ...

  10. grid实战之微信钱包 腾讯服务界面

    网格布局简介 CSS3网格布局是让开发人员设计一个网格并将内容放在这些网格内.而不是使用浮动制作一个网格,实际上是你将一个元素声明为一个网格容器,并把元素内容置于网格中. 有一些浏览器是不支持网格布局 ...