基于注解配置Bean

本文详细介绍了Spring框架中基于注解的组件配置和自动扫描机制,包括@Component、@Repository、@Service、@Controller等注解的使用,以及<context:component-scan>元素的配置方法,探讨了组件的自动装配、过滤和命名策略。

基于注解配置Bean
组件扫描,spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件,特定组件包括
@Component:基本注解,标识了一个受Spring管理的组件
@Respository:标识持久层组件
@Service:标识服务层(业务逻辑层)组件
@Controller:标识控制层
对于扫描到的组件,Spring有默认的命名策略:使用非限定类名,第一个字母小写,也可以在注解中通过value属性值标识组件的名称。

当在组件类上使用了特定的注解之后,还需要在Spring的配置文件中声明context:component-scan,要导入context命名空间,base-package属性指定一个需要扫描的基类包,Spring容器将会扫描这个基类包及其子包中的所有类,当需要扫描多个包时,可以使用逗号分隔,如果仅希望扫描特定的类而非基类包下的所有类,可使用resource-pattern属性过滤特定的类

//默认情况下resource-pattern表示过滤基类包及其子包下所有的类,只能是class结尾的
<context:component-scan base-package="com.cmzy.spring.pojo" resource-pattern="**/*.class"></context:component-scan>

context:component-scan下还可包含两个子节点

//指定排除哪些指定表达式的组件
<context:exclude-filter type="annotation" expression=""/>
//指定包含哪些指定表达式的组件
<context:include-filter type="annotation" expression=""/>

type用于指定用何种类型,默认是annotation,如果是annotation,则expression是指定注解的全类名,如下表示排除指定包下的所有Repository注解标识的类

<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>

在这里插入图片描述
当时使用 context:include-filter时,需要和use-default-filters结合使用,use-default-filters默认为true,表示使用默认的过滤器,而当修改为false,则会使用 context:include-filter声明的。

	<context:component-scan base-package="com.cmzy.spring.annotion" use-default-filters="false">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
	</context:component-scan>

当type是assignable时,expression是指定具体的接口或类的全类名,其他的用法与annotation相同

//AnnotationDao是自定义的类,其子类本来是用@Repository修饰的,使用下面的配置表明只将该接口的子类被Spring容器管理
 	<context:component-scan base-package="com.cmzy.spring.annotion" use-default-filters="false">
		<context:include-filter type="assignable" expression="com.cmzy.spring.annotion.dao.AnnotationDao"/>
	</context:component-scan>

context:component-scan标签还会自动注册AutowireAnnotationBeanProcessor实例,该实例可以自动装配具有@Autowired、@Resource、@Inject注解的属性。
@Autowired注解自动装配具有兼容类型的单个Bean属性
构造器,普通字段(即使是非public),一切具有参数的方法都可以应用@Autowired注解

@Controller
public class AnnotationController {
	//属性中自动注入Bean
	@Autowired
	private AnnotationService service;
	//在构造器中自动注入Bean
	@Autowired
	public AnnotationController(AnnotationService service) {
		super();
		this.service = service;
	}
	//在方法中自动注入Bean
	@Autowired
	public void initServiceInfo(AnnotationService service) {
		this.service = service;
	}

	public void controllerFunction() {
		System.out.println("controller function is exec.......");
		service.serviceFunction();
	}
}

默认情况下,所有使用@Autowired注解的属性都需要被设置,当Spring找不到匹配的Bean装配属性时,会抛出异常,若某一属性允许不被设置,可以设置@Autowired注解的required属性为false。

@Controller
public class AnnotationController {
	//Student没有被Spring容器管理,使用Autowired会报错,必须加上required=false
	@Autowired(required = false)
	private Student student;
	
	@Autowired
	private AnnotationService service;

	public void controllerFunction() {
		System.out.println("controller function is exec.......");
		service.serviceFunction();
	}
}

默认情况下,当IOC容器里面存在多个类型兼容的Bean时,通过类型的自动装配将无法工作,此时可以在@qualifier注解里提供Bean的名称,Spring允许对属性、方法标注@Qualifier来指定注入Bean的名称,但是不能用于构造器。
如下Service下的AnnotationService接口有两个实现类,都添加了Service注解,当controller中使用@Autowired注入Service的时候,就会抛出异常,但是如果将Service注解中添加bean名字@Service(value = “service”),和注入属性名一样,那么就会正常找到
在这里插入图片描述

