概要:
![](http://img.blog.csdn.net/20140603224957734)
再classpath中扫描组件
- 组件扫描(component scanning):Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件
-
特定组件包括:
- @Component:基本注解,标示了一个受Spring管理的组件(可以混用,spring还无法识别具体是哪一层)
- @Respository:建议标识持久层组件(可以混用,spring还无法识别具体是哪一层)
- @Service:建议标识服务层(业务层)组件(可以混用,spring还无法识别具体是哪一层)
- @Controller:建议标识表现层组件(可以混用,spring还无法识别具体是哪一层)
- 对于扫描到的组件,Spring有默认的命名策略:使用非限定类名,第一个字母小写(UserServiceImpl-》userServiceImpl),也可以再注解中通过value属性值标识组件的名称(通常可以将UserServiceImpl —》userService,可以将Impl拿掉,这是一个习惯)
在classpath中扫描组件
-
当在组件类中使用了特定的注解之后,还需要在Spring的配置文件中声明<context:component-scan>:
- base-package属性指定一个需要扫描的基类包,Spring容器将会扫描整个基类包里及其子包中的所有类
- 当需要扫描多个包时,可以使用逗号分隔
- 如果仅希望扫描特定的类而非基包下的所有类,可使用resource-pattern属性过滤特定的类,实例:
![](http://img.blog.csdn.net/20140603230701468)
- <context:include-filter>子节点表示要包含的目标类
- <context:exclude-filter>子节点表示要排除在外的目标类
- <context:component-scan>下可以拥有若干个<context:include-filter>和<context:exclude-filter>子节点
- <context:include-filter>和<context:exclude-filter>子节点支持多种类型的过滤表达式:
![](http://img.blog.csdn.net/20140603235753984)
组件装配
- <context:component-sacn>元素还会自动注册AutowireAnnotationBeanPostProcessor实例,该实例可以自动装配具有@Autowired和@Resource、@Inject注解的属性
使用@Autowired自动装配Bean
-
@Autowired注解自动装配具有兼容类型的单个Bean属性
- 可以放在构造器或普通字段(即使是非public)或一切具有参数的方法都可以应用@Authwired注解
- 默认情况下,所有使用@Autowired注解的属性都需要被设置,当Spring找不到匹配的Bean装配属性时,会抛出异常,若某一属性允许不被设置,可以设置@Authwired注解的required属性为false
- 默认情况下,当IOC容器里存在多个类型兼容的Bean时(@Autowired先是按照类型匹配Bean,如果存在多个类型相同的Bean,此时IOC容器会去寻找与属性名相同名字的Bean),通过类型的自动装配将无法工作,此时可以在@Qualifier注解里Bean属性的名称。Spring允许对方法的方法的输入参数标注@Qualifier以指定注入Bean的名称
- @Authwired注解也可以应用在数组类型的属性上,此时Spring将会把所有匹配的Bean进行自动装配
- @Authwired注解也可以应用在集合属性上,此时Spring读取该集合的类型信息,然后自动装配所有与之兼容的Bean
- @Authwired注解用在java.util.Map上时,若该Map的键值为String,那么Spring将会自动装配与之Map值类型兼容的Bean,此时Bean的名称作为键值
使用@Resource或@Inject自动装配Bean
- Spring还支持@Resource和@Inject注解,这两个注解和@Autowired注解的功能类似
- @Resource注解要求提供一个Bean名称的属性,若该属性为空,则自动采用标注处的变量或方法名作为Bean的名称
- @Inject和@Autowired注解一样也是按照类型匹配注入的Bean,但没有required属性
- 建议使用@Autowired注解
实例代码详解:
代码结构
注意:使用注解还需要导入spring-aop-4.0.5.RELEAE.jar这个包
TestObject.java
package com.coslay.beans.annotation;
import org.springframework.stereotype.Component;
public class TestObject {
}
UserController.java
package com.coslay.beans.annotation.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.coslay.beans.annotation.service.UserService;
@Controller
public class UserController {
@Autowired
private UserService userService;
public void execute(){
System.out.println("UserController execute...");
userService.add();
}
}
UserService.java
package com.coslay.beans.annotation.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import com.coslay.beans.annotation.reporsitory.UserRepository;
@Service
public class UserService {
private UserRepository userRepository;
//如果有多个类型匹配时,则会去匹配与这个属性名字相同的Bean的名字
//@Repository("userRepository")
//public class UserRepositoryImpl implements UserRepository{
//因为名字相同所以匹配成功
//可以将@Autowired放在字段或者方法上
//如果没有指定名字
//还有一种方法是使用@Qualifier("userRepositoryImpl")
//用来指定哪一个指定名字的Bean
//可以将该标签放在字段,设置方法或者设置方法的传入参数前面
@Autowired
@Qualifier("userRepositoryImpl")
public void setUserRepository(UserRepository userRepository){
this.userRepository = userRepository;
}
// @Autowired
// public void setUserRepository(@Qualifier("userRepositoryImpl") UserRepository userRepository){
// this.userRepository = userRepository;
// }
public void add(){
System.out.println("UserService add...");
userRepository.sava();
}
}
UserRepository.java
package com.coslay.beans.annotation.reporsitory;
public interface UserRepository {
void sava();
}
UserRepositoryImpl.java
package com.coslay.beans.annotation.reporsitory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.coslay.beans.annotation.TestObject;
@Repository("userRepository")
public class UserRepositoryImpl implements UserRepository{
@Autowired(required=false)
private TestObject testObject;
@Override
public void sava() {
System.out.println("UserRepository Save...");
System.out.println(testObject);
}
}
UserJdbcRepository.java
package com.coslay.beans.annotation.reporsitory;
import org.springframework.stereotype.Repository;
@Repository
public class UserJdbcRepository implements UserRepository {
@Override
public void sava() {
System.out.println("UserJdbcRepository sava...");
}
}
Main.java
package com.coslay.beans.annotation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.coslay.beans.annotation.controller.UserController;
import com.coslay.beans.annotation.reporsitory.UserRepository;
import com.coslay.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);
userController.execute();
//
// UserService userService = (UserService) ctx.getBean("userService");
// System.out.println(userService);
//
// UserRepository userRepository = (UserRepository) ctx.getBean("userRepository");
// System.out.println(userRepository);
}
}
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 容器扫描的包 -->
<!-- 可以通过resource-pattern指定扫描的资源 -->
<!-- <context:component-scan base-package="com.coslay.beans.annotation"
resource-pattern="reporsitory/*.class">
</context:component-scan> -->
<!--
context:exclude-filter 子节点指定排除哪些指定表达式的组件
context:include-filter 子节点指定包含哪些表达式的组件,该子节点需要use-default-filters="false"配合使用(如果use-default-filters="ture"则使用系统默认扫描所有带有@Component@Controller@Service@Repository的组件)
-->
<context:component-scan base-package="com.coslay.beans.annotation">
<!--
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Repository"/>
-->
<!--
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Repository"/>
-->
<!--
<context:exclude-filter type="assignable"
expression="com.coslay.beans.annotation.reporsitory.UserRepository"/>
-->
<!--
<context:include-filter type="assignable"
expression="com.coslay.beans.annotation.reporsitory.UserRepository"/>
-->
</context:component-scan>
</beans>
分享到:
相关推荐
自定义注解得使用,模拟spring通过注解方式创建bean实例!
在非spring注解类中使用spring容器中的bean_普通类中使用yml配置文件中的配置信息,在大数据采集项目中用到的,已经测试过了
浅尝Spring注解开发_Bean生命周期及执行过程.doc
spring2.5向spring3过渡,必学零配置注解实现Bean定义。
spring注解开发使用教程--Bean加载控制(二)案例代码
IOC之基于注解的配置bean(上) ,具体效果和过程看博文 http://blog.csdn.net/evankaka/article/category/3073841
IOC之基于注解的配置bean(下),具体效果和过程看博文 http://blog.csdn.net/evankaka/article/details/45077041
spring3零配置注解实现Bean定义(包括JSR-250、JSR-330)
Bean、通过 FactoryBean 配置 Bean、通过注解配置 Bean、泛型依赖注入、AOP 基础、前置通知、后置通知、返回通知、异常通知、环绕通知、切面的优先级、切点表达式、使用 XML 文件的方式配置 AOP、使用 JdbcTemplate ...
获取springbean对象
Spring bean 一般通过配置文件和注解进行加载,如果要实现jar或class文件,动态实现spring bean 的动态加载,并通过UrlClassLoader完成jar和class文件的加载。可以实现jar的热替换。spring的bean动态加载则需要对...
主要介绍了Java之Spring注解配置bean实例代码解析,具有一定参考价值,文中还有有关Spring学习的资料下载链接及相关内容推荐,需要的朋友可以了解下。
在 Spring 2.5 中,Spring MVC 框架引入了注解驱动功能,使得开发者可以使用注解来配置 Controller,代替传统的基于 XML 的配置方式。这种方式使得 Controller 的开发变得更加灵活和方便。 使用 @Controller 注解 ...
通过@Bean 和spring的factoryBean注入的bean. 以及对应@service注解注入的类 通过@Autowired 注入对象的时间是现根据类型在根据beanName获取的案例集合
3、通过spring的xml bean配置或bean注解指定初始化方法,如下面实例的initMethod方法通过@bean注解指定。 销毁的时候实现的方法 1、通过java提供的@PreDestroy注释; 2、通过实现spring提供的DisposableBean接口,并...
自定义注解加载Bean是Spring框架提供的一个扩展点。基于这个扩展点可以实现灵活加载Bean的功能。 例如 Dubbo框架通过这个扩展点将添加了自定义注解@org.apache.dubbo.config.annotation.Service和@org.apache.dubbo
在线程中获取spring注入对象,如:数据库连接对象,DAO,Service ,资源文件等
大家都知道spring有多重配置方式,基于XML,基于注解,基于java类的配置,其中基于XML是最强大的一种,下面这篇文章主要给大家介绍了关于Spring装配Bean之XML安装配置bean的相关资料,需要的朋友可以参考借鉴,下面...
主要介绍了Spring如何使用注解的方式创建bean,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下