java spring Bean的配置方式之通过注解方式

在 classpath 中扫描组件

  •     组件扫描(component scanning):  Spring 能够从 classpath 下自动扫描, 侦测和实例化具有特定注解的组件. 
  •     特定组件包括:

            @Component: 基本注解, 标识了一个受 Spring 管理的组件
            @Respository: 标识持久层组件
            @Service: 标识服务层(业务层)组件
            @Controller: 标识表现层组件

  •     对于扫描到的组件, Spring 有默认的命名策略: 使用非限定类名, 第一个字母小写. 也可以在注解中通过 value 属性值标识组件的名称

当在组件类上使用了特定的注解之后, 还需要在 Spring 的配置文件中声明 <context:component-scan> :

  •     base-package 属性指定一个需要扫描的基类包,Spring 容器将会扫描这个基类包里及其子包中的所有类. 
  •     当需要扫描多个包时, 可以使用逗号分隔.
  •     如果仅希望扫描特定的类而非基包下的所有类,可使用 resource-pattern 属性过滤特定的类,示例:
<context:component-scan base-package="beans_annotation"
	resource-pattern="repository/*.class">
</context:component-scan>
  •     <context:include-filter> 子节点表示要包含的目标类
  •     <context:exclude-filter> 子节点表示要排除在外的目标类
  •     <context:component-scan> 下可以拥有若干个 <context:include-filter> 和 <context:exclude-filter> 子节点
  • <context:include-filter> 和 <context:exclude-filter> 子节点支持多种类型的过滤表达式:

示例1

建立一个beans_annotation包在beans_annotation下面建立一个TestObject.Java文件

TestObject类

package beans_annotation;

import org.springframework.stereotype.Component;

/**
*@author Danbro
*@version 创建时间:2019年6月28日上午11:48:53
*@funcition 
**/
@Component
public class TestObject {
	
}

在beans_annotation包下面建立一个子包beans_annotation.controller包

建立一个UserController

UserController类

package beans_annotation.controller;

import org.springframework.stereotype.Controller;

/**
*@author Danbro
*@version 创建时间:2019年6月28日上午11:53:55
*@funcition 
**/
@Controller
public class UserController {
	public void execute() {
		System.out.println("UserController execute....");
	}
}

在beans_annotation包下面建立一个子包beans_annotation.service包

建立一个UserService

UserService类

package beans_annotation.service;

import org.springframework.stereotype.Service;

/**
*@author Danbro
*@version 创建时间:2019年6月28日上午11:52:52
*@funcition 
**/
@Service
public class UserService {
	public void add() {
		System.out.println("UserService add....");
	}
}

在beans_annotation包下面建立一个子包beans_annotation.repository包

建立一个UserRepository接口

UserRepository接口

package beans_annotation.repository;
/**
*@author Danbro
*@version 创建时间:2019年6月28日上午11:50:41
*@funcition 
**/
public interface UserRepository {
	void save();
}

建立一个UserRepository接口的实现类UserRepositoryImpl

UserRepositoryImpl类

package beans_annotation.repository;

import org.springframework.stereotype.Repository;

/**
*@author Danbro
*@version 创建时间:2019年6月28日上午11:51:32
*@funcition 
**/
@Repository("userRepository")//可以指定id名
public class UserRepositoryImpl implements UserRepository {

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

}

获得指定包下的所有有注解的bean

Main类(指定IOC容器扫描的包)

package beans_annotation;

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

import beans_annotation.controller.UserController;
import beans_annotation.repository.UserRepository;
import beans_annotation.repository.UserRepositoryImpl;
import beans_annotation.service.UserService;

/**
*@author Danbro
*@version 创建时间:2019年6月28日上午11:59:11
*@funcition 
**/
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 userRepository = (UserRepository) ctx.getBean("userRepository");
		System.out.println(userRepository);

	}
}

bean配置文件 beans_annotation.xml

beans_annotation.xml

	<!-- 指定IOC容器扫描的包 -->
	<context:component-scan base-package="beans_annotation"></context:component-scan>

结果

beans_annotation.TestObject@5c072e3f
beans_annotation.controller.UserController@4d1b0d2a
beans_annotation.service.UserService@954b04f
beans_annotation.repository.UserRepositoryImpl@149494d8

可以通过resource-pattern 指定扫描的资源,获得repository下面所有的class文件

<context:component-scan base-package="beans_annotation"
	resource-pattern="repository/*.class">
</context:component-scan></beans>
public class Main {
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans_annotation.xml");
		UserRepository userRepository = (UserRepository) ctx.getBean("userRepository");
		System.out.println(userRepository);

	}
}

结果

beans_annotation.repository.UserRepositoryImpl@7276c8cd

context:exclude-filter排除哪些指定表达式的组件(排除org.springframework.stereotype.Repository)

	<context:component-scan base-package="beans_annotation">
		 	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
	</context:component-scan>
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);


	}
}

结果

beans_annotation.TestObject@2eda0940
beans_annotation.controller.UserController@3578436e
beans_annotation.service.UserService@706a04ae

