5.spring:注解配置 Bean

在classpath中扫描组件

组键扫描:能够从classpath下自动扫描,侦测和实例化具有特定注解的组件
特定的组件包括:
->@Componment:基于注解,标识一个受Spring管理的组键
->@Respository:标识持久层组件
->@Service:标识服务层
->@controller:标识表现层组件
对于扫描到的组件,Spring有默认的命名策略,使用非限定类名,第一个字母小写,也可以通过注解中value属性值标识组建的名称

在classpath中扫描组键
当在组键类中使用了特定的注解后,还需要再Spring的配置中声明<context:componment-scan>
->base-packge属性是指定一个需要扫描的基类包,Spring容器将会扫描这个基类包里及其他子类包中的所有类
->当需要扫描多个包时,可以使用逗号分隔
->如果仅希望扫描特定类而非集包下的所有类,可使用resource-pattren属性过滤特定的类

通常情况下我们在创建spring项目的时候在xml配置文件中都会配置这个标签,配置完这个标签后,spring就会去自动

扫描base-package对应的路径或者该路径的子包下面的java文件,
如果扫描到文件中带有@Service,@Component,@Repository,@Controller等这些注解的类,则把这些类注册为bean
注:在注解后加上例如@Component(value=”abc”)时,注册的这个类的bean的id就是abc.

TestObject.java
@Component
public class TestObject {
//@Componment:基于注解,标识一个受Spring管理的组键
}
UserController.java
@Controller
public class UserController {
    //建立关联关系
    @Autowired
    private UserService userService;

    public void execute() {
        System.out.println("UserController execute...");
        userService.add();
    }
}

UserRepository.java

public interface UserRepository {
    void save();
}

UserRepositoryImpl.java

@Repository("userRepository")
public class UserRepositoryImpl implements UserRepository {
    @Override
    public void save() {
        System.out.println("UserRepository  save...");
    }
}
UserService.java
@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;
    public void add() {
        // TODO Auto-generated method stub
        System.out.println("UserService add...");
        userRepository.save();
    }
}

applicationContext.xml

<!-- 指定IOC容器扫描的包 -->
<!-- 下面的引入文件,没有进入IOC容器的立马进入IOC容器,有S的标志 -->
<!-- 扫描这个包以及它的子包 -->
<context:component-scan base-package="com.MrChengsb.Annotations" ></context:component-scan>

<!--
    只扫描子包中这个包里的类
    resource-pattern="repository/*.class"

    不包含...
    指定排除那些表达式的组键
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
 -->

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

<!--
     和 context:component-scan 中的 use-default-filters="false" 进行搭配才可以只包括,就是只有包含的这个
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
-->
<!--    只包含      use-default-filters="false" 共同使用
    <context:include-filter type="assignable" expression="com.MrChengsb.Annotations.repository.UserRepository"/>
-->
<!--
    不包含这个接口以及它的实现类
    <context:exclude-filter type="assignable"
    expression="com.MrChengsb.Annotations.repository.UserRepository"/>
-->

测试以及相关注释:

public static void main(String[] args) {

        ApplicationContext ctx = new ClassPathXmlApplicationContext("Annotations.xml");

        //没有设立关联关系的
//        TestObject test = (TestObject) ctx.getBean("testObject");
//        System.out.println(test);
//
//        UserController uc = (UserController) ctx.getBean("userController");
//        System.out.println(uc);
//
//        UserRepositoryImpl urp = (UserRepositoryImpl) ctx.getBean("userRepository");
//        System.out.println(urp);
//
//        UserService use = (UserService) ctx.getBean("userService");
//        System.out.println(use);
//        

        //设立关联关系的
//        TestObject test = (TestObject) ctx.getBean("testObject");
//        System.out.println(test);

        UserController uc = (UserController) ctx.getBean("userController");
        System.out.println(uc);
        uc.execute();

//        UserRepositoryImpl urp = (UserRepositoryImpl) ctx.getBean("userRepository");
//        System.out.println(urp);
//
//        UserService use = (UserService) ctx.getBean("userService");
//        System.out.println(use);

        //没建立Bean之家的引用关系
//        [email protected]ption in thread "main"
//        UserController execute...
//        java.lang.NullPointerException
//            at com.MrChengsb.Annotations.controller.UserController.execute(UserController.java:18)
//            at com.MrChengsb.Annotations.main.main(main.java:37)
    }

