【Spring第六篇】注解:Annotation
注解:Annotation
首先不惜在spring容器配置中加上以下字段:
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!-- 指定要扫描的包--><context:component-scan base-package="com.kk"/><!-- 开启注解的支持--><context:annotation-config/></beans>
<context:component-scan base-package="com.kk"/> 扫面com.kk目录下所有的注解
实体类:User
@Component注解的作用
在实体类中加上@Component注解,相当于在applicationContext.xml中添加 <bean id="user" class="com.kk.pojo.User"/>
其中在实体类中的字段加上值可以使用 @Value(""),相当于 <property name="name" value="赵六"/>
<bean id="user" class="com.kk.pojo.User"/><property name="name" value="赵六"/></bean>
//@Component 等价 于 <bean id="user" class="com.kk.pojo.User"/>
//其中id为@Component里边的参数user @Component("user")
@Component
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {// @Value("赵六") 等价 于 <property name="name" value="赵六"/>@Value("赵六")public String name;}
dao层
@Repository
在dao层的接口类中,如果我们想要把其中的类交给spring容器托管,我们可以使用@Repository注解
import org.springframework.stereotype.Repository;@Repository
public interface UserDao {}
service层
@Service
在service层的接口类中,如果我们想要把其中的类交给spring容器托管,我们可以使用@Servicea注解
@Service
public interface UserService {}
controller层
@Controller
在controller层中,如果我们想要把其中的类交给spring容器托管,我们可以使用@Controller注解
@Controller
public class UserControl {}
测试:
public class Test {@org.junit.Testpublic void test1(){ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");User user = context.getBean("user", User.class);System.out.println(user.getName());}
}
tip:
ClassPathXmlApplicationContext是spring读取xml最常用的类。而我们一般操作的是ta的接口ApplicationContext。BeanFactory和ApplicationContext区别不大,BeanFactory不在自动BeanPostProcessor和自动 BeanFactoryPostProcessor 上注册。
【Spring第六篇】注解:Annotation相关推荐
- spring beans源码解读之--Bean的注解(annotation)
随着spring注解的引入,越来越多的开发者开始使用注解,这篇文章将对注解的机制进行串联式的讲解,不求深入透彻,但求串起spring beans注解的珍珠,展示给大家. 1. spring beans ...
- 史上最简单的SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)
转:https://blog.csdn.net/forezp/article/details/70037291 最新版本: 史上最简单的SpringCloud教程 | 第六篇: 分布式配置中心(Spr ...
- (转)Spring对注解(Annotation)处理源码分析1——扫描和读取Bean定义
1.从Spring2.0以后的版本中,Spring也引入了基于注解(Annotation)方式的配置,注解(Annotation)是JDK1.5中引入的一个新特性,用于简化Bean的配置,某些场合可以 ...
- spring之旅第四篇-注解配置详解
spring之旅第四篇-注解配置详解 一.引言 最近因为找工作,导致很长时间没有更新,找工作的时候你会明白浪费的时间后面都是要还的,现在的每一点努力,将来也会给你回报的,但行好事,莫问前程!努力总不会 ...
- Spring注解Annotation
1. 使用Spring注解来注入属性 1.1. 使用注解以前我们是怎样注入属性的 类的实现: Java代码 public class UserManagerImpl implements Use ...
- spring中自定义注解(annotation)与AOP中获取注解___使用aspectj的@Around注解实现用户操作和操作结果日志
spring中自定义注解(annotation)与AOP中获取注解 一.自定义注解(annotation) 自定义注解的作用:在反射中获取注解,以取得注解修饰的类.方法或属性的相关解释. packag ...
- Java基础回归之注解Annotation【低仿ButterKnife实战篇】
前言 书接上回,上回说到库里对战湖人三分10投0中,真真气煞我库也,这下把气全撒在鹈鹕身上,一口气轰下破纪录的13记三分. 上回说到Java基础回归之注解Annotation[基础篇],这回我们来真刀 ...
- 五篇教你掌握spring之三:详解Spring的bean以及注解开发
详解Spring的bean以及注解开发 各种复杂类型的依赖注入 我们采用一个类的大杂烩的形式,新建一个Student package com.lwh.pojo;import java.util.*;p ...
- Spring声明式基于注解的缓存(2-实践篇)
目录 一.序言 二.使用示例 1.配置 (1) application.properties (2) 基于Redis缓存的CacheManager配置 2.注解运用测试用例 (1) 指定key条件式缓 ...
最新文章
- python是一门什么课程-为什么说Python是一门伟大的入门语言?丨课程推荐
- ZooKeeper安装指南
- 我的博客今天6岁298天了,我领取了元老博主徽章
- 安装mysql5.6.10_windows下安装mysql(mysql-installer-community-5.6.10.1)详细教程
- Docker Dirty Cow逃逸
- TensorFlow应用实战-17-Qlearning实现迷宫小游戏
- django get和filter方法的几点不同
- shell编程之awk
- @PostConstruct、@PreDestroy注解介绍及Spring中@PostConstruct、constructor、@Autowired的顺序
- 创建一个简单的数据库
- 使用python 启动一个web服务
- python基于PIL模块实现矩阵与图像数据互相转换
- [NOI2003]文本编辑器
- ubuntu生成pem证书连接服务器(已验证)
- 体验服务器windows系统,经典重温:Windows 95操作系统体验
- cordova打包之android应用签名
- RTK差分通讯链路---电台
- iTunes出现“iTunes不能读取iPhone的内容,请前往iPhone偏好设置的摘要选项卡,然后点击“恢复””的解决办法
- opencv-视频处理--画感兴趣区域(ROI)
- Linux Huge page