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之家的引用关系
//        com.MrChengsb.Annotations.controller.UserController@2925bf5bException 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) 
    }

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

猜你喜欢

转载自www.cnblogs.com/Mrchengs/p/10093120.html