Spring 基于注解配置&装配Bean

本文详细介绍Spring框架中使用注解进行Bean配置和装配的方法,包括@Component、@Service等注解的应用,以及@Autowired如何实现Bean之间的自动装配。

该篇博客主要阐述使用注解来配置Bean以及装配Bean(Bean与Bean之间关联关系),通过注解可以大大减少使用XML一大堆的配置代码,方便开发


注意:该篇博客阅读时必须理解清楚什么是配置Bean什么是装配Bean
- 配置Bean:将Bean装载到IoC容器中(注册)
- 装配Bean:Bean与Bean之间的依赖注入

博客主要阐述

1、基于注解配置Bean
2、基于注解装配Bean

一、基于注解配置Bean

作用:就是将Bean装到IoC容器中

1、为自动检测标注Bean

Spring能从classpath下自动扫描、侦测、实例化具有特定注解的组件,这些特定注解的组件如下

  • @Component——通用的构造型注解
  • @Controller——标识控制层(web层)
  • @Repository——标识持久层(Dao层)
  • @Service——标识业务层(Service层)

在目前的Spring版本中,@Controller、@Repository、@Service这三个注解与@Component基本等效,可以混用,但是从命名上可以看出三个注解分别代表哪一层。所以虽然目前三个注解与@Component是等效的,但Spring之后版本或许会为这三个注解带来不一样的新意,所以最好是使用这三个注解来标注每一个层

2、Component用法

Person.java

package com.linjie.springidol;

import org.springframework.stereotype.Component;

/**
 * @author LinJie
 * Component用法
 */
@Component("person")
public class Person {
    public void add(Person p) {
        System.out.println("Person.....");
    }
}

测试类

package com.linjie.springidol;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author LinJie
 *
 */
public class SpringTest {
    @Test
    public void test() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person = (Person) context.getBean("person");
        System.out.println(person);
        person.add(person);
    }
}

applicationContext.xml

<?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"> <!-- bean definitions here -->

            <!--base-package:指定Spring IoC容器 扫描的包  -->
         <context:component-scan base-package="com.linjie.springidol">
         </context:component-scan>

</beans>

结果

这里写图片描述

运行的结果就很清楚了,@Component为我们完成了Bean的注册和装配,我们就不再需要使用XML中的< bean>元素了。其实就相当于

<bean id="person" class="com.linjie.springidol.Person">
1、在applicationContext我们只需要用:
<context:component-scan base-package="com.linjie.springidol"></context:component-scan>

就能够使用注解了,假如你无法实现的话,可能是你未导入spring-aop-4.3.10.RELEASE.jar 具体原因可转战笔者这篇博客:https://blog.youkuaiyun.com/w_linux/article/details/80098853

2、要注意需要在applicationContext中引用context约束,如果你没有相关插件引入可以用以下方法将context约束模板引入

进入该路径spring-framework-4.3.10.RELEASE-dist\spring-framework-4.3.10.RELEASE\docs\spring-framework-reference\html,找到xsd-configuration.html,搜索the context schema,将模板导入到自己的applicationContext.xml中即可

这里写图片描述

3、base-package:指定一个需要扫描的父类包,Spring容器将会扫描这个父类包及其子类包中的所有类,当需要扫描多个包(没有继承关系)时,可以使用逗号分隔
4、@Component所标注的Person类,如果不像上面例子中@Component(“person”),而只是@Component,其Bean的ID默认为无限定类名,即首字母小写。Person Bean的ID就是person,当然显示命名ID即@Component(“person”)
5、resource-pattern只扫描指定的类而非基包下的所有类,这里dao是springidol的子包
<context:component-scan
         base-package="com.linjie.springidol"
         resource-pattern="dao/*.class"
         ></context:component-scan>
