目录

一、Autowired注解

二、Qualifier注释

三、@PostConstruct 和 @PreDestroy 注释

四、@Resource注解

 


Spring可以通过注解进行依赖注入

一、Autowired注解

required 注解用于类中setter方法,同样也可以属性中,要求传入的参数必须要在xml中配置。

代码实现:

层次快照

spring注解方法优先加载 spring注解用法_xml

XML配置:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <context:annotation-config/>

   <!-- 创建一个HelloWorld的bean -->
  <bean id="helloworld" class="com.tutorialspoint.HelloWorld">
   </bean>

   <!-- Definition for Student bean -->
   <bean id="student" class="com.tutorialspoint.Student">
     <property name="age" value="15"></property>
   </bean>

</beans>

Student类

package com.tutorialspoint;
public class Student {
   private Integer age;
   private String name;
   public void setAge(Integer age) {
      this.age = age;
   }
   public Integer getAge() {
      return age;
   }
   public void setName(String name) {
       = name;
   }
   public String getName() {
      return name;
   }
}

Helloworld类

package com.tutorialspoint;

import java.util.Map;
import java.util.Properties;
import ;

import org.springframework.beans.factory.annotation.Autowired;
public class HelloWorld {
	private Student student;
	
	public Student getStudent() {
		return student;
	}
  @Autowired
  //自动通过xml中的bean注入
	public void setStudent(Student student) {
		this.student = student;
	}

	public void add()
	  {
		 System.out.println("添加相关的信息");
	  }
	
}

MainApp类

package com.tutorialspoint;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
      HelloWorld helloWorld = (HelloWorld) context.getBean("helloworld");
      Student student2=helloWorld.getStudent();
      //返回属性类
      System.out.println("HelloWorld中的Student属性的值为......"+student2.getAge());
   }
}

输出结果:

spring注解方法优先加载 spring注解用法_spring注解方法优先加载_02

如果将xml中配置的bean的student对象注销,那么运行将会出现如下错误:

NoSuchBeanDefinitionException: No qualifying bean of type 'com.tutorialspoint.
Student' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

大概意思就是标注了自动装配的方法,至少存在一个符合条件的bean对象进行注入。(也就是说方法去Beans池中找Student类的对象传入,但是发现一个都没有)

二、Qualifier注释

Qualifier相当于过滤的功能,如果有多个相同的bean,通过Qualifier可以指定特定的对象进行注入。

代码实现:

xml代码:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <context:annotation-config/>

   <!-- 创建一个HelloWorld的bean -->
  <bean id="helloworld" class="com.tutorialspoint.HelloWorld">
   </bean>

   <bean id="student" class="com.tutorialspoint.Student">
        <property name="age" value="15"></property>
            </bean> 
    <bean  id="student1" class="com.tutorialspoint.Student">
          <property name="age" value="18"></property>
     </bean>
</beans>

HelloWorld类的代码

package com.tutorialspoint;

import java.util.Map;
import java.util.Properties;
import ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class HelloWorld {
	private Student student;
	
	public Student getStudent() {
		return student;
	}
  @Autowired
  //自动通过xml中的bean注入
  @Qualifier("student1")
	public void setStudent(Student student) {
		this.student = student;
	}

	public void add()
	  {
		 System.out.println("添加相关的信息");
	  }
	
}

其他代码与Autowired相同 

实现结果:

spring注解方法优先加载 spring注解用法_xml_03

三、@PostConstruct 和 @PreDestroy 注释

@PostConstruct和@PreDestroy注释标记bean对象在初始化与销毁时调用的相关方法。

代码实现:

xml代码:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <context:annotation-config/>

   <!-- 创建一个HelloWorld的bean -->
  <bean id="helloworld" class="com.tutorialspoint.HelloWorld">
   </bean>

   <bean id="student" class="com.tutorialspoint.Student" init-method="init" destroy-method="destroy">
        <property name="age" value="15"></property>
     </bean> 
</beans>

Student类的代码

package com.tutorialspoint;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

public class Student {
   private Integer age;
   private String name;
   public void setAge(Integer age) {
      this.age = age;
   }
   public Integer getAge() {
      return age;
   }
   public void setName(String name) {
       = name;
   }
   public String getName() {
      return name;
   }
   @PostConstruct
   public void init()
   {
	   
	  System.out.println("我被初始化了");
   }
   @PreDestroy
   public void destroy(){
      System.out.println("我将要被消灭了");
   }
}

MainApp类的代码:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
   public static void main(String[] args) {
	   AbstractApplicationContext  context = new ClassPathXmlApplicationContext("Beans.xml");
      Student student = (Student) context.getBean("student");
      //返回属性类
      System.out.println("HelloWorld中的Student属性的值为......"+student.getAge());
      context.registerShutdownHook();
   }
}

实现结果:

spring注解方法优先加载 spring注解用法_spring注解方法优先加载_04

四、@Resource注解

@Resource注解在字面意思上是来源,可以通过byName或者通过byType来寻找注入对象,而@Autowired注解是按照bytype注入。在使用@Resource进行注解的时候如果使用了byName策略,那么就会按照byName来寻找相关对象注入,如果使用了byType策略,那么就会按照byType来寻找相关对象进行注入。

     @Resource注解装配顺序为:1、 如果使用了byname和byType结合的策略,那么会结合两者来寻找符合要求的bean,找不到会抛出异常。

2、如果只使用了byType那么会按照Type来寻找符合要求的对象,找不到会抛出异常

3、如果只使用了byName那么会按照Name来寻找符合要求的对象,找不到会抛出异常

4、如果啥都没写,那么就会按照byName来寻找(是属性的名字或则setter中传入参数的名字),没找到就会按照byType来寻找。

代码实例:

xml代码:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <context:annotation-config/>

   <!-- 创建一个HelloWorld的bean -->
  <bean id="helloworld" class="com.tutorialspoint.HelloWorld">
   </bean>

   <bean id="student" class="com.tutorialspoint.Student" >
        <property name="age" value="15"></property>
     </bean> 
     <!-- 通过age来标记不同的bean -->
     <bean id="student1" class="com.tutorialspoint.Student" >
        <property name="age" value="18"></property>
     </bean> 
</beans>

Student类代码:

package com.tutorialspoint;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

public class Student {
   private Integer age;
   private String name;
   public void setAge(Integer age) {
      this.age = age;
   }
   public Integer getAge() {
      return age;
   }
   public void setName(String name) {
       = name;
   }
   public String getName() {
      return name;
   }
}

HelloWorld类代码:

package com.tutorialspoint;

import java.util.Map;
import java.util.Properties;
import ;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class HelloWorld {
	private Student student;
	
	public Student getStudent() {
		return student;
	}
	@Resource(name="student1")
	public void setStudent(Student student) {
		this.student = student;
	}
	public void add()
	  {
		 System.out.println("添加相关的信息");
	  }
	
}

MainApp类代码:

package com.tutorialspoint;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
   public static void main(String[] args) {
	   AbstractApplicationContext  context = new ClassPathXmlApplicationContext("Beans.xml");
      HelloWorld helloWorld = (HelloWorld) context.getBean("helloworld");
      //返回属性类
      Student student2=helloWorld.getStudent();
      System.out.println("HelloWorld中的Student属性的值为......"+student2.getAge());
      context.registerShutdownHook();
   }
}

实现结果:

byName

spring注解方法优先加载 spring注解用法_xml_05

spring注解方法优先加载 spring注解用法_xml_06

byType

spring注解方法优先加载 spring注解用法_spring注解方法优先加载_07

spring注解方法优先加载 spring注解用法_spring注解方法优先加载_08