Spring框架——基于XML/注解开发
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/注解开发的更多相关文章
- Spring 框架的概述以及Spring中基于XML的IOC配置
Spring 框架的概述以及Spring中基于XML的IOC配置 一.简介 Spring的两大核心:IOC(DI)与AOP,IOC是反转控制,DI依赖注入 特点:轻量级.依赖注入.面向切面编程.容器. ...
- Spring中基于xml的AOP
1.Aop 全程是Aspect Oriented Programming 即面向切面编程,通过预编译方式和运行期动态代理实现程序功能的同一维护的一种技术.Aop是oop的延续,是软件开发中的 一个热点 ...
- 跟着刚哥学习Spring框架--通过XML方式配置Bean(三)
Spring配置Bean有两种形式(XML和注解) 今天我们学习通过XML方式配置Bean 1. Bean的配置方式 通过全类名(反射)的方式 √ id:标识容器中的bean.id唯一. √ cl ...
- 基于XML的开发
基于XML的开发 1.定义一个切面类 /** * Created by zejian on 2017/2/20.*/ public class MyAspectXML { public void be ...
- spring的基于xml的AOP配置案例和切入点表达式的一些写法
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- spring 框架的xml文件如何读取properties文件数据
spring 框架的xml文件如何读取properties文件数据 第一步:在spring配置文件中 注意:value可以多配置几个properties文件 <bean id="pro ...
- 10 Spring框架--基于注解和xml的配置的应用案例
1.项目结构 2.基于xml配置的项目 <1>账户的业务层接口及其实现类 IAccountService.java package lucky.service; import lucky. ...
- 10 Spring框架--基于注解的IOC配置
1.工程环境搭建 2.基于注解的IOC配置 IOC注解的分类 (1)用于创建对象的 他们的作用就和在XML配置文件中编写一个<bean>标签实现的功能是一样的@Component: 作用: ...
- Spring框架之使用JdbcTemplate开发Dao层程序
简介: JdbcTemplate开发dao层程序 由Spring框架给我们提供,Spring提供的很多操作数据源(关系型数据库,二维表格模型,有明确的行和列(mysql/orcal等) 非关系 ...
随机推荐
- 正则提取关键字符-python代码实现
原文地址:http://www.bugingcode.com/blog/python_re_extraction_key.html 关于python的正则使用在以前的文章中 http://www.bu ...
- 添砖加瓦:snappy无损压缩算法
一.简介 Snappy(旧称:Zippy)是Google基于LZ77的思路用C++语言编写的快速数据压缩与解压程序库,并在2011年开源.其目标并非最大压缩率或与其他压缩程序的兼容性,而是非常高的速度 ...
- 《英诗金库》I-46:Full Fathom Five, by W. Shakespeare
作品基本信息 作品名称:A Sea Dirge(海的挽歌) 作者:William Shakespeare(威廉·莎士比亚) 出版年代:1612 编注:此诗选自<暴风雨>第一幕第二场.标题& ...
- JasperReport框架使用教程(附带常见空白页问题说明)
概述与安装使用 1. PDF报表概述 概述 在企业级应用开发中,报表生成.报表打印下载是其重要的一个环节.在之前的课程中我们已经学习了报表中比较重要的一种:Excel报表.其实除了Excel报表之 ...
- 【桌面篇】Archlinux安装kde桌面
ArchLinux安装配置手册[桌面篇] 现在你的U盘可以拔掉了,重启后会发现和刚刚没什么区别,还是命令行的界面,别着急现在就带你安装桌面环境. 连接网络 首先检查一下网络是否连接成功 ping ww ...
- WordPress鼠标点击特效和粒子插件
鼠标特效 将下面代码复制到外观-主题编辑器-主题页脚(footer.php)代码<?php wp_footer(); ?>上方. <script type="text/ja ...
- mysql in与exists区别
1.exists是对外表做loop循环,每次loop循环再对内表(子查询)进行查询,那么因为对内表的查询使用的索引(内表效率高,故可用大表),而外表有多大都需要遍历,不可避免(尽量用小表),故内表大的 ...
- iOS下的 Fixed BUG
input 光标位置乱窜 固定式浮层内的输入框光标会发生偏移.即 fixed 定位的容器中输入框光标的位置显示不正确,没有正常地显示在输入框中,而是偏移到了输入框外面 可触发条件 页面body出现滚动 ...
- Mac笔记本使用小道解答集
如何设置Mac默认应用程序 https://www.jianshu.com/p/0f912e6c846c 苹果本安装微软雅黑 下载微软雅黑字体Mac版 解压.ttf 拖拽放入 我的电脑/资源库/fon ...
- LeetCode37 使用回溯算法实现解数独,详解剪枝优化
本文始发于个人公众号:TechFlow,原创不易,求个关注 数独是一个老少咸宜的益智游戏,一直有很多拥趸.但是有没有想过,数独游戏是怎么创造出来的呢?当然我们可以每一关都人工设置,但是显然这工作量非常 ...