6、context:component-scan的两个子节点
  • context:exclude-filter:子节点指定排除哪些指定表达式的组件(排除)
  • context:include-filter:子节点指定包含哪些表达式的组件 (只包含),该子节点需要use-default-filters=”false”配合使用

     <context:component-scan
     base-package="com.linjie.springidol">
     <context:exclude-filter type="annotation"
         expression="org.springframework.stereotype.Service"/> 
          <!--<context:include-filter type="annotation"
             expression="org.springframework.stereotype.Service"/> -->  
     </context:component-scan>
    

type属性值:
这里写图片描述

7、一个简单例子

项目目录(一个父包三个子包)

这里写图片描述

UserView.java(Controller)

package com.linjie.springidol.view;

import org.springframework.stereotype.Controller;

/**
 * @author LinJie
 * View层
 */
@Controller
public class UserView {
    public void show() {
        System.out.println("view1...");
    }
}

UserService.java(Service)

package com.linjie.springidol.service;
import org.springframework.stereotype.Service;
/**
 * @author LinJie
 * Service层
 */
@Service
public class UserService {
    public void show() {
        System.out.println("service....");
    }
}

UserDao.java(Repository)

package com.linjie.springidol.dao;

import org.springframework.stereotype.Repository;

/**
 * @author LinJie
 * Dao层
 */
@Repository
public class UserDao {
    public void show() {
        System.out.println("dao...");
    }
}

Person.java(Component)

package com.linjie.springidol;

import org.springframework.stereotype.Component;

/**
 * @author LinJie
 * Component用法
 */
@Component("person")
public class Person {
    public void show() {
        System.out.println("Person.....");
    }
}

applicationContext.xml

<?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"> <!-- bean definitions here -->


         <context:component-scan
         base-package="com.linjie.springidol">

         </context:component-scan>


</beans>

测试类

package com.linjie.springidol;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.linjie.springidol.dao.UserDao;
import com.linjie.springidol.service.UserService;
import com.linjie.springidol.view.UserView;

public class SpringTest {
    @Test
    public void test() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person = (Person) context.getBean("person");
        person.show();

        UserDao userDao = (UserDao) context.getBean("userDao");
        userDao.show();

        UserService userService = (UserService) context.getBean("userService");
        userService.show();

        UserView userView = (UserView) context.getBean("userView");
        userView.show();
    }
}

结果

这里写图片描述


二、基于注解装配Bean

作用:Bean与Bean之间的关联关系建立(前提:已经装配Bean到IoC容器中)

元素会自动注册AutowiredAnnotationBeanPostProcessor实例(一个Bean后置处理器),该实例可以自动装配具有@Autowired、@Resource、@Inject注解的属性

Autowired
1. @Autowired注解自动装配具有兼容类型的单个Bean属性
2. 构造器、普通字段(访问控制符没有影响)、一切具有参数的方法都可以应用

普通字段:在UserService中调用UserDao

UserService.java

package com.linjie.springidol.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.linjie.springidol.dao.UserDao;
/**
 * @author LinJie
 * Service层
 */
@Service
public class UserService {
    @Autowired
    private UserDao userDao;

    public void show() {
        System.out.println("service....");
        userDao.show();
    }
}

构造器:在UserService中调用UserDao

package com.linjie.springidol.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.linjie.springidol.dao.UserDao;
/**
 * @author LinJie
 * Service层
 */
@Service
public class UserService {

    private UserDao userDao;

    @Autowired
    public UserService(UserDao userDao) {
        super();
        this.userDao = userDao;
    }


    public void show() {
        System.out.println("service....");
        userDao.show();
    }
}

有参数的方法上:在UserService中调用UserDao

package com.linjie.springidol.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.linjie.springidol.dao.UserDao;
/**
 * @author LinJie
 * Service层
 */
@Service
public class UserService {

    private UserDao userDao;

    @Autowired
    public void setUserService(UserDao userDao) {
        this.userDao = userDao;
    }


