SSH笔记-通过@Autowired配置注入关系

1、@Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。 通过 @Autowired的使用来消除 set ,get方法

2、autowire 属性要么根据类型自动装配(byType), 要么根据名称自动装配(byName), 只能选其中一个配置

3、文件:
①TestMain.java:测试类
②TestObject.java:追踪程序运行用
③TestObject2.java:追踪程序运行用
④TestRelation.java:测试内部包含其他类
⑤TestRepository.java:接口类
⑥TestRepositoryImpl.java:实现接口类1
⑦TestTwoRepositoryImpl.java:实现接口类2
⑧annotationContext2.xml:配置文件

4、TestMain.java

package com.demo.sshtest.annotation2;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestMain {

    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("annotationContext2.xml");
        //这里调用TestObject的ApplicationContext,因为TestObject添加了注解组件,所以只需要getBean("testObject")即可,其中testObject即类名首字母小写之后的名字
        //原因:对于扫描到的组件, Spring 有默认的命名策略: 使用非限定类名, 第一个字母小写. 也可以在注解中通过 value 属性值标识组件的名称
        TestObject testObject = (TestObject)applicationContext.getBean("testObject");
        System.out.println(testObject);
        testObject.tObj();
    }

}

5、TestObject.java

package com.demo.sshtest.annotation2;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class TestObject {

    //因为在配置文件中没有配置TestRelation,使用了classpath的扫描组件,所以需要用@Autowired注解属性,不然会报错
    @Autowired
    private TestRelation testRelation;

    public void tObj(){
        System.out.println("TestObject..........");
        //@Autowired注解后,TestRelation的方法就可以调用了
        testRelation.relationship();
    }
}

6、TestObject2.java

package com.demo.sshtest.annotation2;

public class TestObject2 {


    public void tObj2(){
        System.out.println("TestObject2..........");
    }
}

7、TestRelation.java

package com.demo.sshtest.annotation2;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
public class TestRelation {
    //跟TestObject一样,使用了classpath的扫描组件@Autowired注解属性配置TestRepository
    //因为TestRepository是一个接口,而且被TestRepositoryImpl和TestTwoRepositoryImpl分别实现了
    //所以需要使用@Qualifier("testTwoRepositoryImpl")来告诉class扫描组件,需要调用TestRepositoryImpl还是TestTwoRepositoryImpl的repository()方法
    //即:Spring 允许对方法的入参标注 @Qualifiter 已指定注入 Bean 的名称
    @Autowired
    @Qualifier("testTwoRepositoryImpl")
    private TestRepository testRepository;

    public void relationship(){
        System.out.println("relationship...............");
        testRepository.repository();
    }
}

8、TestRepository.java

package com.demo.sshtest.annotation2;

public interface TestRepository{

    void repository();

}

9、TestRepositoryImpl.java

package com.demo.sshtest.annotation2;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class TestRepositoryImpl implements TestRepository{

    //TestObject2没有配置组件扫描,所以需要使用@Autowired(required=false)
    //原因:默认情况下, 所有使用 @Authwired 注解的属性都需要被设置. 当 Spring 找不到匹配的 Bean 装配属性时, 会抛出异常, 若某一属性允许不被设置, 可以设置 @Authwired 注解的 required 属性为 false
    @Autowired(required=false)
    private TestObject2 testObject2;

    public void repository(){
        System.out.println("repository1............");
        System.out.println(testObject2);
    };

}

10、TestTwoRepositoryImpl.java

package com.demo.sshtest.annotation2;

import org.springframework.stereotype.Repository;

@Repository
public class TestTwoRepositoryImpl implements TestRepository {

    public void repository() {
        System.out.println("repository2............");
    };

}

11、annotationContext2.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.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <!--
    1、context:component-scan元素会自动注册 AutowiredAnnotationBeanPostProcessor实例, 该实例可以自动装配具有 @Autowired 和 @Resource 、@Inject注解的属性
    2、 @Autowired 和 @Resource 、@Inject的功能相类似,@Autowired相对来说功能最全面,所以一般使用@Autowired即可
    3、@Resource 注解要求提供一个 Bean 名称的属性,若该属性为空,则自动采用标注处的变量或方法名作为 Bean 的名称
    -->
    <context:component-scan base-package="com.demo.sshtest.annotation2"></context:component-scan>
</beans>

12、项目目录
项目目录

13、demo
https://download.csdn.net/download/qq_22778717/10470271

猜你喜欢

转载自blog.csdn.net/qq_22778717/article/details/80641225