05_IOC容器装配Bean(注解方式)
IOC容器装配Bean(注解方式)
1.使用注解方式进行Bean注册
xml 方式: <bean id="" class="">
spring2.5版本 提供一组注解,完成Bean注册
* @Component 描述Spring框架中Bean
导入jar 和 xml方式开发是相同的
第一步 编写Class,在声明上 添加 @Component
/**
* 使用Spring2.5注解 注册Bean
*/
@Component("helloService")
// <bean id="helloService" class="...." />
publicclassHelloService{
publicvoid sayHello(){
System.out.println("hello, spring annotation!");
}
}
第二步 编写applicationContext.xml 通知Spring注解类所在包
* 需要引入 context 名称空间
<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="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">
<!-- 配置 注解Bean 所在包 -->
<context:annotation-config/>
<context:component-scanbase-package="cn.itcast.spring.a_beandefinition"></context:component-scan>
</beans>
publicclassSpringTest{
@Test
// 测试 注解Bean 注册
publicvoid demo1(){
ApplicationContext applicationContext =newClassPathXmlApplicationContext(
"applicationContext.xml");- //bean的名称来自@Component("helloService")
HelloService helloService =(HelloService) applicationContext
.getBean("helloService");helloService.sayHello();
}
}
* @Repository 用于对DAO实现类进行标注 (持久层)
* @Service 用于对Service实现类进行标注 (业务层)
* @Controller 用于对Controller实现类进行标注 (表现层)
2.属性依赖注入
1) 简单属性的注入 通过 @Value注解完成,不需要提供setter方法
@Service("userService")
public class UserService {
// 注入name属性
@Value("itcast")
private String name;
}
2) 复杂属性注入,通过@Autowired注解 完成Bean自动装配
@Autowired 默认按照类型进行注入
/**
* 用户操作数据层
*/
@Repository("userDAO")
publicclassUserDAO{
}
/**
* 用户业务层
*/
@Service("userService")
publicclassUserService{
// 注入name属性
@Value("itcast")
// 简单属性
privateString name;
- //@Autowired默认按照类型
@Autowired
privateUserDAO userDAO;
@Override
publicString toString(){
return"UserService [name="+ name +", userDAO="+ userDAO +"]";
}
}
@Autowired注解 结合 @Qualifer注解按照名称注入
@Service("userService")
public class UserService {
@Autowired
@Qualifier("uDAO")
// 复杂对象
private UserDAO userDAO;
}
@Repository("uDAO")
public class UserDAO {
}
3) 使用@Resource注解 完成复杂对象Bean装配
@Resource和@Autowired注解功能相似
@Autowired
@Qualifer("userDAO")
private UserDAO userDAO ;
@Resource(name="userDAO")
private UserDAO userDAO ;
3.Bean其它属性设置
1) 指定Bean的初始化方法和销毁方法(注解) <bean init-method="" destroy-method="" />
@PostConstruct 作用 init-method
@PreDestroy 作用 destroy-method
@Component("lifeBean")
publicclassLifeCycleBean{
@PostConstruct
publicvoid setup(){
System.out.println("初始化...");
}
@PreDestroy
publicvoid teardown(){
System.out.println("销毁...");
}
}
@Test
// 测试初始化和销毁
publicvoid demo1(){
ClassPathXmlApplicationContext applicationContext =newClassPathXmlApplicationContext(
"applicationContext.xml");LifeCycleBean lifeCycleBean =(LifeCycleBean) applicationContext
.getBean("lifeBean");System.out.println(lifeCycleBean);
// 销毁方法执行,必须销毁ApplicationContext
applicationContext.close();
}
2) Bean的作用范围 <bean scope="" />
@Scope 注解 ,默认作用域 singleton 单例
@Component("scopeBean")
// 如果没有指定scope 是 singleton 单例
@Scope("prototype")
publicclassScopeBean{
}
@Test
// 测试Bean 范围
publicvoid demo2(){
ApplicationContext applicationContext =newClassPathXmlApplicationContext(
"applicationContext.xml");ScopeBean scopeBean =(ScopeBean) applicationContext
.getBean("scopeBean");System.out.println(scopeBean);
ScopeBean scopeBean2 =(ScopeBean) applicationContext
.getBean("scopeBean");System.out.println(scopeBean2);
}
4.Spring3.0 提供 注册Bean的注解
@Configuration 指定POJO类为Spring提供Bean定义信息
@Bean 提供一个Bean定义信息
先定义2个JavaBean:
// 轿车
publicclassCar{
privateString name;
privatedouble price;
publicString getName(){
return name;
}
publicvoid setName(String name){
this.name = name;
}
publicdouble getPrice(){
return price;
}
publicvoid setPrice(double price){
this.price = price;
}
@Override
publicString toString(){
return"Car [name="+ name +", price="+ price +"]";
}
}
// 商品
publicclassProduct{
privateString pname;
privateint pnum;
publicString getPname(){
return pname;
}
publicvoid setPname(String pname){
this.pname = pname;
}
publicint getPnum(){
return pnum;
}
publicvoid setPnum(int pnum){
this.pnum = pnum;
}
@Override
publicString toString(){
return"Product [pname="+ pname +", pnum="+ pnum +"]";
}
}
/**
* 配置Bean (工厂)
*/
@Configuration
publicclassBeanConfig{
// 提供两个方法 获得Car和Product对象
@Bean(name ="car")
//方法名称随意
publicCar initCar(){
Car car =newCar();
car.setName("大众");
car.setPrice(10000);
return car;
}
@Bean(name ="product")
publicProduct showProduct(){
Product product =newProduct();
product.setPname("空调");
product.setPnum(100);
return product;
}
}
@Test
// 获得配置Bean 工厂创建Bean对象
publicvoid demo(){
ApplicationContext applicationContext =newClassPathXmlApplicationContext(
"applicationContext.xml");Car car =(Car) applicationContext.getBean("car");
System.out.println(car);
Product product =(Product) applicationContext.getBean("product");
System.out.println(product);
}
5.xml和注解混合使用
很多企业开发者 还是采用xml作为主流配置
* Bean 注册 通过XML完成
* 注入使用 @Autowired 注解完成
将2个Dao注入到Service
// 客户DAO
publicclassCustomerDAO{
}
// 订单DAO
publicclassOrderDAO{
}
// 客户Service
publicclassCustomerService{
// xml注入
privateCustomerDAO customerDAO;
publicvoid setCustomerDAO(CustomerDAO customerDAO){
this.customerDAO = customerDAO;
}
@Override
publicString toString(){
return"CustomerService [orderDAO="+ orderDAO +", customerDAO="
+ customerDAO +"]";
}
}
<bean id="customerDAO"class="cn.itcast.spring.e_xmluseannotaion.CustomerDAO"></bean>
<bean id="orderDAO" class="cn.itcast.spring.e_xmluseannotaion.OrderDAO"></bean>
<!--将DAO 注入Service-->
<bean id="customerService"class="cn.itcast.spring.e_xmluseannotaion.CustomerService">
<property name="customerDAO" ref="customerDAO"></property>
</bean>
@Test
// 完成 DAO 注入到Service测试
publicvoid demo(){
ApplicationContext applicationContext =newClassPathXmlApplicationContext(
"applicationContext2.xml");CustomerService customerService =(CustomerService) applicationContext
.getBean("customerService");System.out.println(customerService);
}
// 客户Service
publicclassCustomerService{
// 注解注入
@Autowired
privateOrderDAO orderDAO;
// xml注入
privateCustomerDAO customerDAO;
publicvoid setCustomerDAO(CustomerDAO customerDAO){
this.customerDAO = customerDAO;
}
@Override
publicString toString(){
return"CustomerService [orderDAO="+ orderDAO +", customerDAO="
+ customerDAO +"]";
}
}
<context:annotation-config/> 启用四个注解 使@Resource、@ PostConstruct、@ PreDestroy、@Autowired注解生效
结论 :
1、 xml配置 和 注解配置 效果完全相同
2、 如果Bean 来自第三方(源码无法改动), 必须使用xml
3、 Spring3.0 Bean注册方式, 使用比较少,主要用于Bean 构造逻辑及其复杂
转载于:https://www.cnblogs.com/tangwan/p/4674974.html
05_IOC容器装配Bean(注解方式)相关推荐
- Spring 框架 详解 (四)------IOC装配Bean(注解方式)
Spring的注解装配Bean Spring2.5 引入使用注解去定义Bean @Component 描述Spring框架中Bean Spring的框架中提供了与@Component注解等效的三个注 ...
- JAVAWEB开发之Spring详解之——Spring的入门以及IOC容器装配Bean(xml和注解的方式)、Spring整合web开发、整合Junit4测试
Spring框架学习路线 Spring的IOC Spring的AOP,AspectJ Spring的事务管理,三大框架的整合 Spring框架概述 什么是Spring? Spring是分层的Java ...
- 十年架构师详解,Spring-IoC容器装配Bean
先看一下Spring容器内部是如何协助的,也就是Spring容器.Bean的配置信息.Bean的实现类及应用程序之间的关系,架构社区:142019080 如下图 由上图我们看到,一个Bean从创建到被 ...
- [Spring5]IOC容器_Bean管理注解方式_注入属性@Autowired_@Qualified_@Resource_@Value
基于注解方式实现属性注入 (1)@AutoWired:根据属性类型进行自动装配 第一步 把service和dao对象创建,在service和dao类添加创建对象注解 第二步 在service注入dao ...
- [Spring5]IOC容器_Bean管理注解方式_创建对象
IOC操心Bean管理(基于注解方式) 1.什么是注解 (1)注解是代码特殊标记,格式:@注解名称(属性名称=属性值,属性名称=属性值-) (2)使用注解,注解作用在类上面,方法上面,属性上面 (3) ...
- [Spring5]IOC容器_Bean管理注解方式_完全注解开发
完全注解开发 (1)创建配置类,替代xml配置文件 package com.atguigu.spring.config;import org.springframework.context.annot ...
- [Spring5]IOC容器_Bean管理注解方式_组件扫描配置细节
开启组件扫描细节配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="ht ...
- Spring教程--IOC(注解方式)和整合junit
1 IOC装配Bean(注解方式) 1.1 Spring的注解装配Bean Spring2.5 引入使用注解去定义Bean @Component 描述Spring框架中Bean Spring的框架中 ...
- Spring基于Annotation装配Bean
在 Spring 中,尽管使用 XML 配置文件可以实现 Bean 的装配工作,但如果应用中 Bean 的数量较多,会导致 XML 配置文件过于臃肿,从而给维护和升级带来一定的困难. Java 从 J ...
最新文章
- Python:数据导入、爬虫:csv,excel,sql,html,txt
- 线段树求矩形面积并 扫描线+离散化
- debian10新建文档_Debian 10(Buster)安装过程图文详解
- redis可以存多少条数据_在银行存50万元,一年能有多少利息?不工作可以吗?...
- Facebook 正在大规模重构 React Native
- java中的gridy_JAVA格局管教器.
- 修复jqgrid setgridparam postdata 的多次查询条件累加
- 【雷达通信】基于matlab GUI雷达脉冲压缩【含Matlab源码 303期】
- 行路难PPT计算机考试,《行路难》公开课.ppt
- hub设备_「网络安全」网络设备篇(9)——集线器Hub
- T和?是什么 ?有什么区别?
- 《当我谈跑步时我谈些什么》:痛苦难以避免,而磨难可以选择
- Python-玩转数据-利用百度高德经纬度地图定位
- java crm 进销存 websocket即时聊天发图片文字 好友群组 SSM源码
- 基于ssm的基金分析系统的设计与实现-计算机毕业设计
- (FortiGate)飞塔防火墙IPMAC绑定设置步骤
- react核心精讲视频与实战教程
- 2021华为校园招聘算法题
- 免费录屏软件 Captura 及 FFmpeg 安装配置教程
- No provider available from registry 127.0.0.1:2181 for service com.ddbuy.ser 解决方法
热门文章
- plpythonu_PostgreSQL PL/Python 函数
- selenium java封装_selenium2.0的初步封装(java版本)
- PHP笔记-表格及分页功能
- Qt笔记-QSslSocket双向认证
- Java工作笔记-接入互联网的免费WebService
- WEB安全基础-URL跳转漏洞
- might和could的区别用法_might 与 could区别,谢谢,may和might的区别及用法
- 长沙中级职称计算机考试时间,湖南土木工程中级职称注册及每年考试时间是什么时候...
- mysql php宝塔 root_宝塔面板,脚本命令
- 数据结构之查找算法:基本概念