测试代码省略.......

原文地址:https://www.cnblogs.com/Mrchengs/p/10093120.html

时间: 2024-08-12 02:32:03

5.spring:注解配置 Bean的相关文章

Spring -- 注解配置Bean

通过注解配置Bean 特定组件包括: @Component: 基本注解, 标识了一个受 Spring 管理的组件 @Respository: 标识持久层组件 @Service: 标识服务层(业务层)组件 @Controller: 标识表现层组件 上面的组件可以混用,因为IOC容器并无法区分当前类是否为业务.持久.还是表现层. 对于扫描到的组件, Spring 有默认的命名策略: 使用非限定类名, 第一个字母小写. 也可以在注解中通过 value 属性值标识组件的名称 使用注解配置Bean前,我们

Spring IOC机制之使用注解配置bean

一. 通过注解配置bean 1.1       概述 相对于XML方式而言,通过注解的方式配置bean更加简洁和优雅,而且和MVC组件化开发的理念十分契合,是开发中常用的使用方式. 1.2       使用注解标识组件 ①普通组件:@Component:标识一个受Spring IOC容器管理的组件 ②持久化层组件:@Respository:标识一个受Spring IOC容器管理的持久化层组件 ③业务逻辑层组件:@Service:标识一个受Spring IOC容器管理的业务逻辑层组件 ④表述层控制

14Spring通过注解配置Bean(2)

下面将对13Spring通过注解配置Bean(1)的中Repository.Service.Controller通过注解方式来建立关联. <context:component-scan>元素还会自动注册AutowiredAnnotationBeanPostProcessor后置处理器实例,该实例可以自动装配具有@Autowired属性. @Autowired注解会自动装配具有兼容类型的单个Bean属性 ——构造器,普通字段(即使是非public),一切具有参数的方法都可以应用@Autowire

Spring4学习笔记-通过注解配置bean

通过注解配置Bean TestObject.java package com.spring.beans.annotation; import org.springframework.stereotype.Component;; @Component public class TestObject { } UserController.java package com.spring.beans.annotation.controller; import org.springframework.st

spring注解配置quartz应用

项目中会经常用到定时器,因此,其quartz的使用,我们必须要掌握.下面就以例子来讲解如何在spring中整合quartz, 使用注解配置的方式来实现定时执行任务. 一.引入jar包 项目中需引入quartz的jar包,由于整合到spring中,肯定也引入了spring的相关jar包. 例子中引用的是quartz-2.1.1版本,使用maven管理项目,pom文件需引入quartz依赖 二.spring配置文件中配置 (applicationContext.xml) 1)      xmlns和

13Spring通过注解配置Bean(1)

配置Bean的形式:基于XML文件的方式:基于注解的方式(基于注解配置Bean:基于注解来装配Bean的属性) 下面介绍基于注解的方式来配置Bean. ——组件扫描(component scanning):Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. ——特定组件包括: [email protected]:基本注解,标识了一个受Spring管理的组件 [email protected]:标识持久层组件 [email protected]:标识服务层(业务层)组

关于Spring注解配置的步骤

今天分享一下 关于Spring注解配置的流程 1 导包:如下图所示 2 书写User和Car类  代码如下 package cn.lijun.bean; public class Car { private String name; private String color; public String getName() { return name; } public void setName(String name) { this.name = name; } public String g

spring注解配置quartz

常规配置quartz可以参考我的另外一篇博文:http://www.cnblogs.com/yangzhilong/p/3349116.html spring配置文件里增加: 命令空间: http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd 配置: <task:annotation-driven/> 当然这还需要扫描注解等常规配置. ja

spring FactoryBean配置Bean

概要: 实例代码详解: 目录结构 Car.java package com.coslay.beans.factorybean; public class Car { private String brand; private double price; public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public double getPric