zoukankan      html  css  js  c++  java
  • Spring注解配置

    配置文件:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
     4     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     5         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
     6     
     7 
     8     <!--context:component-scan 指定 扫描的包 -->
     9     <!--可以通过 resource-pattern 指定扫描的资源, resource-pattern="myrepository/*.class" 
    10         的含义: 只扫描 base-package 对应包下的 目录为 myrepository 的所有java Bean -->
    11     <!-- <context:component-scan base-package="imooc_spring.test.anotation" 
    12         resource-pattern="myrepository/*.class"></context:component-scan> -->
    13 
    14     <!-- 
    15         context:exclude-filter type="annotation"
    16             expression="org.springframework.stereotype.Repository" 
    17             子节点指定排除哪些注解
    18         context:include-filter type="annotation"    需要结合context:component-scan 标签的
    19         use-default-filters="false"来使用
    20             
    21         context:exclude-filter type="assignable"
    22             这个expression指的是自己写的类,意思排除哪些类
    23             expression="imooc_spring.test.anotation.TestObj"
    24      -->
    25     <context:component-scan base-package="imooc_spring.test.anotation" >
    26         <!-- <context:exclude-filter type="annotation"
    27             expression="org.springframework.stereotype.Repository" /> -->
    28             
    29         <context:exclude-filter type="assignable"
    30             expression="imooc_spring.test.anotation.TestObj" />
    31     </context:component-scan>
    32 </beans>

    @Repository注解:

     1 package imooc_spring.test.anotation.myrepository;
     2 
     3 import org.springframework.stereotype.Repository;
     4 
     5 /**
     6  * 指定id,默认为dAO,即类名首字母小写,如果指定了名称那么只能ctx.getBean(指定名称)来获取bean
     7  * 这个例子里就只能通过ctx.getBean("wyldao)来获取DAO 的实例了;
     8  * 
     9  * @author Wei
    10  */
    11 @Repository("wyldao")
    12 public class DAO {
    13     /**
    14      * 返回x和y的乘积
    15      * 
    16      * @param x
    17      * @param y
    18      * @return x*y
    19      */
    20     public int multi(int x, int y) {
    21         return x * y;
    22     }
    23 }

    @Component 注解:

     1 package imooc_spring.test.anotation;
     2 
     3 import org.springframework.stereotype.Component;
     4 /**
     5  * Component 注解
     6  * @author Wei
     7  *
     8  */
     9 @Component
    10 public class TestObj {
    11     public void SayHi(){
    12         System.out.println("
    Hi this is TestObj.SayHi()...");
    13     }
    14 }

    @Controller注解:

     1 package imooc_spring.test.anotation;
     2 
     3 import org.springframework.stereotype.Controller;
     4 
     5 @Controller
     6 public class UserController {
     7     public void execute(){
     8         System.out.println("
    UserController.execute()...");
     9     }
    10 }

    @Repository注解:

     1 package imooc_spring.test.anotation;
     2 
     3 import org.springframework.stereotype.Repository;
     4 
     5 //@Repository
     6 @Repository("wyl_repo")
     7 public class UserRepositoryImpl implements IUserRepository {
     8 //模拟持久化层
     9     @Override
    10     public void save() {
    11         // TODO Auto-generated method stub
    12         System.out.println("
    UserRepositoryImpl.save()...");
    13     }
    14 
    15 }

    @Service注解:

     1 package imooc_spring.test.anotation;
     2 
     3 import org.springframework.stereotype.Service;
     4 
     5 @Service
     6 public class UserService {
     7     public void add(){
     8         System.out.println("
    UserService.add()...");
     9     }
    10 }

    2.另外一组注解:

    @Autowired、@Resource 这两个注解的功能可以说是一样的,前面一组注解的功能也是类似的。

    @Autowired 注解自动装配具有兼容类型的单个 Bean属性

    可以用来标识 

    1 构造器,

    2 普通字段(即使是非 public),

    3 一切具有参数的方法

    这三种都可以应用@Authwired 注解

    默认情况下, 所有使用 @Authwired 注解的属性都需要被设置. 当 Spring 找不到匹配的 Bean 装配属性时, 会抛出异常, 若某一属性允许不被设置, 可以设置 @Authwired 注解的 required 属性为 false

    @Resource 和 @Inject 注解,这两个注解和 @Autowired 注解的功用类似

    @Resource 注解要求提供一个 Bean 名称的属性,若该属性为空,则自动采用标注处的变量或方法名作为 Bean 的名称

    @Inject 和 @Autowired 注解一样也是按类型匹配注入的 Bean, 但没有 reqired 属性

    建议使用 @Autowired 注解

     1 package imooc_spring.test.anotation;
     2 
     3 import org.springframework.beans.factory.annotation.Autowired;
     4 import org.springframework.stereotype.Service;
     5 
     6 @Service
     7 public class UserService {
     8     @Autowired(required=false)
     9     public UserRepositoryImpl rep ;
    10     
    11     public void add(){
    12         System.out.println("
    UserService.add()...");
    13     }
    14 }
     1 package imooc_spring.test.anotation;
     2 
     3 import org.springframework.stereotype.Repository;
     4 
     5 //@Repository
     6 @Repository("wyl_repo")
     7 public class UserRepositoryImpl implements IUserRepository {
     8 //模拟持久化层
     9     @Override
    10     public void save() {
    11         // TODO Auto-generated method stub
    12         System.out.println("
    UserRepositoryImpl.save()...");
    13     }
    14 
    15 }

    @Autowired(required=false)

    这个就相当于IOC容器里的ref属性,比如

    1 <bean id="people" class="test.spring.autowired.Person" scope="prototype"
    2         autowire="byName">
    3         <property name="name" value="小明"></property>
    4         <property name="cat" ref="cat222"></property>
    5 </bean>
    6 <bean id="cat222" class="test.spring.autowired.Cat">
    7       <property name="name" value="我是小喵喵"></property>
    8 </bean>

    整个IOC容器:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
     4     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     5         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
     6     <bean id="moocAppctx" class="imooc_spring.test.aware.MoocApplicationContext"
     7         init-method="hhhh">
     8     </bean>
     9 
    10     <!--context:component-scan 指定 扫描的包 -->
    11     <!--可以通过 resource-pattern 指定扫描的资源, resource-pattern="myrepository/*.class" 
    12         的含义: 只扫描 base-package 对应包下的 目录为 myrepository 的所有java Bean -->
    13     <!-- <context:component-scan base-package="imooc_spring.test.anotation" 
    14         resource-pattern="myrepository/*.class"></context:component-scan> -->
    15 
    16     <!-- context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository" 
    17         子节点指定排除哪些注解 context:include-filter type="annotation" 需要结合context:component-scan 
    18         标签的 use-default-filters="false"来使用 context:exclude-filter type="assignable" 
    19         这个expression指的是自己写的类,意思排除哪些类 expression="imooc_spring.test.anotation.TestObj" -->
    20     <context:component-scan base-package="imooc_spring.test.anotation">
    21         <!-- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository" 
    22             /> -->
    23 
    24         <!-- <context:exclude-filter type="assignable" expression="imooc_spring.test.anotation.TestObj" 
    25             /> -->
    26 
    27 
    28     </context:component-scan>
    29 
    30 </beans>

    中的ref,一个bean引用另一个bean,上面的例子中就是 

    UserService 这个bean引用 UserRepositoryImpl 这个bean,

    当然了,使用这些注解的时候都需要结合

    <context:component-scan base-package="imooc_spring.test.anotation">
    </context:component-scan>

    来一起使用。

    @Autowired(required=false)

    中的required属性可以不写,不写的时候默认为required=true,表示如果没有找到要引用的bean,那么引用的这个bean就为null,

    但是这样子的话可能会由于引用的bean属性为null,从而可能会引起空指针异常。

  • 相关阅读:
    Python os.getcwd()方法
    Python os.walk()方法
    PyTorch 模型构造
    Python map()函数
    字符串转数字测试--知识备忘
    如何判断一个变量是数组Array类型--实例--加个人见解
    js面试题
    ios学习笔记
    读取图片文件--实例
    名言记录
  • 原文地址:https://www.cnblogs.com/Sunnor/p/5705628.html
Copyright © 2011-2022 走看看