    public void show() {
        System.out.println("service....");
        userDao.show();
    }
}
3. 加上@Autowired意思就是会去IoC容器里找是否有与其被注解的Bean类型兼容的Bean(通过@Component等注册Bean的注解所装饰的才会在IoC容器里)
4. 所有使用@Autowired注解的属性都需要被设置(即通过@Component等注解配置到IoC容器中),否则会报异常,如果没有配置这个Bean,可以设置@Autowired注解的required属性为false,但该Bean就是null:@Autowired(required=false),但是注意当使用构造器装配时,只有一个构造器可以将@Autowired的required属性设置为true。其他使用@Autowired注解所标注的构造器只能将required属性设置为false,当使用@Autowired标注多个构造器时,Spring就会从所有满足装配条件构造器中选择入参最多的那个构造器

UserDao未被配置
若此时调用UserDao则报错

 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.linjie.springidol.dao.UserDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

使用@Autowired(required=false)

package com.linjie.springidol.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.linjie.springidol.dao.UserDao;
/**
 * @author LinJie
 * Service层
 */
@Service
public class UserService {

    private UserDao userDao;

    @Autowired(required=false)
    public void setUserService(UserDao userDao) {
        this.userDao = userDao;
    }


    public void show() {
        System.out.println("service....");
        System.out.println("userDao :"+userDao);
        userDao.show();
    }
}

显示结果(无法实现调用show(),因为userDao为null)

这里写图片描述

5. 限定歧义性的依赖:可能会有多个Bean都满足装配条件,并且都可以装配到属性或参数中(采用@Qualifier)
场景

假设有两个Bean都实现了Instrument接口

分析

在这种场景下,@Autowired注解没办法选择哪一个Bean才是它需要的

解决

采用Spring的@Qualifier

用法
public void setInstrument(@Qualifier("small1")Instrument instrument){}

或者

@Qualifier("small1")
public void setInstrument(Instrument instrument) {}
代码演示

Instrument.java(接口)

package com.linjie.springidol;

public interface Instrument {
    public void play();
}

small1.java(实现类1)

package com.linjie.springidol;

import org.springframework.stereotype.Component;

@Component
public class small1 implements Instrument{

    @Override
    public void play() {
        System.out.println("small1");
    }

}

small2.java(实现类2)

package com.linjie.springidol;

import org.springframework.stereotype.Component;

@Component
public class small2 implements Instrument{

    @Override
    public void play() {
        System.out.println("small2");
    }

}

big.java(调用IoC中的Bean)

package com.linjie.springidol;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component("b")
public class big {
    private Instrument instrument;

    @Autowired
    public void setInstrument(@Qualifier("small1")Instrument instrument) {
        this.instrument = instrument;
    }

    public void show() {
        System.out.println(instrument);
        instrument.play();
    }
}

测试类

package com.linjie.springidol;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.linjie.springidol.dao.UserDao;
import com.linjie.springidol.service.UserService;
import com.linjie.springidol.view.UserView;

public class SpringTest {
    @Test
    public void test() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        big b = (big) context.getBean("b");
        b.show();
    }
}

结果(可以看到通过Qualifier来指定使用small1 Bean)

这里写图片描述

6、@Autowired可以应用在数组类型的属性上,此时Spring将会把所有匹配的Bean都进行自动装配
7、@Autowired可以应用在集合属性上,此时Spring读取集合的类型信息,然后自动装配所有与之兼容的Bean
8、@Autowired用在java.util.Map上,若Map的键值为String,那么Spring将自动装配与之Map值类型兼容的Bean,此时Bean的名称作为键值
9、关于@Resource、@Inject与@Autowired相类似,所以不多做阐述

参考

《Spring IN ACTION》

