Spring的Bean的管理-注释的装配
基于注释的装配
在Spring中,使用xml配置文件实现Bean的装配工作,但是实际开发中如果Bean的数量较多,会导致XML配置文件过于臃肿,给后期维护升级带来一定的困难。为解决此问题,Spring提供了注解,通过注解也可以实现Bean的装配
语言: java
技术: spring
运行环境: eclipse
测试工具: JUnit
前言
这里提供一些Spring常用注解
注解 | 描述 |
---|---|
@Component | 指定一个普通的Bean,可以作用在任何层次 |
@Controller | 指定一个控制器组件Bean,用于将控制层的类标识为Spring中的Bean,功能上等同于@Component |
@Service | 指定一个业务逻辑组件Bean,用于将业务逻辑层的类标识为Spring中的Bean,功能上等@Component |
@Repository | 指定一个数据访问组件Bean,用于将数据访问层的类标识为Spring中的Bean,功能上@Component |
@Scope | 指定Bean实例的作用域 |
@Value | 指定Bean实例的注入值 |
@Autowired | 指定要自动装配的对象 |
@Resource | 指定要注入的对象 |
@Qualifier | 指定要自动装配的对象名称,通常与@Autowired联合使用 |
@PostConstruct | 指定Bean实例完成初始化后调用的方法 |
@PreDestroy | 指定Bean实例销毁前调用的方法 |
一、导入依赖
在webapp下的WEB-INF下的lib包中导入spring-aop-4.3.6RELEASE.jar依赖包
总体结构如下:
二、创建XML配置文件
在src目录下创建applicationContext.xml注意:是放在src目录下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 设置开启注解 -->
<context:annotation-config />
<!-- 设置扫描哪个包下的注解 -->
<context:component-scan base-package="controller" />
<context:component-scan base-package="dao" />
<context:component-scan base-package="service" />
<context:component-scan base-package="test" />
<!-- 以上是主要要扫描的四个包下的注解,也可将四个包都放在同一个包下面,这样只需写一行代码 -->
</beans>
三、定义、实现
1.定义DAO层
package dao;
/**
*在dao包下创建UserDao接口作为数据访问层接口,
*并在UserDao接口中声明say()方法,
*/
public interface UserDao {
public void say();
}
}
2.实现DAO层
package dao;
/**
* 在dao包下创建UserDaoImpl作为UserDao的实现类,
* 并在UserDaoImpl类中实现UserDao接口中的say()方法
*/
import org.springframework.stereotype.Repository;
@Repository(value="userDao") //@Repository注解将UserDaoImpl类标识为Spring中的Bean
public class UserDaoImpl implements UserDao{
@Override
public void say() {
System.out.println("userDao say hello world!"); //打印提示信息
}
}
3.定义Service层
package service;
/**
*在service包下创建UserService接口作为业务逻辑层接口,
*并在UserService接口中定义say()方法
*/
public interface UserService {
public void say();
}
4.实现Service层
package service;
/**
* 在service包下创建UserServiceImpl作为UserService的实现类,
* 并在UserServiceImpl类中实现UserService接口中的Say()方法。
*/
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import dao.UserDao;
@Service(value="userService") //使用@Service注解将UserServiceImpl类标识为Spring中的Bean
public class UserServiceImpl implements UserService{
@Resource(name="userDao") //使用@Resource注解注入UserDao
private UserDao userDao;
@Override
public void say() {
this.userDao.say();
System.out.println("UserService say hello world");
}
}
5.定义Controller层
package controller;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import service.UserService;
@Controller //使用@Controller注解将UserController类标识为Spring中的Bean
public class UserController {
@Resource(name="userService") 使用@Resource注解注入UserService
private UserService userService;
public void say() {
this.userService.say();
System.out.println("userController say Hello world");
}
}
6.定义测试类
package test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import controller.UserController;
public class test {
@Test //@Test:测试方法,在这里可以测试期望异常和超时时间
public void test(){
//通过容器获取对象而非new
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
UserController userController = (UserController)applicationContext.getBean("userController");
userController.say();
}
}
7.添加JUnit
这里可能Test会报错,原因可能是没有添加JUnit
以下是添加方法:
7.测试结果
测试步骤1
测试步骤2
测试步骤3
测试步骤4
总结
这里对文章进行总结:
Spring容器已经成功获取了UserController实例,并通过调用实例中的方法执行了各层中的输出语句,这说明Spring容器基于注解完成了Bean的装配。
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgcbjaf
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01