我爱编程

Spring | 基于注解配置Bean(一)

2018-06-28  本文已影响1人  EclipseO2

Spring 能够从 classpath 下自动扫描,侦测具有特定注解的组件,组件包括

命名政策:第一个字母小写,也可以在注解中通过 value 属性标识组件名称

//接口层
public interface UserRepository {
    public void save();
}

//实现层
@Repository("userRepository")
public class UserRepositoryImpl implements UserRepository {

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

}

//业务层
@Service
public class UserService {
        
    public void add() {
        System.out.println("UserService add ... ");
    }

}

//控制层
@Controller
public class UserController {

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

Spring 默认情况下,注解的名字为类名的第一个字母小写,如 testObject

@Component
public class TestObject {}



注解的值也可以自己设定,后面使用 getBean() 调用的时候也可以使用自己设置的值。如以下默认注解名为 userRepositoryImpl,可以使用 value 属性标识注解名,@Repository("userRepository")

@Repository()
public class UserRepositoryImpl implements UserRepository {

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

}

getBean() 方法中的参数,默认为注解后的第一个字母小写的类名,也可以使用自己用 value 标识的注解名

public static void main(String[] args) {
        
    ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-annotation.xml");
        
    TestObject o = (TestObject) ctx.getBean("testObject");
    System.out.println(o);
        
    UserController userController = (UserController) ctx.getBean("userController");
    userController.execute();
        
    UserRepository userRepository = (UserRepository) ctx.getBean("userRepository");
    System.out.println(userRepository);
        
    UserService userService = (UserService) ctx.getBean("userService");
    System.out.println(userService);
}
<context:component-scan base-package="edu.just.spring.beans.annotation"></context:component-scan>

edu.just.spring.beans.annotation 包及子包下扫描,此时该包下所有注解过的类都能执行

<context:component-scan base-package="edu.just.spring.beans.annotation"
        resource-pattern="repository/*.class"></context:component-scan>

此配置标识只能执行 edu.just.spring.beans.annotation.repository 子包下的 Bean 的方法

<context:component-scan base-package="edu.just.spring.beans.annotation">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>

使用注解名进行排除,排除指定子包下注解为 Repository 的目标类

<context:component-scan base-package="edu.just.spring.beans.annotation">
        <context:exclude-filter type="assignable" expression="edu.just.spring.beans.annotation.repository.UserRepository"/>
</context:component-scan>

使用接口名进行排除,排除指定子包下接口名为 UserRepository 的目标类

<context:component-scan base-package="edu.just.spring.beans.annotation" use-default-filters="false">
  <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>

使用注解名进行包含,此时只能执行标识为 Repository 的类

<context:component-scan base-package="edu.just.spring.beans.annotation" use-default-filters="false">
    <context:include-filter type="assignable" expression="edu.just.spring.beans.annotation.repository.UserRepository"/>
</context:component-scan>

使用接口名字进行包含,此时只能包含接口名为 UserRepository 的目标类

上一篇 下一篇

猜你喜欢

热点阅读