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. LG_2286_[HNOI2004]宠物收养场

    题目描述 凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的一个特殊的公式,得出该领 ...

  2. 吴裕雄--天生自然HTML学习笔记:启动TOMCAT服务器时出现乱码解决方法

  3. jQuery 源码学习 - 02 - jQuery.fn.extend 与 jQuery.extend

    参考资料:[深入浅出jQuery]源码浅析--整体架构,备用地址:chokcoco/jQuery-. extend 方法在 jQuery 中是一个很重要的方法.jQuery 内部用它来拓展静态方法或者 ...

  4. 8.2.2 使用Java8增强的Iterator遍历集合元素

    8.2.2 使用Java 8增强的Iterator遍历集合元素 Iterator接口方法 程序示例 Iterator仅用于遍历集合 Iterator必须依附于Collection对象 修改迭代变量的值 ...

  5. echarts饼图字体大小修改

    const option = { tooltip: { trigger: 'item', formatter: "{a} {b}: {c} ({d}%)" }, series: [ ...

  6. Linux sed命令实例解析

    最近看project的makefile,又见到了sed的强大编辑能力,在makefile工作之前,通常都是执行脚本或者make menuconfig来配置好各种全局变量.sed活动阶段通常在bash ...

  7. 5G时代,会有什么奇葩事儿?

    ​ 在3GPP RAN第187次会议关于5G短码方案的讨论中,中国华为推荐的PolarCode方案获得认可,成为5G控制信道eMBB场景编码的最终解决方案.坦白讲,笔者在读这个新闻的时候,手里备着一本 ...

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

    达拉草201771010105<面向对象程序设计(java)>第十二周学习总结 第一部分:理论知识 Java的抽象窗口工具箱(AbstractWindow Toolkit,AWT)包含在j ...

  9. 优秀的Spring Cloud开源项目整理推荐

    无论是对于初学者,还是有一定工作经验的程序员来讲,Spring Cloud开源项目都是一笔宝贵的财富.下面给大家整理了十个优秀的开源项目,分别是spring-cloud-examples.spaasc ...

  10. 从头认识js-js中的对象

    什么是对象? ECMA-262中把对象定义为:“无序属性的集合,其属性可以包含基本值,对象或者函数”.严格来讲,对象是一组没有特定顺序的值.对象的每个属性或方法·都有一个名字,而每个名字都映射到一个值 ...