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 ...
随机推荐
- hdu 3007【最小圆覆盖-随机增量法模板】
#include<iostream> #include<cstdio> #include<cmath> #include<algorithm> usin ...
- bzoj 2561: 最小生成树【最小割】
看错题了以为多组询问吓得不行-- 其实还挺好想的,就是数据范围一点都不网络流.把U作为s,V作为t,以最小生成树为例,(U,V,L)要在最小生成树上,就要求所有边权比L小的边不能连通(U,V)所在的联 ...
- sql server 改sa 密码
ALTER LOGIN sa ENABLE ; ALTER LOGIN sa WITH PASSWORD = 'kongwenyi' ;
- Ubuntu 必装软件及安装教程
安装搜狗拼音输入法 因为sogou是基于fcitx的,所以先添加fcitx键盘输入法系统[系统默认是iBus].在终端中,输入命令将下载源添加至系统源(添加依赖). sudo add-apt-repo ...
- 利用autotools工具制作从源代码安装的软件 分类: linux 2014-06-02 23:27 340人阅读 评论(0) 收藏
编写程序(helloworld.c)并将其放到一个单独目录. helloworld.c: #include<stdio.h> int main() { printf("hello ...
- Python variable 作用域和初始化
Python 根据LEGB rule在不同的namespace中找变量 在def的函数中对global 变量做修改还是不推荐的,应该将其作为参数传入函数 try: do_something() cnt ...
- android开发学习 ------- 【转】Gradle相关
一直在用AndroidStudio,但是对于其Gradle了解的很少. 推荐 http://www.jianshu.com/p/9df3c3b6067a 觉得说的很棒!
- hihocoder offer收割编程练习赛10 C 区间价值
思路: 令v[l, r](0<= l <= r < n)表示区间[l,r]的价值,则长度为n的区间的价值最少为0,最多为n*(n-1)/2.整体对价值二分,求能满足sum{v[l, ...
- js基础 -----鼠标事件(按下 拖拽)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- CCF|游戏|Java
import java.util.Scanner; public class tyt { public static void main(String[] args) { Scanner in = n ...