//service接口
public interface AnnotationService {

	void serviceFunction();
}

//其中一个实现类,使用@Service注解标识
@Service
public class AnnotationServiceTest implements AnnotationService {
	public void serviceFunction() {
		// TODO Auto-generated method stub

	}
}
//其中一个实现类,使用@Service注解标识
@Service
public class AnnotationServiceImpl implements AnnotationService {

	@Autowired
	private AnnotationPojo pojo;
	@Autowired
	private AnnotationDao dao;
	public void serviceFunction() {
		// TODO Auto-generated method stub
		System.out.println("service function is exec..........");
		pojo.setName("张三");
		pojo.setAge(20);
		System.out.println(pojo.toString());
		
		dao.daoFunction();	
	}
}
//controller中注入Service
@Controller
public class AnnotationController {
	
	@Autowired
	private AnnotationService service;

	public void controllerFunction() {
		System.out.println("controller function is exec.......");
		service.serviceFunction();
	}
}

或者如果在Service中没有指定Bean名字与注入属性名相同,可以在注入属性的时候使用@Qualifier注解指定注入哪个Bean,value = "annotationServiceImpl"则是没有指定Bean名字默认以小写字母开头的类名。

@Controller
public class AnnotationController {
	//属性中自动注入Bean
	@Autowired
	@Qualifier(value = "annotationServiceImpl")
	private AnnotationService service;

	public void controllerFunction() {
		System.out.println("controller function is exec.......");
		service.serviceFunction();
	}
}


@Controller
public class AnnotationController {
    
	private AnnotationService service;

	@Autowired
	@Qualifier(value = "annotationServiceImpl")
	public void setService(AnnotationService service) {
		this.service = service;
	}

	public void controllerFunction() {
		System.out.println("controller function is exec.......");
		service.serviceFunction();
	}
}

在方法中,还可以将@Qualifier注解加在参数前面

@Controller
public class AnnotationController {
	
	private AnnotationService service;

	@Autowired
	public void setService(@Qualifier(value = "annotationServiceImpl")AnnotationService service) {
		this.service = service;
	}

	public void controllerFunction() {
		System.out.println("controller function is exec.......");
		service.serviceFunction();
	}
}

@Autowired注解也可以应用在数组类型的属性上,此时Spring将会把所有匹配的Bean进行自动装配。如下Service是数组,则会将之前两个添加@Service注解的Bean都装配进来

@Controller
public class AnnotationController {
	@Autowired
	private AnnotationService[] service;

	public void controllerFunction() {
		System.out.println("controller function is exec.......");
		service[0].serviceFunction();
	}
}

@Autowired注解也应用在集合属性上,此时Spring读取该集合的类型信息,然后自动装配所有与之兼容的Bean
@Autowired注解应用在Map上时,若该Map的键值为String,那么Spring将自动匹配与之map值类型兼容的Bean的名称作为键值。

@Resource、@Inject与@Autowired功能类似,@Resource注解要求提供一个Bean名称的属性,若该属性为空,则自动采用标注处的变量或方法名作为Bean的名称。@Inject和@Autowired注解一样也是按类型匹配注入Bean,但是没有required属性,建议使用@Autowired注解。

<think>好的,我需要帮助用户了解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]。</think>### 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("com.example.service") // 明确限定扫描范围,避免性能浪费 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("localCache") // 明确指定注入localCache private Cache cache; ``` 3. **简化装配的注解** - `@Value`:注入简单类型或配置文件属性。 - `@Resource`:按名称注入(JSR-250标准)。 --- #### 三、最佳实践总结 1. **配置Bean**: - 优先使用组件扫描,但需明确指定包路径(如`@ComponentScan("com.example")`)[^3]。 - 第三方库的Bean使用`@Configuration` + `@Bean`显式配置。 2. **装配Bean**: - 强制依赖使用**构造函数注入**,可选依赖使用Setter注入。 - 多实现时通过`@Qualifier`或`@Primary`消除歧义[^1]。 3. **代码可维护性**: - 避免滥用`@Component`,按层级使用`@Repository`/`@Service`等注解。 - 配置类与业务代码分离,便于管理。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值