spring通过注解配置Bean

spring通过注解配置Bean


1.Spring通过基于注解配置Bean

在classpath中扫描组件:

组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件。
特定组件包括:
--@Compoent:基本注解,标识了一个受Spring管理的组件
--@Repository: 标识持久层组件
--@Service:标识服务层(业务层)组件
--@Controller:标识表现层组件
(spring目前是不区分特定组件的,即特定组件对其来说都是一样的,但为了编程规范,最好使用合适组件)

对于扫描到的组件,Spring有默认的命名策略:使用非限定类名,第一个字母小写,也可以在注解中通过value属性值标识组件的名称。
即在获取该实例时:例如是UserService的类,引用bean的id为userService
TestObject to  =  (TestObject) ctx.getBean("testObject");

注意:在spring配置文件中:使用context标签需要导入context的命名空间(xmlns:context="http://www.springframework.org/schema/context")

指定spring IOC 容器扫描的包:

<context:component-scan base-package="com.wul.spring.beans.annotation">
</context:component-scan>


通过reource-pattern指定扫描的资源:

base-package属性指定一个需要扫面的基类包,Spring容器将会扫描这个基类包里及其子包中的所有类。
当需要扫描多个包时,可以使用多个逗号分隔
如果仅希望扫描特定的类而非基包下的所有类,可使用resource-pattern属性过滤特定的类,示例:
<context:component-scan 
		base-package="com.wul.spring.beans.annotation"
	         resource-pattern="repository/*.class" >
</context:component-scan>
只能得到repository组件下的类


<context:component-scan>的子节点:

<context:component-scan>下可以拥有若干个<context:include-filter>和-<context:exclude-filter>子节点
1.<context:include-filter>子节点表示要包含的目标类
2.<context:exclude-filter>子节点表示要排除在外的目标类( 该字节点需要将use-default-filters(使用默认过滤器)置为false

<context:include-filter>和<context:exclude-filter>子节点支持多种类型的过滤表达式:


	<!-- context:exclude-filter子节点指定排除哪些指定表达式的组件 -->
	<context:component-scan base-package="com.wul.spring.beans.annotation" >	
	    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/> 
	</context:component-scan>

	<!-- context:include-filter子节点指定包含哪些指定表达式的组件,该字节点需要将use-default-filters(使用默认过滤器)置为false -->
	<context:component-scan base-package="com.wul.spring.beans.annotation" use-default-filters="false">	
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
	</context:component-scan>



2如何利用注解配置bean之间的关联关系

组件装配
<context:component-scan>元素还会 自动注册 AutowireAnnotationBeanPostProcessor实例(bean 后置处理器),
该实例可以自动装配具有@Autowired和@Resource,@Inject注解的属性

使用@Autowired自动装配Bean

@Autowired注解自动装配具有兼容类型的的单个Bean属性
 
1.  构造器, 普通字段(即使是public), 一切只有参数的方法 都可以应用@Autowired
   
2. 默认情况下,所有使用@Autowired注解的属性都需要被设置,当spring找不到匹配的Bean装配属性时,会抛出异常,
   若某一属性允许不被设置,可以设置@Autowired注解的required属性为false

3.默认情况下,当IOC容器里存在多个类型兼容的bean时,通过类型的自动装配将无法工作,此时
       (1)@Repository("userRepository")
       (2)可以在@Qualifter注解里提供Bean的名称(Spring允许对方法的入参标注@Qualifier已指定注入Bean的名称)。
     
4.@Autowired注解也可以应用在数组类型的属性上,此时Spring将会把所有匹配的Bean进行自动装配。
5.@Autowired注解也可以应用在集合属性上,此时Spring读取该集合的类型信息,然后自动装配所有与之兼容的Bean
6.@Autowired注解用在java.util.Map上时,若该Map的键值为String,那么Spring将自动装配与之Map值类型兼容的Bean,此时Bean的名称作为键值。

使用@Resource或@Inject自动装配Bean

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

下面给出示例:


TestObject.java
package com.wul.spring.beans.annotation;

import org.springframework.stereotype.Component;

@Component
public class TestObject {
	
	
	
}
TestObject2.java
package com.wul.spring.beans.annotation;

public class TestObject2 {
	

}
UserController.java
package com.wul.spring.beans.annotation.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.wul.spring.beans.annotation.service.UserService;

@Controller
public class UserController {
	
	@Autowired
	private UserService userService;
	
	public void execute(){
		System.out.println("UserController execute...");
		userService.add();
	}
}
UserRepository.java
package com.wul.spring.beans.annotation.repository;

public interface UserRepository {
	
	void save();
	
}

UserRepositoryImpl.java
package com.wul.spring.beans.annotation.repository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


import com.wul.spring.beans.annotation.TestObject2;

@Repository("userRepository")
public class UserRepositoryImpl implements UserRepository{
	
	@Autowired(required=false)
	private TestObject2 testObject2;

	
	@Override
	public void save() {
		System.out.println("UserRepositoryImpl save...");
	}

}

UserJdbcRepositoryImpl.java
package com.wul.spring.beans.annotation.repository;

import org.springframework.stereotype.Repository;


@Repository
public class UserJdbcRepositoryImpl implements UserRepository{

	@Override
	public void save() {
		System.out.println("UserJdbcRepositoryImpl save...");
	}

}
UserService.java

package com.wul.spring.beans.annotation.service;

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

import com.wul.spring.beans.annotation.repository.UserRepository;

@Service
public class UserService {
	
	private UserRepository userRepository;
	
	@Autowired
	//@Qualifier("userRepositoryImpl")
	private void setRepository(UserRepository userRepository) {
		this.userRepository = userRepository;

	}
	
	
	public void add(){
		System.out.println("UserService add...");
		userRepository.save();
	}
}
beans-annotation.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-4.0.xsd">
	
	<!-- 指定spring IOC 容器扫描的包 -->
	<context:component-scan base-package="com.wul.spring.beans.annotation">
	</context:component-scan>
	
	
	<!-- 可以通过reource-pattern指定扫描的资源 -->
	<!-- <context:component-scan 
		base-package="com.wul.spring.beans.annotation"
	    resource-pattern="repository/*.class" ></context:component-scan> -->
	    
	<!-- context:exclude-filter子节点指定排除哪些指定表达式的组件 -->
<!-- 	<context:component-scan base-package="com.wul.spring.beans.annotation" >	
	    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/> 
	</context:component-scan> -->
	
	<!-- context:include-filter子节点指定包含哪些指定表达式的组件,该字节点需要将use-default-filters(使用默认过滤器)置为false -->
<!-- 	<context:component-scan base-package="com.wul.spring.beans.annotation" use-default-filters="false">	
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
	</context:component-scan> -->

</beans>
	
Main.java
package com.wul.spring.beans.annotation;

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

import com.wul.spring.beans.annotation.controller.UserController;
import com.wul.spring.beans.annotation.repository.UserRepository;
import com.wul.spring.beans.annotation.service.UserService;

public class Main {
	
	public static void main(String[] args) {
		
		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);
//		
//		UserService userService = (UserService) ctx.getBean("userService");
//		System.out.println(userService);
		
//		UserRepository userReponsitory = (UserRepository) ctx.getBean("userReponsitory");
//		System.out.println(userReponsitory);
		
		//bean之间的关联关系
		UserController userController = (UserController) ctx.getBean("userController");
		System.out.println(userController);
		userController.execute();
		
	}
}












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值