14Spring通过注解配置Bean(2)
下面将对13Spring通过注解配置Bean(1)的中Repository、Service、Controller通过注解方式来建立关联。
<context:component-scan>元素还会自动注册AutowiredAnnotationBeanPostProcessor后置处理器实例,该实例可以自动装配具有@Autowired属性。
- @Autowired注解会自动装配具有兼容类型的单个Bean属性
——构造器,普通字段(即使是非public),一切具有参数的方法都可以应用@Autowired注解
——默认情况下,所有使用@Autowired注解的属性都需要被设置,当Spring找不到匹配的Bean装配属性时,会抛出异常,若某一属性允许不被设置,可以设置@Autowired注解的required属性为false
——默认情况下,当IOC容器里存在多个类型兼容的Bean时,通过类型的自动装配将无法工作,此时可以在@Qualifier注解里提供Bean的名称,Spring允许对方法的入参标注@Qualifier已指定注入Bean的名称
——@Autowired注解也可以应用在数组类型的属性上,此时Spring将会把所有匹配的Bean进行自动装配。
——@Autowired注解也可以应用在集合属性上,此时Spring读取集合的类型信息,然后自动装配所有与之兼容的Bean。
——@Autowired注解用在java.util.Map上时,若该Map的键值为String,那么Spring将自动装配与之Map值类型兼容的Bean,此时ean的名称作为键值
package annotation.repository;
public interface UserRepository {
void save();
}
package annotation.repository; import annotation.TestObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository; @Repository
public class UserRepositoryImpl implements UserRepository { /**
* required = false表示IOC容器中可有可无,否则一定要配置TestObject的bean实例
*/
@Autowired(required = false)
private TestObject testObject; @Override
public void save() {
System.out.println("UserRepositoryImpl save");
System.out.println(testObject);
}
}
package annotation.repository; import org.springframework.stereotype.Repository; /**
* Created by jecyhw on 2015/6/19.
*/
@Repository
public class UserJdbcRepository implements UserRepository {
@Override
public void save() {
System.out.println("UserJdbcRepository save");
}
}
package annotation.service; import annotation.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service; @Service
public class UserService { //配置userRepository有三种方式
/* @Autowired
@Qualifier(value = "userRepositoryImpl")
private UserRepository userRepository;*/ /* @Autowired
@Qualifier(value = "userRepositoryImpl")*/
private UserRepository userRepository; /* @Autowired
@Qualifier(value = "userRepositoryImpl")//当有多个兼容的Bean时,来指定Bean的名称,未指定的话,将会使用名称自动和兼容Bean的名称比较,如果未找到唯一匹配值将会触发多个Bean无法匹配异常
public void setUserRepository( UserRepository userRepository) {
this.userRepository = userRepository;
}*/ @Autowired
public void setUserRepository( @Qualifier(value = "userRepositoryImpl") UserRepository userRepository) {
this.userRepository = userRepository;
} public void add() {
System.out.println("UserService add");
userRepository.save();
}
}
package annotation.controller; import annotation.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; @Controller
public class UserController {
@Autowired
private UserService userService; public void execute() {
System.out.println("UserController execute");
userService.add();
}
}
<?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/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">
<!--指定Spring IOC容器扫描的包-->
<!--<context:component-scan base-package="annotation">
</context:component-scan>--> <!--可以通过resource-pattern指定扫描的资源-->
<!--<context:component-scan base-package="annotation"
resource-pattern="repository/*.class">
</context:component-scan>-->
<!--context:exclude-filter 子节点指定排除哪些指定表达式的组件-->
<!--context:include-filter 子节点指定包含指定表达式的组件,该子节点需要use-default-filters 配个使用-->
<context:component-scan base-package="annotation">
<!--annotation表示根据指定注解来包含还是不包含-->
<!--<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"></context:exclude-filter>-->
<!--<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"></context:include-filter>-->
<!--assignable根据指定类名来包含还是不包含-->
<!-- <context:exclude-filter type="assignable" expression="annotation.repository.UserRepository"></context:exclude-filter>-->
<!--<context:include-filter type="assignable" expression="annotation.repository.UserRepository"></context:include-filter>-->
</context:component-scan>
</beans>
package annotation;
import annotation.controller.UserController;
import annotation.repository.UserRepository;
import annotation.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("14-1.xml");
// TestObject to = (TestObject) ctx.getBean("testObject");
// System.out.println(to); UserController userController = (UserController) ctx.getBean("userController");
System.out.println(userController);
userController.execute(); // UserService userService = (UserService) ctx.getBean("userService");
// System.out.println(userService);
//
// UserRepository userRepository = (UserRepository) ctx.getBean("userRepository");
// System.out.println(userRepository);
}
}
14Spring通过注解配置Bean(2)的更多相关文章
- [原创]java WEB学习笔记103:Spring学习---Spring Bean配置:基于注解的方式(基于注解配置bean,基于注解来装配bean的属性)
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring(二)--FactoryBean、bean的后置处理器、数据库连接池、引用外部文件、使用注解配置bean等
实验1:配置通过静态工厂方法创建的bean [通过静态方法提供实例对象,工厂类本身不需要实例化!] 1.创建静态工厂类 public class StaticFactory { private st ...
- 通过注解配置Bean
之前说的三种配置方式,都是使用XML配置,现在我们说说使用注解配置Bean. 这部分内容主要分为两个部分:使用注解配置Bean,使用注解配置Bean属性. 在classpath中扫描组件 组件扫描:S ...
- Spring(十五):通过注解配置 Bean
在ClassPath中扫描组件 1)组件扫描(component scanning):Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件: 2)特定组件包含: --- @C ...
- IDEA02 利用Maven创建Web项目、为Web应用添加Spring框架支持、bean的创建于获取、利用注解配置Bean、自动装配Bean、MVC配置
1 环境版本说明 Jdk : 1.8 Maven : 3.5 IDEA : 专业版 2017.2 2 环境准备 2.1 Maven安装及其配置 2.2 Tomcat安装及其配置 3 详细步骤 3.1 ...
- Spring IOC机制之使用注解配置bean
一. 通过注解配置bean 1.1 概述 相对于XML方式而言,通过注解的方式配置bean更加简洁和优雅,而且和MVC组件化开发的理念十分契合,是开发中常用的使用方式. 1.2 ...
- 13Spring通过注解配置Bean(1)
配置Bean的形式:基于XML文件的方式:基于注解的方式(基于注解配置Bean:基于注解来装配Bean的属性) 下面介绍基于注解的方式来配置Bean. ——组件扫描(component scannin ...
- Spring框架入门之基于Java注解配置bean
Spring框架入门之基于Java注解配置bean 一.Spring bean配置常用的注解 常用的有四个注解 Controller: 用于控制器的注解 Service : 用于service的注解 ...
- Spring 注解配置Bean
一.使用注解配置Bean 1.注解 在类定义.方法定义.成员变量定义前使用.其简化<bean>标签,功能同<bean>标签.格式为: @注解标记名. 2.组件扫描 Spring ...
随机推荐
- bzoj 4822: [Cqoi2017]老C的任务【扫描线+树状数组+二维差分】
一个树状数组能解决的问题分要用树套树--还写错了我别是个傻子吧? 这种题还是挺多的,大概就是把每个矩形询问差分拆成四个点前缀和相加的形式(x1-1,y1-1,1)(x2.y2,1)(x1-1,y2,- ...
- c++中快速排序
(一)为什么要用c++标准库里的排序函数 Sort()函数是c++一种排序方法之一,学会了这种方法也打消我学习c++以来使用的冒泡排序和选择排序所带来的执行效率不高的问题!因为它使用的排序方法是类似于 ...
- WIN32 API ------ 最简单的Windows窗口封装类
1 开发语言抉择 1.1 关于开发Win32 程序的语言选择 C还是C++ 在决定抛弃MFC,而使用纯Win32 API 开发Window桌面程序之后,还存在一个语言的选择,这就是是否使用C++.C+ ...
- Xors on Segments Codeforces - 620F
http://codeforces.com/problemset/problem/620/F 此题是莫队,但是不能用一般的莫队做,因为是最优化问题,没有办法在删除元素的时候维护答案. 这题的方法(好像 ...
- yii2 设置多个入口文件
在web下希望加个core.php的后台入口,但因为权限问题,总是会跳转到index.php
- 190 Reverse Bits 颠倒二进制位
颠倒给定的32位无符号整数的二进制位.例如,给定输入 43261596(二进制表示为 00000010100101000001111010011100 ),返回 964176192(二进制表示为 00 ...
- 原创 SqlParameter 事务 批量数据插入
不错,很好,以后防注入批量事务提交虽然麻烦点研究了几个小时,但不会是问题了 SqlCommand cmd; HelpSqlServer helps = new HelpSqlServer(); //定 ...
- Spring---AOP注解开发&jdbc模板&Spring事务管理
一.AOP注解开发 此处需要回忆一遍AOP的概念.简单的来说,AOP就是利用动态代理技术,做到不触动源代码但却扩展了功能.那么就需要一个被扩展的对象和一个“新的功能”,例如说给某类的saveUser方 ...
- 【Python】第一个爬虫
import urllib.request import re class DownPic: def __init__(self,url,re_str): self.url = url self.re ...
- 使用 ArrayAdapter 来定制 ListView
一个 ListView,其宽高都设为 match_parent,可以更省资源. activity_main.xml <ListView android:id="@+id/list_Vi ...