博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring系列教程——09Spring注解开发
阅读量:3958 次
发布时间:2019-05-24

本文共 2641 字,大约阅读时间需要 8 分钟。

Spring系列教程——09Spring注解开发

注解开发在Spring中与beans.xml配置文件有着相同的作用,不过注解的使用相比配置文件的话会相对简洁一点。

文章目录

一般我们在使用注解开发最开始的beans.xml文件的内容为:

但是现在进行注解开发我们需要加上一些内容变为:

一.@Component的使用

接下来我们给Student类加上@Component注解

测试类代码如下:

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");Student student = context.getBean(Student.class);System.out.println(student);

在这里插入图片描述

测试成功。

这里顺便提一下,项目中都是用的Spring3,JDK7,如果版本不对应好,可能会出现Failed to read candidate component class

@Component的作用相当于我们前面的章节在beans.xml文件里面配置了

\<bean id="" class="">,上面我们直接在Student类上面加上了@Component,其实我们还可以去指定他的id,@Component(“student”);这时我们的测试代码为:

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");Student student = (Student)context.getBean("student");System.out.println(student);

结果一样:

在这里插入图片描述
这里我们总结一下:

如果@Component没配置id,通过类型获取(这个类型可以是接口,还可以是实现类,前者没有演示);如果配置了就用id获取

上面提到这个类型可以写接口但是要注意一下,这个接口的的实现类与他在同一个包下,而且两者都被@Component修饰。

二.注解开发实现web-service-dao

dao层:

@Repository//dao层public class UserDaoImpl implements UserDao {
private String name; @Override public void add() {
System.out.println("实现了Dao层的add"); }}

service层:

@Service//service层public class UserService {
@Autowired//自动向userDao赋值 private UserDao userDao; private String name; public void add(User user){
System.out.println("service add方法被调用"); userDao.add(); }}

web层

@Controller//web层public class UserAction {
@Autowired//Spring自动赋值给userService private UserService userService; public void Save(User user){
System.out.println("Action save方法被调用,添加了"+user); userService.add(user); }}

测试代码如下:

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");UserAction bean =  context.getBean(UserAction.class);User user = new User();user.setUsername("jack");user.setPassword("123");bean.Save(user);

在这里插入图片描述

上面用了三个注解@Repository@Service@Controller,他们都是@Component的衍生注解,这三个注解可以保证Bean对象产生的顺序。具体执行的过程如下:

首先@Repository修饰的UserDaoImpl类生产bean对象交由Spring容器来管理;接下来是@Service修饰的UserService生成bean对象,这一过程中被@Autowired修饰的UserDao自动注入前面产生的UserDaoImpl对象;接下来@Controller修饰的UserAction对象产生Bean对象,它里面的被@Autowired修饰的userService由前一步产生的UserService的Bean对象注入。

上面我们演示了没有带id的注解的使用方式,如果使用带id的,这里举个例子,比如我们的UserDaoImp的修饰注解为@Repository("userDaoImpl");那么在UserService类里面的private UserDao userDao;应该这样使用注解:

@Autowired//自动注入@Qualifier("userDaoImpl")//指定特定的id的Bean对象来注入private UserDao userDao;

依次类推。

三.其他注解介绍

@Resource("id"):等效于 @Autowired @Qualifier("id")的合用

@Scope: 例如@Scope("prototype")表示对应的bean对象不是单例的
@PostConstruct:等价于我们这一节里面讲生命周期时配置的init-method
@PreDestroy:等价于我们这一节里面讲生命周期时配置的destroy-method

转载地址:http://otlzi.baihongyu.com/

你可能感兴趣的文章
Linux Netlink通信机制详解
查看>>
rsync 远程同步
查看>>
nano使用
查看>>
c函数
查看>>
linux 链接
查看>>
centos6.x 添加开机启动服务
查看>>
zfs 简单使用
查看>>
linux EXT4格式分区扩容
查看>>
实现 du 命令
查看>>
git revert reset 使用
查看>>
一些比较好的golang安全项目
查看>>
HTTP状态码
查看>>
go语言
查看>>
mysql mariaDB 以及存储引擎
查看>>
游戏行业了解介绍
查看>>
linux at 命令使用
查看>>
Go在windows下执行命令行指令
查看>>
inotify
查看>>
inode
查看>>
Shell: sh,bash,csh,tcsh等shell的区别
查看>>