context:include-filter 只扫描某些表达式的组件 需要use-default-filters="false

	<context:component-scan base-package="beans_annotation" use-default-filters="false">
			<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
	</context:component-scan>
public class Main {
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans_annotation.xml");
		UserRepository userRepository = (UserRepository) ctx.getBean("userRepository");
		System.out.println(userRepository);

	}
}

结果

beans_annotation.repository.UserRepositoryImpl@10dba097

assignable是接口和此接口的实现,其他使用和annotation一样

 

组件装配

<context:component-scan> 元素还会自动注册 AutowiredAnnotationBeanPostProcessor 实例, 该实例可以自动装配具有 @Autowired 和 @Resource 、@Inject注解的属性.

使用 @Autowired 自动装配 Bean

    @Autowired 注解自动装配具有兼容类型的单个 Bean属性

  •         构造器, 普通字段(即使是非 public), 一切具有参数的方法都可以应用@Authwired 注
  •  

先对以下类进行修改

package beans_annotation.controller;

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

import beans_annotation.service.UserService;

/**
*@author Danbro
*@version 创建时间:2019年6月28日上午11:53:55
*@funcition 
**/
@Controller
public class UserController {
	@Autowired
	private UserService userService;
	public void execute() {
		System.out.println("UserController execute....");
		userService.add();
	}
}
package beans_annotation.service;

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

import beans_annotation.repository.UserRepository;
import beans_annotation.repository.UserRepositoryImpl;

/**
*@author Danbro
*@version 创建时间:2019年6月28日上午11:52:52
*@funcition 
**/
@Service
public class UserService {
	@Autowired
	private UserRepository userRepository;
	public void add() {
		System.out.println("UserService add....");
		userRepository.save();
	}
}
	<context:component-scan base-package="beans_annotation">
	</context:component-scan>
public class Main {
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans_annotation.xml");
		UserController userController = (UserController) ctx.getBean("userController");
		userController.execute();
	}
}

通过调用UserController Bean来调用其他Bean的方法 其中使用@Autowired自动装配

UserController execute....
UserService add....
UserRepository save

除了这种方法 也可以用set方法设置

public class UserController {
	private UserService userService;
	
	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	public void execute() {
		System.out.println("UserController execute....");
		userService.add();
	}
}
  • 默认情况下, 所有使用 @Authwired 注解的属性都需要被设置. 当 Spring 找不到匹配的 Bean 装配属性时, 会抛出异常, 若某一属性允许不被设置, 可以设置 @Authwired 注解的 required 属性为 false

把TestObject类修改下

ackage beans_annotation;

import org.springframework.stereotype.Component;

/**
*@author Danbro
*@version 创建时间:2019年6月28日上午11:48:53
*@funcition 
**/
public class TestObject {
	
}
package beans_annotation.repository;

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

import beans_annotation.TestObject;

/**
*@author Danbro
*@version 创建时间:2019年6月28日上午11:51:32
*@funcition 
**/
@Repository("userRepository")//可以指定id名
public class UserRepositoryImpl implements UserRepository {
	
	@Autowired(required = false)
	private TestObject TestObject;

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

}

结果

UserController execute....
UserService add....
UserRepository save
null

 

 

  •         默认情况下, 当 IOC 容器里存在多个类型兼容的 Bean 时, 通过类型的自动装配将无法工作. 此时可以在 @Qualifier 注解里提供 Bean 的名称. Spring 允许对方法的入参标注 @Qualifiter 已指定注入 Bean 的名称

 

在创建一个JdbcRepositoryImpl

package beans_annotation.repository;

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

import beans_annotation.TestObject;

/**
*@author Danbro
*@version 创建时间:2019年6月28日上午11:51:32
*@funcition 
**/
public class JdbcRepositoryImpl implements UserRepository {
	
	@Override
	public void save() {
		System.out.println("JdbcRepositoryImpl save");
	}

}

此时一个接口有两个实现,在service里装配的时候有两个UserRepository实现,@Qualifiter 已指定注入 Bean 的名称

package beans_annotation.service;

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

import beans_annotation.repository.UserRepository;
import beans_annotation.repository.UserRepositoryImpl;

/**
*@author Danbro
*@version 创建时间:2019年6月28日上午11:52:52
*@funcition 
**/
@Service
public class UserService {
	
//	public void setUserRepository(UserRepository userRepository) {
//		this.userRepository = userRepository;
//	}
	
	@Autowired
	@Qualifier("userRepositoryImpl")
	private UserRepository userRepository;
	
	public void add() {
		System.out.println("UserService add....");
		userRepository.save();
	}
}
  •         @Authwired 注解也可以应用在数组类型的属性上, 此时 Spring 将会把所有匹配的 Bean 进行自动装配.
  •         @Authwired 注解也可以应用在集合属性上, 此时 Spring 读取该集合的类型信息, 然后自动装配所有与之兼容的 Bean. 
  •         @Authwired 注解用在 java.util.Map 上时, 若该 Map 的键值为 String, 那么 Spring 将自动装配与之 Map 值类型兼容的 Bean, 此时 Bean 的名称作为键值
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值