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

配置Bean的形式:基于XML文件的方式;基于注解的方式(基于注解配置Bean;基于注解来装配Bean的属性)

下面介绍基于注解的方式来配置Bean。

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

——特定组件包括:

  [email protected]:基本注解,标识了一个受Spring管理的组件

  [email protected]:标识持久层组件

  [email protected]:标识服务层(业务层)组件

  [email protected]:标识表现层组件

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

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

——base-package属性指定一个需要扫描的基类包,Spring容器将会扫描这个基类包里及其子包中的所有类。

——当需要扫描多个包时,可以使用逗号分隔

——如果仅希望扫描特定的类而非基包下的所有类,可适用resource-pattern属性过滤特定的类,实例:

  <context:component-scan base-package=“com.spring.beans” resource-pattern="autowire/*.class"/>

——<context:include-filter>子节点表示要包含的目标类

——<context:exclude-filter>子节点表示要排除在外的目标类

——<context:component-scan>下可以拥有若干个<context:include-filter>和<context:exclude-filter>子节点

package annotation;

import org.springframework.stereotype.Component;

@Component
public class TestObject {

}
package annotation.repository;

public interface UserRepository {
    void save();
}
package annotation.repository;

import org.springframework.stereotype.Repository;

@Repository(value = "userRepository")
public class UserRepositoryImpl implements UserRepository {
    @Override
    public void save() {
        System.out.println("UserRepositoryImpl save");
    }
}
package annotation.controller;

import org.springframework.stereotype.Controller;

@Controller
public class UserController {
    public void execute() {
        System.out.println("UserController execute");
    }
}
package annotation.service;

import org.springframework.stereotype.Service;

@Service
public class UserService {
    public void add() {
        System.out.println("UserService add");
    }
}
<?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.xsd">
    <!--指定Spring IOC容器扫描的包-->
    <!--<context:component-scan base-package="annotation">
    </context:component-scan>-->

    <!--可以通过resource-pattern指定扫描的资源-->
    <!--<context:component-scan base-package="annotation"
                            resource-pattern="repository/*.class">
    </context:component-scan>-->
    <!--context:exclude-filter 子节点指定排除哪些指定表达式的组件-->
    <!--context:include-filter 子节点指定包含指定表达式的组件,该子节点需要use-default-filters 配个使用-->
    <context:component-scan base-package="annotation">
        <!--annotation表示根据指定注解来包含还是不包含-->
        <!--<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"></context:exclude-filter>-->
        <!--<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"></context:include-filter>-->
        <!--assignable根据指定类名来包含还是不包含-->
       <!-- <context:exclude-filter type="assignable" expression="annotation.repository.UserRepository"></context:exclude-filter>-->
        <!--<context:include-filter type="assignable" expression="annotation.repository.UserRepository"></context:include-filter>-->
    </context:component-scan>
</beans>
package annotation;
import annotation.controller.UserController;
import annotation.repository.UserRepository;
import annotation.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("13-1.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);
    }
}
时间: 2024-07-30 16:31:28

13Spring通过注解配置Bean(1)的相关文章

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 -- 注解配置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容器管理的业务逻辑层组件 ④表述层控制

Spring通过注解配置Bean

@Component: 基本注解, 标识了一个受 Spring 管理的组件@Repository: 标识持久层组件@Service: 标识服务层(业务层)组件@Controller: 标识表现层组件 配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=&qu

5.spring:注解配置 Bean

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

Spring_通过注解配置 Bean(2)

Spring_通过注解配置 Bean(1)

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.spr

Spring--通过注解来配置bean【转】

Spring通过注解配置bean 基于注解配置bean 基于注解来配置bean的属性 在classpath中扫描组件 组件扫描(component scanning):Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定的组件包括: [email protected]:基本注解,标识了一个受Spring管理的组件 [email protected]:标识持久层组件 [email protected]:标识服务层(业务层)组件 [email protected]: