Spring框架:第六章:注解功能

8.1、注解配置Dao、Service、Controller组件
实验32:通过注解分别创建Dao、Service、Controller★
Spring配置bean的常用注解有
@Controller 配置web层的组件
@Service 配置Service层的组件
@Repository 配置Dao层的组件
@Component 除了web层、service层、dao层之外的组件。
@Scope 配置作用域

bean对象

/**
 *  @Component 等价于<br/>
 *  <bean id="book" class="com.pojo.Book" />
 */
@Component
public class Book {}

/**
 *  @Repository 等价于<br/>
 *  <bean id="bookDao" class="com.dao.BookDao" />
 */
@Repository
public class BookDao {}

/**
 *  @Service 等价于<br/>
 *  <bean id="bookService" class="com.service.BookService" />
 */
@Service
public class BookService {}

/**
 *  @Controller 等价于<br/>
 *  <bean id="bookServlet" class="com.controller.BookServlet" />
 */
@Controller
public class BookServlet {}

   

8.2、指定扫描包时的过滤内容
实验33:使用context:include-filter指定扫描包时要包含的类
实验34:使用context:exclude-filter指定扫描包时不包含的类
<context:include-filter /> 设置包含的内容
注意:通常需要与use-default-filters属性配合使用才能够达到“仅包含某些组件”这样的效果。即:通过将use-default-filters属性设置为false,
<context:exclude-filter />设置排除的内容

annotation com.XxxAnnotation
过滤所有标注了XxxAnnotation的类。这个规则根据目标组件是否标注了指定类型的注解进行过滤
assignable com.BaseXxx
过滤所有BaseXxx类的子类。这个规则根据目标组件是否是指定类型的子类的方式进行过滤。

applicationContext.xml 中配置的内容如下

<!-- use-default-filters="false" 设置取消默认包含规则 -->
<context:component-scan base-package="com" use-default-filters="false">
    <!-- context:include-filter 设置包含的内容 -->
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    <!-- context:exclude-filter 设置排除的内容 -->
    <context:exclude-filter type="assignable" expression="com.atguigu.service.BookService"/>
</context:component-scan>

 

以上配置会包含所有@Service注解的类。排除com.service.BookService类

包扫描的排除示例:

<!--
    context:component-scan 包扫描
        base-package 设置你要扫描的包,以子包
        base-package="com" 表示Spring会扫描com包,以及它所有子包都会扫描
 -->
<context:component-scan base-package="com">
    <!--
        context:exclude-filter 排除某些类
            type="annotation"表示按注解进行排除
            expression 按哪些注解
     -->
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    <!-- 表示按类进行过滤,排除 -->
    <context:exclude-filter type="assignable" expression="com.dao.BookDao"/>
</context:component-scan>

  

包扫描的包含示例:

<!--
    context:component-scan 包扫描
        base-package 设置你要扫描的包,以子包
        base-package="com" 表示Spring会扫描com包,以及它所有子包都会扫描
        
    use-default-filters="false"禁用默认的包扫描包含规则
    包扫描自定义包含规则一定要结合 use-default-filters="false" 一起使用
 -->
<context:component-scan base-package="com" use-default-filters="false">        
    <!--
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
        按照注解的形式配置包含扫描规则
     -->
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    <context:include-filter type="assignable" expression="com.atguigu.dao.BookDao"/>
</context:component-scan>

 

8.3、使用注解@Autowired自动装配
实验35:使用@Autowired注解实现根据类型实现自动装配★

@Autowired 注解 会自动的根据标注的对象类型在Spring容器中查找相对应的类。如果找到,就自动装配。
使用@Autowired注解,不需要get/set方法

public class BookService {
/**
* @Autowired 配置自动注入
* 1、先按类型进行查找然后注入。
*/
@Autowired
private BookDao bookDao;

@Override
public String toString() {
    return "BookService [bookDao=" + bookDao + "]";
}

 

}

8.4、多个同类型的bean如何自动装配实验36:如果资源类型的bean不止一个,默认根据@Autowired注解标记的成员变量名作为id查找bean,进行装配★

 

 

更多内容请见原文,原文转载自:https://blog.csdn.net/weixin_44519496/article/details/120343257

 

posted @ 2022-09-21 20:08  忘川信使  阅读(23)  评论(0编辑  收藏  举报