微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

spring之通过注解方式配置Bean一

(1)组件扫描:spring能够从classpath下自动扫描、侦测和实例化具有特定注解的组件。

(2)特定组件包括

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

(3)对于扫描到的组件,spring有认的命名规则:使用非限定类名。第一个字母小写,也可以在注解中通过value属性值标识组件的名称

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

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

一、所需环境

引入以下jar包,加入到build path中:

两个连接数据库的,五个spring核心包,aop是额外的使用注解方式需要引入的包,一定要记得引入这个包。

基本目录如下:

二、基本配置

TestObjec.java

package com.gong.spring.beans.annotation;

import org.springframework.stereotype.Component;

@Component
public class TestObject {

}

UserController.java

 com.gong.spring.beans.annotation.controller;

 org.springframework.stereotype.Controller;

@Controller
 UserController {
    void execute() {
        System.out.println("UserController的execute方法");
    }
}

UserRepository.java

 com.gong.spring.beans.annotation.repository;

interface UserRepository {
     save();
}

UserRepositoryImpl.java

 org.springframework.stereotype.Repository;

//可以指定使用时的名字
@Repository(value="userRepository")
class UserRepositoryImpl implements UserRepository{

    @Override
     save() {
         Todo Auto-generated method stub
        System.out.println("UserReposityImpl的save方法");
    }

}

UserService.java

 com.gong.spring.beans.annotation.service;

 org.springframework.stereotype.Service;

@Service
 UserService {
     add() {
        System.out.println("UserService中的add方法");
    }
}

Main.java

 org.springframework.context.ApplicationContext;
 org.springframework.context.support.ClasspathXmlApplicationContext;

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

 Main {
    static  main(String[] args) {
        1.创建spring的IOC容器对象
        ApplicationContext ctx = new ClasspathXmlApplicationContext("beans-annotation.xml");
        2.从容器中获取Bean实例
        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);
    }
    
}

 beans-annotation.xml(需要引入context命名空间)

<?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.3.xsd">
        
    <!-- 配置springIOC容器扫描的包 -->    
    context:component-scan base-package="com.gong.spring.beans.annotation"></context:component-scan>

</beans>

输出

说明这些带有注解的类已经被spring所识别并被IOC容器所管理。需要注意的是,认情况下获取bean的实例时,名字是类名,但首字母是小写。例如TestObject通过getBean("testObject")来获取,当然,也可以在注解时使用value属性来声明bean的名字。

三、子节点配置

再来看:

    ="com.gong.spring.beans.annotation"
    resource-pattern="repository/*.class">

指定扫描的模式之后,我们就只能访问到repository下面的组件了,即输出

=

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

  • annotation:所有标注了XxxAnnotation的类;
  • assignable:所有继承或扩展XxxService的类;
  • aspectj
  • regex
  • custom

(1)使用annotation的方式

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

不包含org.springframework.stereotype.Repository,即输出

    
    use-default-filters="false"context:include-filter >

只包含org.springframework.stereotype.Repository,注意需要配置use-default-filters为false,即输出

(2)使用assignable方式

    ="assignable"="com.gong.spring.beans.annotation.repository.UserRepository">

不包含UserRepository接口及其实现类的。

    >

设置use-default-filters="false"。只包含UserRepository接口及其实现类的。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