Spring3的IOC的annotation学习笔记
以下记录了一个小小的Spring3的annotation的应用笔记。
文件列表:
UserService-interface
UserDao-interface
UserServiceImpl-UserService接口的实现
UserDaoImpl-UserDao接口的实现
User-实体类
package com.niewj.model;
public class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.niewj.service;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import com.niewj.dao.LogDao;
import com.niewj.dao.UserDao;
import com.niewj.model.User;
@Service("userServiceImplementation")
// @Component/@Repository/@Controller/@Service
@Scope("singleton")
// @Scope("prototype")
public class UserServiceImpl implements UserService {
// 也可以此处声明,也可以在setter处声明。
/* @Autowired
@Qualifier("userDaoImplementation")*/
//@Resource(name="userDaoImplementation")// 不指定的话,他会找setter方法,最后可以会退到,找byType匹配。
private UserDao uuuserDao;
private LogDao logDao;
public UserDao getUuuserDao() {
return uuuserDao;
}
/*
@Autowired// (required=false)
@Qualifier("userDaoImplementation")// id/name
*/
@Resource(name="userDaoImplementation")
public void setUuuserDao(UserDao uuuserDao) {
this.uuuserDao = uuuserDao;
}
public LogDao getLogDao() {
return logDao;
}
@Autowired
public void setLogDao(LogDao logDao) {
this.logDao = logDao;
}
@Override
public boolean regist(User user) {
logDao.log();
return uuuserDao.save(user);
}
}
package com.niewj.dao;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.stereotype.Repository;
import com.niewj.model.User;
/* @Repository/@Controller/@Service都属于@Component,只是为了更明显的标注业务层、控制层、还是Dao层 */
@Repository("userDaoImplementation")
public class UserDaoImpl implements UserDao {
@PostConstruct// 相当于<bean init-method="init">
public void init(){
System.err.println("实例化DAO后,会马上调用此方法。");
}
@PreDestroy//相当于<bean destroy-method="destroy">
public void destroy(){
System.err.println("销毁DAO之前,会执行此方法。");
}
@Override
public boolean save(User user) {
System.err.println("新增用户:" +user.getName());
return true;
}
}
package com.niewj.dao;
import org.springframework.stereotype.Repository;
@Repository("logDao")
public class LogDaoImpl implements LogDao {
@Override
public void log() {
System.out.println("Logging......记录在日志............OK");
}
}
package com.niewj;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.niewj.model.User;
import com.niewj.service.UserService;
import com.niewj.service.UserServiceImpl;
public class AnnotationTest {
@Test
public void testIocAnnotation() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService loginService = ac.getBean("userServiceImplementation", UserServiceImpl.class);
User user = new User();
user.setName("dotjar");
loginService.regist(user);
}
}
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <import resource="beans.xml" /> </beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!-- 开启Annotation支持 如果有context:component-scan可以省去此处声明-->
<!-- <context:annotation-config /> -->
<context:component-scan base-package="com.niewj">
</context:component-scan>
</beans>
1.UserServiceImpl中的@Service中的字串应该和getBean中的一致;
2.UserDaoImpl中的@Repository中参数应该和UserServiceImpl中Setter方法处的@Autowired@Qualifier("dao参数")一致
3.配置文件中开启注解支持:
*1.配置文件中开启context命名空间支持:<beans>标记中加入:
<1>加入【xmlns】:【xmlns:context="http://www.springframework.org/schema/context"】
<2>加入【xsi:schemaLocation】:
【http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd】
*2.配置文件中开启注解支持:
<context:annotation-config />
*3.因为有<context:component-scan>所以, <context:annotation-config />可以省去。
小结:
1.
@Repository/@Controller/@Service都属于@Component,只是为了更明显的标注业务层、控制层、还是Dao层
2.
@Resource(name="userDaoImplementation")// 不指定name的话,他会找setter方法,按照byName找不到,最后可以byType匹配。
3.
@Autowired // byType自动装配
@Qualifier("userDaoImplementation") //byName,必须有@Autowired同时出现。
Spring3的IOC的annotation学习笔记的更多相关文章
- Spring入门IOC和AOP学习笔记
Spring入门IOC和AOP学习笔记 概述 Spring框架的核心有两个: Spring容器作为超级大工厂,负责管理.创建所有的Java对象,这些Java对象被称为Bean. Spring容器管理容 ...
- Java注解Annotation学习笔记
一.自定义注解 1. 使用关键字 @interface 2. 默认情况下,注解可以修饰 类.方法.接口等. 3. 如下为一个基本的注解例子: //注解中的成员变量非常像定义接口 public @int ...
- IoC容器Autofac学习笔记
一.一个没有使用IoC的例子 IoC的全称是Inversion of Control,中文叫控制反转.要理解控制反转,可以看看非控制反转的一个例子. public class MPGMovieList ...
- JDK5.0 Annotation学习笔记(一)
背景知识: 从JDK5开始提供名为Annotation(注释)的功能,它被定义为JSR-175规范.注释是以"@注释名"在代码中存在的,还可以添加一些参数值,例如: ...
- Spring中的IOC容器(学习笔记)
如何将Bean配置到Spring的Bean容器中 通过xml配置文件: Bean实现类来自第三方类库:如“DataSource”等 需要命名空间配置如:context,aop,mvc等 ...
- Web Service学习笔记(webservice、soap、wsdl、jws详细分析)
Web Service概述 Web Service的定义 W3C组织对其的定义如下,它是一个软件系统,为了支持跨网络的机器间相互操作交互而设计.Web Service服务通常被定义为一组模块化的API ...
- Web Service学习笔记
Web Service概述 Web Service的定义 W3C组织对其的定义如下,它是一个软件系统,为了支持跨网络的机器间相互操作交互而设计.Web Service服务通常被定义为一组模块化的API ...
- Web Service学习笔记(webservice、soap、wsdl、jws详细分析) (转)
Web Service概述 Web Service的定义 W3C组织对其的定义如下,它是一个软件系统,为了支持跨网络的机器间相互操作交互而设计.Web Service服务通常被定义为一组模块化的API ...
- Hibernate 马士兵 学习笔记 (转)
目录(?)[+] 第2课 Hibernate UML图 第3课 风格 第4课 资源 第5课 环境准备 第6课 第一个示例Hibernate HelloWorld 第7课 建立Annotation版本的 ...
随机推荐
- 记录一次源码扩展案列——FastJson自定义反序列化ValueMutator
背景:曾经遇到一个很麻烦的事情,就是一个json串中有很多占位符,需要替换成特定文案.如果将json转换成对象后,在一个一个属性去转换的话就出出现很多冗余代码,不美观也不是很实用. 而且也不能提前在j ...
- java基础知识总结(续写)
1.两个容易搞混的C盘文件夹 文件名 描述 Progrm Files 默认存储的64位软件 Progrm Files(x86) 默认存储32位软件 2.常用基础DOS命令(Windows+R打开命令) ...
- pytest文档43-元数据使用(pytest-metadata)
前言 什么是元数据?元数据是关于数据的描述,存储着关于数据的信息,为人们更方便地检索信息提供了帮助. pytest 框架里面的元数据可以使用 pytest-metadata 插件实现.文档地址http ...
- P2340 [USACO03FALL]Cow Exhibition G题解
新的奇巧淫技 原题传送门 众所周知,模拟退火是一种很强大的算法,DP很强,但我模拟退火也不虚,很多题你如果不会的话基本可以拿来水很多分.比如这道题,我用模拟退火可以轻松水过(虽然我是足足交了两页才过) ...
- kubernetes:用kubeadm管理token(kubernetes 1.18.3)
一,token的用途: 1,token是node节点用来连接master节点的令牌字串, 它和ca证书的hash值是把一台node节点加入到kubernetes集群时要使用的凭证 2, 通过kubea ...
- 第三章 虚拟机的简单使用及其xshell远程工具的使用
一.虚拟机的快照 1.虚拟机的几种状态: 开机状态 === 运行状态 关机状态 挂起状态 === 虚拟机不关机,但是你使用不了 定身术 快照就是虚拟机的某种状态 === 月光宝盒 2.快照分类: 开机 ...
- Linux终端 terminal
终端 一个可以进行人机交互的界面 物理终端 设备终端:键盘.鼠标.显示器 终端类型 控制台终端: /dev/console 串行终端:/dev/ttyS# 虚拟终端:tty:teletypewrite ...
- [Vue音乐项目] 第一节 环境搭建
1.Node安装 登录官网,下载最新版本并安装: 在我的电脑内,执行以下操作:右键->属性->高级->环境变量->系统变量->path 查看是否有node的安装路径,没有 ...
- Paillier同态加密的介绍以及c++实现
我们先来简短认识一下Paillier同态加密算法: 如果就这么按照定义来用最简朴的c++程序写 就像这样: #include <iostream> #include <math.h& ...
- uart接口介绍和认识
接口/总线/驱动 UART (Universal Asynchronous Receiver/Transmitter) 通用异步收发器. UART是用于控制计算机与串行设备的芯片.有一点要注意的是,它 ...