spring-IOC之依赖注入——注解篇

Spring学习随记2

spring-IOC之依赖注入

官网地址
XML篇

POM依赖
		<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.2.2.RELEASE</version>
	</dependency>
代码结构

在这里插入图片描述

使用@Configuration,@Bean注解代码实现
package com.csh.test.project.annotation.configure;

import com.csh.test.project.annotation.service.EmployeeService;
import com.csh.test.project.annotation.service.ManagementService;
import com.csh.test.project.annotation.service.impl.EmployeeServiceImpl;
import com.csh.test.project.annotation.service.impl.ManagementServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created on 2020-01-19.
 *
 * @author chensihong
 */
@Configuration
public class AppConfig {

    @Bean
    public EmployeeService employeeService() {
        return new EmployeeServiceImpl();
    }

    @Bean
    public ManagementService managementService(EmployeeService employeeService) {
        final ManagementServiceImpl managementService = new ManagementServiceImpl();
        managementService.setEmployeeService(employeeService);
        return managementService;
    }
}
package com.csh.test.project.annotation.service.impl;

import com.csh.test.project.annotation.service.EmployeeService;

/**
 * Created on 2020-01-17.
 *
 * @author chensihong
 */
public class EmployeeServiceImpl implements EmployeeService {

    public EmployeeServiceImpl() {
        //init
        System.err.println("EmployeeServiceImpl constructor");
    }

    @Override
    public void print() {
        System.err.println("EmployeeServiceImpl print!!!");
    }
}
package com.csh.test.project.annotation.service.impl;

import com.csh.test.project.annotation.service.EmployeeService;
import com.csh.test.project.annotation.service.ManagementService;

/**
 * Created on 2020-01-17.
 *
 * @author chensihong
 */
public class ManagementServiceImpl implements ManagementService {
    public void setEmployeeService(EmployeeService employeeService) {
        employeeService.print();
    }

    public ManagementServiceImpl() {
        //init
        System.err.println("ManagementServiceImpl");
    }
}
package com.csh.test.project.annotation.service;

/**
 * Created on 2020-01-17.
 *
 * @author chensihong
 */
public interface EmployeeService {
    void print();
}

package com.csh.test.project.annotation.service;

/**
 * Created on 2020-01-17.
 *
 * @author chensihong
 */
public interface ManagementService {
}

package com.csh.test.project.annotation;

import com.csh.test.project.annotation.configure.AppConfig;
import com.csh.test.project.annotation.service.ManagementService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Created on 2020-01-19.
 *
 * @author chensihong
 */
public class App {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
        ctx.getBean(ManagementService.class);
    }
}
使用@Configuration,@ComponentScan,@Component/@Service/@Repository,@AutoWired注解代码实现(接口类/App.java同上)
package com.csh.test.project.annotation.configure;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * Created on 2020-01-19.
 *
 * @author chensihong
 */
@Configuration
@ComponentScan(basePackages = {"com.csh.test.project.annotation"})
public class AppConfig {

}
package com.csh.test.project.annotation.service.impl;

import com.csh.test.project.annotation.service.EmployeeService;
import org.springframework.stereotype.Repository;

/**
 * Created on 2020-01-17.
 *
 * @author chensihong
 */
@Repository
public class EmployeeServiceImpl implements EmployeeService {

    public EmployeeServiceImpl() {
        //init
        System.err.println("EmployeeServiceImpl constructor");
    }

    @Override
    public void print() {
        System.err.println("EmployeeServiceImpl print!!!");
    }
}

package com.csh.test.project.annotation.service.impl;

import com.csh.test.project.annotation.service.EmployeeService;
import com.csh.test.project.annotation.service.ManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * Created on 2020-01-17.
 *
 * @author chensihong
 */
@Repository
public class ManagementServiceImpl implements ManagementService {
    @Autowired
    public void setEmployeeService(EmployeeService employeeService) {
        employeeService.print();
    }

    public ManagementServiceImpl() {
        //init
        System.err.println("ManagementServiceImpl");
    }
}
拓展
package com.csh.test.project.annotation;

import com.csh.test.project.annotation.configure.AppConfig;
import com.csh.test.project.annotation.service.ManagementService;
import com.csh.test.project.annotation.service.impl.AfterRegisterTest;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Created on 2020-01-19.
 *
 * @author chensihong
 */
public class App {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
        // ctx 中register的使用,加载配置类,或将类放入spring容器中维护
        ctx.register(AppConfig.class);
        //将类放入spring容器中维护
        ctx.register(AfterRegisterTest.class);
        //个人理解为刷新,启动bean的生命周期维护,若使用AnnotationConfigApplicationContext(xxx.class)
        // 携参构造函数自带refresh(),该方法只需要调用一次,多于或少于则报错
        ctx.refresh();
        ctx.getBean(ManagementService.class);
        ctx.getBean(AfterRegisterTest.class);
    }
}

@Qualifier注解的使用
可标记字段、方法、参数、类/接口/枚举、自定义注解
@Qualifier和@Component/@Service/@Repository结合标注类
@Qualifier和@Autowired结合标记引用哪个类
@Repository
@Qualifier("1")
public class EmployeeServiceImpl implements EmployeeService 

@Repository
@Qualifier("2")
public class EmployeeServiceImpl2 implements EmployeeService

@Repository
public class ManagementServiceImpl implements ManagementService {
    @Autowired
    @Qualifier("2")
    //注入EmployeeServiceImpl2实例
    public void setEmployeeService(EmployeeService employeeService) {
        employeeService.print();
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值