Java框架spring 學習筆記(九):Spring的bean管理(@Required、@Component、@Autowired、@Resource註解)

註解:代碼裏面特殊的標記,使用註解能夠完成相關功能java

註解寫法:@註解名稱(屬性名、屬性值)spring

 

@Requiredui

用在set方法上,一旦用了這個註解,那麼容器在初始化bean的時候必需要進行set,也就是說必須對這個值進行依賴注入。this

 

編寫Student.javaspa

 1 package com.example.spring;
 2 import org.springframework.beans.factory.annotation.Required;
 3 
 4 public class Student {
 5     private String name;
 6 
 7  @Required  8     public void setName(String name){
 9         this.name = name;
10     }
11 
12     public void print(){
13         System.out.println("name:"+name);
14     }
15 }

 

編寫Beans.xmlcode

 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" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 9 
10     <context:annotation-config/>
11 
12     <bean id="student" class="com.example.spring.Student" >
13         <property name="name" value="XiaoMing"></property>
14     </bean>
15 
16 </beans>

 

編寫Application.javacomponent

 1 package com.example.spring;
 2 
 3 import org.springframework.context.support.AbstractApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 public class Application {
 7     public static void main(String[] args) {
 8         //bean配置文件所在位置 D:\\IdeaProjects\\spring\\src\\Beans.xml
 9         //使用AbstractApplicationContext容器
10         AbstractApplicationContext context = new ClassPathXmlApplicationContext("file:D:\\IdeaProjects\\spring\\src\\Beans.xml");
11         //獲得配置建立的對象
12         Student student = (Student) context.getBean("student");
13         student.print();
14     }
15 }

 

建立對象的註解有四個註解xml

  1. @Component :對那些比較中立的類進行註釋
  2. @Controller :WEB層
  3. @Service  :業務層
  4. @Repository  :持久層

 

@Component對象

在類定義上面使用,表示用註解方式建立對象,至關於在配置文件中,<bean id="" class="">blog

編寫User.java

 1 package com.example.spring;
 2 
 3 import org.springframework.stereotype.Component;
 4 
 5 @Component(value = "user") //至關於beans.xml中的<bean id="user" class="com.example.spring.User">
 6 public class User {
 7     public void print(){
 8         System.out.println("user pirnt.");
 9     }
10 }

編寫Beans.xml

 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"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans
 6     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 7     http://www.springframework.org/schema/context
 8     http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 9 
10     <!--開啓註解掃描,到包裏面掃描類、方法、屬性上面是否有註解-->
11     <context:component-scan base-package="com.example.spring"></context:component-scan>
12 
13 </beans>

 編寫Application.java

 1 package com.example.spring;
 2 
 3 import org.springframework.context.support.AbstractApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 public class Application {
 7     public static void main(String[] args) {
 8         //bean配置文件所在位置 D:\\IdeaProjects\\spring\\src\\Beans.xml
 9         //使用AbstractApplicationContext容器
10         AbstractApplicationContext context = new ClassPathXmlApplicationContext("file:D:\\IdeaProjects\\spring\\src\\Beans.xml");
11         //獲得配置建立的對象
12         User user = (User) context.getBean("user");
13         user.print();
14     }
15 }

運行輸出

user pirnt.

其餘的註解建立對象方式相似 

 

 

 

注入對象屬性的註解:

  1. @Autowired
  2. @Resource

 

@Autowired

對象類型屬性注入註解

編寫UserDao.java

 1 package com.example.spring;
 2 
 3 import org.springframework.stereotype.Component;
 4 
 5 //@Component 建立對象註釋,至關於在配置文件中中的<bean id="userDao" class="com.example.spring.UserDao">
 6 @Component(value = "userDao")
 7 public class UserDao {
 8     public void print(){
 9         System.out.println("Dao print.");
10     }
11 }

編寫UserService.java

 1 package com.example.spring;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Component;
 5 
 6 //@Component 建立對象註釋,至關於在配置文件中中的<bean id="userService" class="com.example.spring.UserService">
 7 @Component(value = "userService")
 8 public class UserService {
 9     //一、定義dao類型屬性,在dao屬性上面使用註解,完成對象注入
10     //@Autowired 至關於配置文件中的對象類型注入(以下)
11     //  <bean id="userDao" class="com.example.spring.UserDao"></bean>
12     //  <bean id="userService" class="com.example.spring.UserService" >
13     //      <property name="userDao" ref="userDao"></property>
14     //  </bean>
15  @Autowired 16     private UserDao userDao;
17     //二、使用註解方式時候不須要set方法
18     //public void setUserDao(UserDao userDao){
19     //    this.userDao = userDao;
20     //}
21 
22     public void print(){
23         System.out.println("Service print.");
24         userDao.print();
25     }
26 }

編寫Beans.xml

 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"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans
 6     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 7     http://www.springframework.org/schema/context
 8     http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 9 
10     <!--開啓註解掃描,到包裏面掃描類、方法、屬性上面是否有註解-->
11     <context:component-scan base-package="com.example.spring"></context:component-scan>
12 
13 
14 </beans>

編寫Application.java

 1 package com.example.spring;
 2 
 3 import org.springframework.context.support.AbstractApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 public class Application {
 7     public static void main(String[] args) {
 8         //bean配置文件所在位置 D:\\IdeaProjects\\spring\\src\\Beans.xml
 9         //使用AbstractApplicationContext容器
10         AbstractApplicationContext context = new ClassPathXmlApplicationContext("file:D:\\IdeaProjects\\spring\\src\\Beans.xml");
11         //獲得配置建立的對象
12         UserService user = (UserService) context.getBean("userService");
13         user.print();
14     }
15 }

運行輸出

Service print.
Dao print.

 

@Resource

@Autowired是經過類名來自動注入,和@Component(value = "userDao")中的value值無關,這時就引出了@Resource註解,@Resource(name="userDao"),就會查找@Component(value = "userDao")註解的類並注入。

相關文章
相關標籤/搜索