博客
关于我
Spring--通过注解来配置bean【转】
阅读量:443 次
发布时间:2019-03-06

本文共 3761 字,大约阅读时间需要 12 分钟。

Spring注解驱动的Bean配置与组件扫描指南

在Spring应用中,通过注解配置Bean是一种简洁高效的方式,能够自动发现和管理需要管理的组件。以下是关于Spring注解配置Bean的详细指南,包括组件扫描、自动注入以及相关配置示例。

1. 组件扫描(Component Scanning)

Spring能够从类路径中自动扫描,发现并实例化带有特定注解的组件。这种自动化扫描使开发者免去了手动配置的痛苦。以下是组件扫描的关键点:

  • @Component:这是Spring管理组件的基础注解,标识一个受Spring管理的组件。
  • @Repository:标识持久层组件,通常用于DAO层。
  • @Service:标识服务层(业务层)组件。
  • @Controller:标识表现层组件,通常用于Web层。

Spring默认的命名策略是:使用类的全名,首字母大写,除非在注解中指定了value属性来指定bean的名称。

要启用组件扫描,需要在Spring配置文件中添加如下配置:

  • base-package:指定需要扫描的基础包路径,Spring将扫描该包及其子包中的所有类。
  • resource-pattern:用于过滤特定的类,可以通过正则表达式指定。
  • include-filter:指定要包含的目标类。
  • exclude-filter:指定要排除的目标类。

2. 自动注入(Autowired)

@Autowired注解是Spring最常用的注入方式,支持通过类型、名称或自定义逻辑进行注入。以下是@Autowired的使用要点:

  • 类型注入:默认情况下,Spring通过类型进行注入。对于接口或非接口类型,Spring会查找唯一匹配的实现类。
  • 名称注入:可以通过@Qualifier注解指定特定的bean名称。
  • 数组注入:@Autowired可以注入数组类型的属性,Spring会自动匹配所有兼容类型的bean。
  • 集合注入:@Autowired可以注入集合属性,Spring会根据集合的类型自动装配所有兼容类型的bean。
  • Map注入:@Autowired可以注入Map属性。当Map的键值类型已知时,Spring会根据键值类型自动装配对应的bean。

3. @Resource和@Inject注解

除了@Autowired,Spring还支持@Resource和@Inject注解,这两种注解与@Autowired功能相似,但有一些细微差别:

  • @Resource:需要指定bean名称(如果不指定,默认使用字段或方法名)。与@Autowired不同,@Resource不支持required属性,默认所有属性都需要被注入。
  • @Inject:与@Autowired类似,但没有required属性,默认所有属性都需要被注入。@Inject不支持数组或集合注入。

建议优先使用@Autowired注解,因为它提供了更丰富的功能。

4. 代码示例

以下是一些典型的代码示例,展示了如何使用Spring注解配置Bean和组件扫描。

TestObject.java
package com.yl.annotation;import org.springframework.stereotype.Component;@Componentpublic class TestObject {}
UserRepository.java
package com.yl.annotation.repository;public interface UserRepository {    void save();}
UserRepositoryImpl.java
package com.yl.annotation.repository;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Repository;import com.yl.annotation.TestObject;@Repository("userRepository")public class UserRepositoryImpl implements UserRepository {    @Autowired    private TestObject testObject;    @Override    public void save() {        System.out.println("UserRepository save...");        System.out.println(testObject);    }}
UserJdbcRepository.java
package com.yl.annotation.repository;import org.springframework.stereotype.Repository;@Repositorypublic class UserJdbcRepository implements UserRepository {    @Override    public void save() {        System.out.println("UserJdbcRepository save...");    }}
UserService.java
package com.yl.annotation.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.stereotype.Service;import com.yl.annotation.repository.UserRepository;@Servicepublic class UserService {    @Autowired    @Qualifier("userJdbcRepository")    private UserRepository userRepository;    public void add() {        System.out.println("UserService add...");        userRepository.save();    }}
UserController.java
package com.yl.annotation.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import com.yl.annotation.service.UserService;@Controllerpublic class UserController {    @Autowired    private UserService userService;    public void execute() {        System.out.println("UserController execute...");        userService.add();    }}
beans-annotation.xml

5. 自定义过滤

如果需要更复杂的过滤,可以使用自定义TypeFilter实现自定义的过滤逻辑。例如:

package com.yl.filter;import org.springframework.core.type.TypeFilter;public class MyCustomFilter extends TypeFilter {    public MyCustomFilter() {        super(MyCustomFilter.class);    }    @Override    protected boolean match( Class
type ) { return super.match(type) && isMyCondition(type); } private boolean isMyCondition(Class
type) { return type.getName().contains("Custom"); }}

在Spring配置中使用:

转载地址:http://llayz.baihongyu.com/

你可能感兴趣的文章
php laravel实现依赖注入原理(反射机制)
查看>>
php laravel请求处理管道(装饰者模式)
查看>>
ReentrantReadWriteLock读写锁底层实现、StampLock详解
查看>>
PHP mongoDB 操作
查看>>
ReentrantLock读写锁
查看>>
ReentrantLock的公平锁与非公平锁
查看>>
php mysql procedure获取多个结果集
查看>>
php mysql query 行数,PHP和MySQL:返回的行数
查看>>
php mysql session_php使用MySQL保存session会话
查看>>
PHP mysql_real_escape_string() 函数防SQL注入
查看>>
php mysql优化方法_MySQL优化常用方法
查看>>
PHP OAuth 2.0 Server
查看>>
php odbc驱动,php常用ODBC函数集(详细)
查看>>
php openssl aes ecb,php openssl_encrypt AES-128-ECB iOS
查看>>
php paypal rest api,PayPal REST API指定网络配置文件PHP
查看>>
php pcntl 多进程学习
查看>>
PHP pcntl_fork不能在web服务器中使用的变通方法
查看>>
php private ,public protected三者的区别
查看>>
php PSR规范
查看>>
php rand() 重复,array_rand()函数从另外一个数组中随机取得的一定数量的数组的元素是否会重复?...
查看>>