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("\nHi 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("\nUserController.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("\nUserRepositoryImpl.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("\nUserService.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("\nUserService.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("\nUserRepositoryImpl.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,從而可能會引起空指針異常。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM