需求:

用注解的方式完成注入对象中的效果。

创建文件:

同注入对象章节。
1.创建com.tutorialspoint包
2.创建HelloWorld类
3.创建MainAPP主类(void main)
4.创建Beans.xml
5.ModelTest类

代码:

HelloWorld.java

  1. package com.tutorialspoint;
  2. public class HelloWorld {
  3. private String message;
  4. public void setMessage(String message){
  5. this.message = message;
  6. }
  7. public void getMessage(){
  8. System.out.println("Your Message : " + message);
  9. }
  10. }

MainApp.java

  1. package com.tutorialspoint;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class MainApp {
  5. public static void main(String[] args) {
  6. // TODO Auto-generated method stub
  7. ApplicationContext context =
  8. new ClassPathXmlApplicationContext("Beans.xml");
  9. HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
  10. obj.getMessage();
  11. ModelTest model = (ModelTest) context.getBean("test");
  12. model.getStr();
  13. model.getHelloWorld();
  14. }
  15. }

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" xmlns:aop="http://www.springframework.org/schema/aop"
  4. xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/aop
  9. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  10. http://www.springframework.org/schema/tx
  11. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  12. http://www.springframework.org/schema/context
  13. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  14. <context:annotation-config/>
  15. <bean id="helloWorld" name="hello" class="com.tutorialspoint.HelloWorld">
  16. <property name="message" value="Hello World!"/>
  17. </bean>
  18. <bean id="test" class="com.tutorialspoint.ModelTest">
  19. <property name="str" value="Hello Str!"/>
  20. </bean>
  21. </beans>

ModelTest.java

@Autowired注解:

  1. package com.tutorialspoint;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. public class ModelTest {
  4. private String str;
  5. @Autowired
  6. private HelloWorld helloWorld;
  7. public void setStr(String string){
  8. this.str = string;
  9. }
  10. public void getStr(){
  11. System.out.println("Your str : " + str);
  12. }
  13. public void setHelloWorld(HelloWorld helloworld){
  14. this.helloWorld = helloworld;
  15. }
  16. public void getHelloWorld(){
  17. helloWorld.getMessage();
  18. }
  19. }

@Resource注解:

  1. package com.tutorialspoint;
  2. import javax.annotation.Resource;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. public class ModelTest {
  5. private String str;
  6. @Resource(name="hello")
  7. private HelloWorld helloWorld;
  8. public void setStr(String string){
  9. this.str = string;
  10. }
  11. public void getStr(){
  12. System.out.println("Your str : " + str);
  13. }
  14. public void setHelloWorld(HelloWorld helloworld){
  15. this.helloWorld = helloworld;
  16. }
  17. public void getHelloWorld(){
  18. helloWorld.getMessage();
  19. }
  20. }

运行结果:

图片.png

要点:

@autowired 一般用在service层,@resources一般用在Mapper层
@Resource:
@Resource默认按byName自动注入。 既不指定name属性,也不指定type属性,则自动按byName方式进行查找。如果没有找到符合的bean,则回退为一个原始类型进行进行查找,如果找到就注入。 只是指定了@Resource注解的name,则按name后的名字去bean元素里查找有与之相等的name属性的bean。 只指定@Resource注解的type属性,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常。
@Autowired:
@Autowired默认先按byType进行匹配,如果发现找到多个bean,则又按照byName方式进行匹配,如果还有多个,则报出异常。