• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Spring的Bean的管理-注释的装配

武飞扬头像
阋木
帮助1

基于注释的装配

在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
系列文章
更多 icon
同类精品
更多 icon
继续加载