&lt;think&gt;好的,我需要帮助用户了解Spring框架中基于注解配置Bean装配Bean的最佳实践。首先,我应该回顾一下用户提供的引用内容,确保回答准确。 用户提到的引用[1]区分了配置Bean装配Bean配置Bean是将Bean注册到IoC容器,而装配Bean是处理Bean之间的依赖注入。引用[2][3]给出了具体的配置示例,比如使用@Configuration@Bean配置,以及@ComponentScan进行组件扫描。 接下来,我需要按照用户的要求,结构清晰地分步骤解释最佳实践。可能需要分成配置Bean装配Bean两部分,每部分列出常用的注解,并结合代码示例说明。例如,配置Bean可以使用@Component、@Repository、@Service、@Controller等,而装配Bean则用@Autowired或@Resource。 还要注意代码示例的正确性,比如在Java配置类中使用@Configuration@Bean,或者在组件扫描时使用@ComponentScan。同时,需要提到最佳实践,如明确包路径、优先使用构造函数注入等。 最后,生成相问题,确保问题与主题相,比如于条件装配、Java配置与XML配置的区别,或者解决依赖冲突的方法。需要确保问题覆盖用户可能感兴趣的扩展内容。 需要检查是否所有数学表达式都正确使用$...$或$$...$$格式,但这个问题是Spring的,可能涉及较少数学内容,但如果有示例中的表达式,比如在说明某些原理时,可能需要使用。不过根据用户的问题,可能不需要数学公式,所以这部分可能可以忽略。 最后,确保回答使用中文,引用标识自然添加,比如在提到组件扫描时引用[3],在Java配置示例时引用[2]。&lt;/think&gt;### Spring框架中基于注解配置Bean装配Bean的最佳实践 #### 一、配置Bean:将Bean注册到IoC容器 配置Bean的核心是**通过注解标记类,使其被Spring扫描并加载到容器中**。以下是常用注解及实践方法: 1. **基础组件注解** - `@Component`:通用注解,标记任意类为Spring组件[^3]。 - 衍生注解(语义化分类): - `@Repository`:用于数据访问层(DAO) - `@Service`:用于服务层 - `@Controller`:用于Web控制层 **示例**: ```java @Service public class UserService { // 业务逻辑 } ``` 2. **Java显式配置** - 使用`@Configuration`标记配置类,配合`@Bean`方法显式定义Bean[^2]。 **示例**: ```java @Configuration public class AppConfig { @Bean public DataSource dataSource() { return new HikariDataSource(); // 返回需要管理的Bean实例 } } ``` 3. **组件扫描** - 使用`@ComponentScan`指定包路径,Spring会自动扫描并注册Bean[^3]。 **最佳实践**: ```java @Configuration @ComponentScan(&quot;com.example.service&quot;) // 明确限定扫描范围,避免性能浪费 public class AppConfig {} ``` --- #### 二、装配Bean:依赖注入(DI) 装配Bean的核心是**通过注解实现Bean之间的依赖系自动注入**,常用方式如下: 1. **字段/构造函数注入** - `@Autowired`:自动按类型注入,优先使用构造函数注入以提高可测试性不可变性[^1]。 **示例**: ```java @Service public class OrderService { private final UserService userService; @Autowired // 可省略(Spring 4.3+支持隐式构造函数注入) public OrderService(UserService userService) { this.userService = userService; } } ``` 2. **限定符与条件装配** - `@Qualifier`:按名称指定Bean,解决同一类型多个实现的问题。 - `@Primary`:标记优先注入的Bean。 **示例**: ```java @Bean @Primary public Cache redisCache() { return new RedisCache(); } @Bean public Cache localCache() { return new LocalCache(); } @Autowired @Qualifier(&quot;localCache&quot;) // 明确指定注入localCache private Cache cache; ``` 3. **简化装配注解** - `@Value`:注入简单类型或配置文件属性。 - `@Resource`:按名称注入(JSR-250标准)。 --- #### 三、最佳实践总结 1. **配置Bean**: - 优先使用组件扫描,但需明确指定包路径(如`@ComponentScan(&quot;com.example&quot;)`)[^3]。 - 第三方库的Bean使用`@Configuration` + `@Bean`显式配置。 2. **装配Bean**: - 强制依赖使用**构造函数注入**,可选依赖使用Setter注入。 - 多实现时通过`@Qualifier`或`@Primary`消除歧义[^1]。 3. **代码可维护性**: - 避免滥用`@Component`,按层级使用`@Repository`/`@Service`等注解。 - 配置类与业务代码分离,便于管理。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值