springboot中@component的一些用法?

工厂&策略模式(大量替换if的使用)

package com.example.demo.config;

import com.example.demo.strategys.TableStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author liangjunxing
 * @date 2022/4/13 0013
 */
@Component("handlerContext")
public class HandlerContext {
    @Autowired
    private ApplicationContext applicationContext;
    //存放所有策略类Bean的map
    public static Map<String, Class<TableStrategy>> orderStrategyBeanMap= new HashMap<>();

    public TableStrategy getOrderStrategy(String type){
        Class<TableStrategy> strategyClass = orderStrategyBeanMap.get(type);
        if(strategyClass==null) throw new IllegalArgumentException("没有对应的类型");
        //从容器中获取对应的策略Bean
        return applicationContext.getBean(strategyClass);
    }
}
package com.example.demo.config;

import com.example.demo.annotation.HandlerTableType;
import com.example.demo.strategys.TableStrategy;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 策略核心功能,获取所有策略注解的类型
 * 并将对应的class初始化到HandlerContext中
 */
@Component
public class HandlerProcessor implements ApplicationContextAware {
    /**
     * 获取所有的策略Beanclass 加入HandlerContext属性中
     * @param applicationContext
     * @throws BeansException
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        //获取所有策略注解的Bean
        Map<String, Object> orderStrategyMap = applicationContext.getBeansWithAnnotation(HandlerTableType.class);
        orderStrategyMap.forEach((k,v)->{
            Class<TableStrategy> orderStrategyClass = (Class<TableStrategy>) v.getClass();
            String type = orderStrategyClass.getAnnotation(HandlerTableType.class).value();
            //将class加入map中,type作为key
            HandlerContext.orderStrategyBeanMap.put(type,orderStrategyClass);
        });
    }
}

package com.example.demo.annotation;

import java.lang.annotation.*;

@Target(ElementType.TYPE)  //作用在类上
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited  //子类可以继承此注解
public @interface HandlerTableType {
    /**
     * 策略类型
     * @return
     */
    String value();
}
package com.example.demo.strategys;

import org.apache.poi.hssf.usermodel.HSSFSheet;


public interface TableStrategy {
        abstract public void handleShow( String[] shows, HSSFSheet sheet);
}
package com.example.demo.strategys.impi;

import com.example.demo.annotation.HandlerTableType;

import com.example.demo.dao.ArticleMapper;
import com.example.demo.entity.Article;
import com.example.demo.strategys.TableStrategy;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
@HandlerTableType("article") //使用注解标明策略类型
public class ArticleTableStrategy implements TableStrategy {

    @Autowired
    private ArticleMapper mapper;
    @Override
    public void handleShow( String[] shows, HSSFSheet sheet) {
    		//对应该类型的业务逻辑代码
        }
    }
}


package com.example.demo.strategys.impi;

import com.example.demo.annotation.HandlerTableType;
import com.example.demo.dao.CronMapper;
import com.example.demo.entity.Cron;
import com.example.demo.strategys.TableStrategy;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author liangjunxing
 * @date 2022/4/14 0014
 */
@Component
@HandlerTableType("socks") //使用注解标明策略类型
public class SocksTableStrategy implements TableStrategy {
    @Autowired
    private CronMapper mapper;
    @Override
    public void handleShow(String[] shows, HSSFSheet sheet) {
        //对应该类型的业务逻辑代码
        }
    }
}

//工厂策略模式
HSSFSheet sheet = new HSSFWorkbook().createSheet("socks");
//调用对应type下的方法
handlerContext.getOrderStrategy("socks").handleShow(new String[]{},sheet);

反射方法(若依框架的定时任务实现思路)

package com.example.demo.config;

import org.springframework.stereotype.Component;

/**
 * @author liangjunxing
 * @date 2022/5/27 0027
 */
@Component("helloConfig")
public class HelloConfig {
    public String hello(String s){
        return "hello,"+s;
    }
}
        //反射方法
        Object bean = applicationContext.getBean("helloConfig");
        Method method = bean.getClass().getDeclaredMethod("hello", String.class);
        Object invoke = method.invoke(bean, new Object[]{"world"});
        System.out.println("==="+invoke+"===");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值