十一注解方式配置Bean
来源:互联网 发布:linux 查看网卡流量 编辑:程序博客网 时间:2024/05/29 02:07
项目地址:
https://git.coding.net/littleWhiteJ/SpringStudy2.git
Spring 能够从 classpath 下自动扫描, 侦测和实例化具有特定注解的组件.
特定组件包括:
@Component: 基本注解, 标识了一个受 Spring 管理的组件
@Respository: 标识持久层组件
@Service: 标识服务层(业务层)组件
@Controller: 标识表现层组件
当在组件类上使用了特定的注解之后, 还需要在 Spring 的配置文件中声明 <context:component-scan> :base-package 属性指定一个需要扫描的基类包,Spring 容器将会扫描这个基类包里及其子包中的所有类. 当需要扫描多个包时, 可以使用逗号分隔.如果仅希望扫描特定的类而非基包下的所有类,可使用 resource-pattern 属性过滤特定的类.
<context:include-filter> 子节点表示要包含的目标类
<context:exclude-filter> 子节点表示要排除在外的目标类
<context:component-scan> 下可以拥有若干个
<context:include-filter> 和 <context:exclude-filter> 子节点
ex:
创建如下的几个类:
1.
@Controller()public class UserController { public void execute(){ System.out.println("UserController execute..."); }}
2.
@Repository("userRepository")public class UserRepositoryImpl implements UserRepository { public void save() { System.out.println("UserRepository save..."); }}
3.
@Servicepublic class UserService { public void add(){ System.out.println("UserService add..."); }}
4.
@Componentpublic class TestObject {}配置文件:
<?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"> <!--指定IOC容器需要扫描的包--> <context:component-scan base-package="com.spring.annotation"></context:component-scan></beans>
然后在主程序中打印这些对象:
public class Main { public static void main(String[] agrs){ ApplicationContext ctx=new ClassPathXmlApplicationContext("beans-annotation.xml"); TestObject to= (TestObject) ctx.getBean("testObject"); System.out.println(to); UserController userController= (UserController) ctx.getBean("userController"); System.out.println(userController); UserRepository userRepository= (UserRepository) ctx.getBean("userRepository"); System.out.println(userRepository); UserService us= (UserService) ctx.getBean("userService"); System.out.println(us); }}
如果指定IOC容器需要扫描的包格式如下:
<context:component-scan base-package="com.spring.annotation"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/> </context:component-scan>
此时,如果再次执行主方法会报如下的异常:
只包含一部分的包:
<context:component-scan base-package="com.spring.annotation" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/> </context:component-scan>
下面我们利用注解来实现Bean与Bean之间的引用关系
@Controller()public class UserController { @Autowired UserService userService; public void execute(){ System.out.println("UserController execute..."); userService.add(); }}
@Servicepublic class UserService { @Autowired private UserRepository userRepository; public void add(){ System.out.println("UserService add..."); userRepository.save(); }}
在main方法中执行得到如下的结果:
这里用到了
@Autowired这个注解,它能够实现bean的自动装配,他会在对应的包下面找到与之类型匹配的对象
下面是这个注解的详细用法:
@Autowired 注解自动装配具有兼容类型的单个 Bean属性构造器, 普通字段(即使是非 public), 一切具有参数的方法都可以应用@Authwired 注解
默认情况下, 所有使用 @Authwired 注解的属性都需要被设置. 当 Spring 找不到匹配的 Bean 装配属性时, 会抛出异常, 若某一属性允许不被设置, 可以设置 @Authwired 注解的 required 属性为 false(如果IOC容器中找不到对应的Bean也没有关系,不会抛异常)
格式:
@Autowired(required=false)
默认情况下, 当 IOC 容器里存在多个类型兼容的 Bean 时, 通过类型的自动装配将无法工作. 此时可以在 @Qualifier 注解里提供 Bean 的名称.
格式:
用在属性上:
@Autowired @Qualifier("userService")
用在set方法上:
@Autowired @Qualifier("userService") public void setUserService(UserService userService) { this.userService = userService; }
或者:
@Autowired public void setUserService(@Qualifier("userService") UserService userService) { this.userService = userService; }
Spring 允许对方法的入参标注 @Qualifiter 已指定注入 Bean 的名称
@Authwired 注解也可以应用在数组类型的属性上, 此时 Spring 将会把所有匹配的 Bean 进行自动装配.
@Authwired 注解也可以应用在集合属性上, 此时 Spring 读取该集合的类型信息, 然后自动装配所有与之兼容的 Bean.
@Authwired 注解用在 java.util.Map 上时, 若该 Map 的键值为 String, 那么 Spring 将自动装配与之 Map 值类型兼容的 Bean, 此时 Bean 的名称作为键值
Spring 还支持 @Resource 和 @Inject 注解,这两个注解和 @Autowired 注解的功用类似
@Resource 注解要求提供一个 Bean 名称的属性,若该属性为空,则自动采用标注处的变量或方法名作为 Bean 的名称
@Inject 和 @Autowired 注解一样也是按类型匹配注入的 Bean, 但没有 reqired 属性建议使用 @Autowired 注解
- 十一注解方式配置Bean
- spring学习十一 通过注解配置bean
- spring 注解方式配置Bean
- spring 注解方式配置Bean
- Spring学习笔记----注解方式配置bean
- 基于注解@Bean的配置使用方式
- Spring4----使用注解方式配置bean和bean的属性
- spring中配置Bean的xml方式和注解方式
- Spring Bean配置方式之三:注解配置
- Spring Bean配置方式之三:注解配置
- Spring学习(九)使用ioc注解方式配置bean
- Spring-12:通过注解的方式配置Bean
- Spring学习(九)使用ioc注解方式配置bean
- annotation方式的事务注解和bean配置
- 非注解方式获取spring bean对象以及配置值
- Spring基于注解的方式配置bean的实例
- spring(三)(基于注解的方式配置bean)
- Spring笔记2-基于注解的方式配置Bean
- iOS 实用功能汇总(2)
- java中引用类型的使用
- void类型与void类型指针
- Follow me 挑战程序与设计
- JSON和JSONP
- 十一注解方式配置Bean
- Vue自定义过滤器
- sed使用
- RHEL7--UNIT11--网络管理
- Androidstudio the logging tag can be at most 23 characters
- Python操作Jenkins
- Android中把assets目录下所有图片转换成bitmap存入list集合
- ReactNative 之state来改变button的背景图片
- Mybatis 面试基础题