Spring基于的注解自动装配和依赖注入(***)
#自动装配的小Demo: package com.gyf.annotation;
//DAO层
public interface UserDao {
public void save();
}
package com.gyf.annotation; import org.springframework.stereotype.Repository; @Repository("userDao")
public class UserDaoImpl implements UserDao {
@Override
public void save() {
System.out.println("UserDao接口的实现类的save方法的实现...");
}
} //业务层
package com.gyf.annotation; public interface UserService {
void save();
} package com.gyf.annotation; import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service("userService")
public class UserServiceImpl implements UserService {
@Resource(name = "userDao")
private UserDao userDao;
@Override
public void save() {
this.userDao.save();
System.out.println("userservice...save...");
}
} //控制层
package com.gyf.annotation;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;
@Controller("userController")
public class UserController {
@Resource(name="userService")
private UserService userService;
public void save(){
this.userService.save();
System.out.println("userController...save...");
}
}
说明:
无论是servlet、JSP,还是ssm都是用的mvc的设计思想;
Dao层与数据库相关联,业务层并调用DAO层的变量,并进行中间逻辑处理,控制层
对业务层进行调用,并进行进一步的逻辑控制,包括字符编码的设置等。
#配置文件:
<?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
http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.gyf.annotation"/>
</beans>
package com.gyf.test; import com.gyf.annotation.UserController;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class test {
public static void main(String[] args) {
String xmlPath="beans6.xml";
ApplicationContext ac=new ClassPathXmlApplicationContext(xmlPath);
UserController userController=(UserController)ac.getBean("userController");
userController.save();
}
}
----------------------------------------------------------------------------------------------------------------------------------------------
#依赖注入 //DAO层
package com.gyf.ioc; public interface UserDao {
void say();
} package com.gyf.ioc; public class UserDaoImpl implements UserDao { @Override
public void say() {
System.out.println("userDao say hello world");
} }
//业务层
package com.gyf.ioc; public interface UserService {
void say();
} package com.gyf.ioc; public class UserServiceImpl implements UserService {
private UserDao userDao; public void setUserDao(UserDao userDao) {
this.userDao = userDao;
} @Override
public void say() {
this.userDao.say();
System.out.println("userService say hello world");
}
}
#配置文件<?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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="userDao" class="com.gyf.ioc.UserDaoImpl"/> <bean id="userService" class="com.gyf.ioc.UserServiceImpl">
<!--将DAO层的数据注入到业务层,说明该方法是set方法进行注入到业务层组件中-->
<property name="userDao" ref="userDao"/>
</bean> </beans>
#测试:
package com.gyf.test; import com.gyf.ioc.UserDao;
import com.gyf.ioc.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class test {
@Test
public void test1(){
ApplicationContext ac=new ClassPathXmlApplicationContext("beansIOC.xml"); UserDao userDao=(UserDao)ac.getBean("userDao");
userDao.say();
UserService userService=(UserService)ac.getBean("userService");
userService.say();
}
}
学会重复!
多练习,Spring 基础很重要!
这个有点类似于多态的运用,以及变量的调用;
Spring基于的注解自动装配和依赖注入(***)的更多相关文章
- Spring的自动装配与依赖注入
Spring的自动装配与依赖注入 装配 = 创建Bean + 注入Bean 创建Bean 自动发现 显式注册Bean 注入Bean 基于配置的注入 自动注入 Spring的装配分为显式装配和隐式装配, ...
- Spring学习(六)-----Spring使用@Autowired注解自动装配
Spring使用@Autowired注解自动装配 在上一篇 Spring学习(三)-----Spring自动装配Beans示例中,它会匹配当前Spring容器任何bean的属性自动装配.在大多数情况下 ...
- Spring自动装配之依赖注入(DI)
依赖注入发生的时间 当Spring IOC 容器完成了Bean 定义资源的定位.载入和解析注册以后,IOC 容器中已经管理类Bean定义的相关数据,但是此时IOC 容器还没有对所管理的Bean 进行依 ...
- [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring4笔记5--基于注解的DI(依赖注入)
基于注解的DI(依赖注入): 对于 DI 使用注解,将不再需要在 Spring 配置文件中声明 Bean 实例.只需要在 Spring 配置文件中配置组件扫描器,用于在指定的基本包中扫描注解. < ...
- Spring升级案例之IOC介绍和依赖注入
Spring升级案例之IOC介绍和依赖注入 一.IOC的概念和作用 1.什么是IOC 控制反转(Inversion of Control, IoC)是一种设计思想,在Java中就是将设计好的对象交给容 ...
- 三大框架 之 Spring(IOC控制反转、DI依赖注入)
目录 常用词汇 left join与left outer join的区别 Struts2的标签库导入 Spring Spring概述 什么是Spring spring特点 下载 IOC 什么IOC 传 ...
- Spring IOC源代码具体解释之容器依赖注入
Spring IOC源代码具体解释之容器依赖注入 上一篇博客中介绍了IOC容器的初始化.通过源代码分析大致了解了IOC容器初始化的一些知识.先简单回想下上篇的内容 加载bean定义文件的过程.这个过程 ...
- Spring详解(三)------DI依赖注入
上一篇博客我们主要讲解了IOC控制反转,也就是说IOC 让程序员不在关注怎么去创建对象,而是关注与对象创建之后的操作,把对象的创建.初始化.销毁等工作交给spring容器来做.那么创建对象的时候,有可 ...
随机推荐
- HTML5-CSS3-JavaScript(1)
之前大致总结过HTML5的发展. 这里贴出之前的随笔:http://www.cnblogs.com/jiangxiaobo/p/5199924.html 我们就从HTML5的基础总结起.希望可以提高自 ...
- 281A
#include <iostream> #include <string> #include <cctype> using namespace std; int m ...
- Centos7上安装及配置Apache
Apache HTTP服务器是世界上最流行的Web服务器. 它是一款免费的开源和跨平台的HTTP服务器,提供强大的功能,可以通过各种模块进行扩展. 以下说明介绍如何在CentOS 7机器上安装和管理A ...
- js图的数据结构处理---弗洛伊德算法
function Graph() { this.graph = [ [0, 2, 4, 0, 0, 0], [0, 0, 1, 4, 2, 0], [0, 0, 0, 0, 3, 0], [0, 0, ...
- Makefile中变量定义中末尾不能有空格
我在Makefile中添加了 ifndef EMASSDIR EMASSDIR=$(shell emassTop.py)endif 但是emassTop.py)后面不小心加入了空格,造成出现“Make ...
- Response.Redirect & window.location.href
对接中信的微信H5支付时,对方(其实是微信)需要对我们的域名进行授权,即,我方需向渠道报备支付域名,微信只认可由此域名发起的支付交易. 支付中心只提供了一套支付接口供下游系统访问.因为给渠道报备的域名 ...
- RESTful 和RPC
RESTful 全称是 Resource Representational State Transfer 即资源表现状态转换 通俗来说就是 资源在网络中以某种表现形式进行状态转移 RPC 全称是Rem ...
- Hibernate基础增删改查语法
1.创建好Hibernate项目,创建好实体类和测试类,如果不会创建Hibernate项目的同学,点此处:http://www.cnblogs.com/zhaojinyan/p/9336174.htm ...
- Linux 命令整理-tailf
1.tailf 跟踪日志文件 常用参数格式: tailf -n logfile 动态跟踪日志文件logfile,最初的时候打印文件的最后10行内容. 实例 查看从倒数多少行的日志信息 2.tail 跟 ...
- <<attention is all you need>>速读笔记
背景 在seq2seq中,一般是有一个encoder 一个decoder ,一般是rnn/cnn 但是rnn 计算缓慢,所以提出了纯用注意力机制来实现编码解码. 模型结构 大部分神经序列转导模型都有一 ...