Spring 讲解(四)
Spring 中使用注解注入
注解:就是一个类,使用
@
注解名称。实际开发中:使用注解取代 xml 配置文件。
1、常用注解释义
@component
取代 <bean class="">
@Component("id")
取代 <bean id="" class="">
web开发,提供3个 @Component 注解衍生注解取代
@Repository(“名称”):dao层
@Service(“名称”):service层
@Controller(“名称”):web层
web 开发中其他常用注解
@Autowired:自动根据类型注入
@Qualifier(“名称”):指定自动注入的id名称
@Resource(“名称”)
@ PostConstruct 自定义初始化
@ PreDestroy 自定义销毁
2、案例代码演示
接口和实现类
public interface UserService {
void add(User user);
}
=========================================================================================
@Component
public class UserServiceImpl implements UserService {
@Override
public void add(User user) {
System.out.println("添加用户信息..."+user);
}
}
配置 bean.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
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 开启注解-->
<context:annotation-config/>
<!-- 注解的位置-->
<context:component-scan base-package="com.example.demo"/>
</beans>
测试函数
public class ServiceTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserService userService = context.getBean(UserServiceImpl.class);
User user = new User();
user.setUsername("元始天尊");
user.setAge(999);
user.setPassword("555");
userService.add(user);
}
}
控制台日志信息如下:
添加用户信息...User{username='元始天尊', password='555', age=999}
3、重点:注解是如何取代 xml 中 bean 的配置
a、在类中声明@Component 相当于配置了 :
<bean class="com.example.demo.service.impl.UserServiceImpl"></bean>
b、Spring默认情况下注解不生效,需要开启注解功能 :
<!-- 开启注解-->
<context:annotation-config/> <!-- 注解的位置-->
<context:component-scan base-package="com.example.demo"/>
c、使用注解时 需注意 bean.xml 配置了以下约束,否则不会生效:
!
4、还可以使用以下配置
@Component("userService")
public class UserServiceImpl implements UserService {
private String name;
public void setName(String name) {
this.name = name;
}
@Override
public void add() {
System.out.println("创建用户...." + name);
}
@Override
public void add(User user) {
System.out.println("添加用户信息..."+user);
}
}
public class ServiceTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserService userService = (UserService)context.getBean("userService");
User user = new User();
user.setUsername("元始天尊");
user.setAge(999);
user.setPassword("555");
userService.add(user);
}
}
2 种运行的结果都是一样的。
@Component("userService") 就是加上了 id ,就相当于在 Spring 容器中注入的时候有了唯一的 id 号码,所以在获取的时候可以通过 id 获取,即
UserService userService = (UserService)context.getBean("userService");。
Spring 讲解(四)的更多相关文章
- Spring第四天,BeanPostProcessor源码分析,彻底搞懂IOC注入及注解优先级问题!
- Spring系列(四) 面向切面的Spring
除了IOC外, AOP是Spring的另一个核心. Spring利用AOP解决应用横切关注点(cross-cutting concern)与业务逻辑的分离, 目的是解耦合. 横切关注点是指散布于代码多 ...
- Spring Boot(四) Mybatis-MySql
Spring Boot(四) Mybatis-MySql 0.准备数据库表 -- ---------------------------- -- Table structure for person ...
- 阶段3 2.Spring_01.Spring框架简介_01.spring课程四天安排
spring共四天 第一天:spring框架的概述以及spring中基于XML的IOC配置 第二天:spring中基于注解的IOC和ioc的案例 第三天:spring中的aop和基于XML以及注解的A ...
- spring security四种实现方式
spring security四种实现方式 spring(20) > 目录(?)[+] 最简单配置spring-securityxml实现1 实现UserDetailsService 实现动态过 ...
- 学习spring第四天
Spring第四天讲义 今日内容 Spring的事务管理 Spring和MyBatis框架的集成 1. Spring的事务管理 1.1. 事务是什么? 在操作数据库时(增删改),如果同时操作多次数据, ...
- Spring AOP四种实现方式Demo详解与相关知识探究
一.前言 在网络上看到一篇博客Spring实现AOP的4种方式,博主写的很通俗易懂,但排版实在抓狂,对于我这么一个对排版.代码格式有强迫症的人来说,实在是不能忍受~~~~(>_<)~~~~ ...
- spring学习(四) ———— 整合web项目(SSH)
清楚了spring的IOC 和 AOP,最后一篇就来整合SSH框架把,记录下来,以后应该会用的到. --WH 一.web项目中如何使用spring? 当tomcat启动时,就应该加载spring的配置 ...
- 【Spring】12、Spring Security 四种使用方式
spring security使用分类: 如何使用spring security,相信百度过的都知道,总共有四种用法,从简到深为:1.不用数据库,全部数据写在配置文件,这个也是官方文档里面的demo: ...
随机推荐
- boost intrusive
1. the advantages of intrusive container (1) Intrusive containers don't allocate memory dynamically. ...
- python之字符串切分
在工作中,经常遇到字符串切分,尤其是操作linux命令,返回一段文本,如下面这种格式 Filesystem Size Used Avail Use% Mounted on /dev/vda1 40G ...
- XSS漏洞基础
什么是XSS? XSS全程Cross-site scripting,跨站脚本攻击.恶意攻击者往Web页面里插入html代码,当用户浏览该页之时,嵌入其中Web里面的html代码会被执行,从而达到恶意用 ...
- 使用DMA方式发送串口数据
一.初始化部分代码 //串口接收DMA缓存 uint8_t Uart_Rx[UART_RX_LEN] = {}; uint32_t Uart_Send_Buffer[] = {}; void USAR ...
- CodeForces - 849B 几何
题意:给n个点,问是否能两条平行线覆盖所有的点 思路:因为要求全部覆盖,所以我们第一个点肯定是会入其中一条直线,其实只用判前三个点的所有情况即可 #include<stdio.h> #in ...
- Linux测试端口的连通性的四种方法
目录 1.telnet 2.ssh 3.crul 4.wget 方法一.telnet telnet为用户提供了在本地计算机上完成远程主机工作的能力,因此可以通过telnet来测试端口的连通性,具体用法 ...
- 2018-2019-2 《Java程序设计》第10周学习总结
20175319 2018-2019-2 <Java程序设计>第10周学习总结 教材学习内容总结 本周学习<Java程序设计>第十二章java多线程机制: - 进程与线程 程序 ...
- linux从head.s到start_kernelstart_kernel之---内核解压到重定位分析
一: arm linux 内核生成过程 1. 依据arch/arm/kernel/vmlinux.lds 生成linux内核源码根目录下的vmlinux,这个vmlinux属于未压缩,带调试信息.符号 ...
- pip install 报SSL异常和timeout异常
在安装pip3 install virtualenv时报了SSL异常 如图 pip is configured with locations that require TLS/SSL, however ...
- Yahoo34条军规——雅虎WEB前端网站优化
雅虎给出了优化网站加载速度的34条法则(包括Yslow规则22条) 详细说明,下载转发 ponytail 的译文(来自帕兰映像). 1.Minimize HTTP Requests 减少HTTP请求 ...