lottery lec8

发布时间 2024-01-09 22:39:26作者: xyfyy

Lottery lec8

首先,需要将domain对infrastructure的依赖转变为infrastructure依赖domain

刚开始看到这个需求感觉很奇怪,通过观察代码

目前的repository是在domain当中的,其内部调用dao来访问数据库。可以将这个repository的实现放在infrastructure中,其依赖于domain中repository的接口。而在domain中使用repository,并不需要去依赖infrastructure,只需要使用Spring提供的控制反转,获取到repository对象即可。

java实现细节:

 boolean alterStatus(Long activityId, Enum<Constants.ActivityState> beforeState, Enum<Constants.ActivityState> afterState);

这个函数定义,比较困惑的就是Enum<Constants.ActivityState>, 后面仔细一想,如果想保证传入的参数是枚举类类型,肯定是需要一种类型声明的。可以通过Enum<enum class>来声明。

Spring注解

@Configuration注解
作用:

@Configuration注释类表明其主要目的是作为bean定义的源

@Configuration类允许通过调用同一类中的其他@Bean方法来定义bean之间的依赖关系

@Configuration注解申明当前类是一个配置类,相当于xml配置文件

/**
 * 说明:此处@Configuration 注解的作用,
 * 1、使配置类变成了full类型的配置类,spring在加载Appconfig的时候,Appconfig由普通类型转变为cglib代理类型 ,
 * 2、在 @Bean method中使用,是单例的,不会创建对个对象
 */
@ComponentScan("com.jiagouedu")
@Configuration
public class AppConfig {
 
    @Bean
    public User user(){
        System.out.println("-----initMethod = \"user\"-return user -----");
        return new User();
    }
     
    @Bean
    public Cat cat(){
        return new Cat();
    }
 
 
    @Bean
    //条件注解,只有TestConditional返回为true时,才能实例化Fox
    @Conditional(value = TestConditional.class)
    public Fox fox(){
        //假如 Appconfig上使用了 @Configuration注解,cat()方法不会每次都返回一个新的cat 对象,而是返回一个公共的代理对象
        ;
        System.out.println("test conditional");
        return new Fox(cat());
    }
}

配置@Configuration和不配置的区别:

​ 使用@Configuration注解后,在调用方法 fox()创建 fox实例的时候,需要参数 cat,调用方法cat()生成cat实例,此时会去spring的单例bean工厂获取cat的单例bean的实例;

​ 不使用@Configuration注解,实例化fox的时候,每次都会创建一个新的 cat对象,供实例化fox使用;


Bean注解
`@Bean`注解告诉 Spring 这个方法将会返回一个对象,这个对象要注册为 Spring 应用上下文中的

bean 注解参数:

value和name : name 和 value 两个属性是相同的含义的, 在代码中定义了别名。 为 bean 起一个名字,如果默认没有写该属性,那么就使用方法的名称为该 bean 的名称

autowire: 装配方式

​ Autowire.NO (默认设置)

​ Autowire.BY_NAME
​ Autowire.BY_TYPE

指定 bean 的装配方式, 根据名称 和 根据类型 装配, 一般不设置,采用默认即可

initMethod,为Bean添加初始化方法

destroyMethod,为Bean添加销毁方法

@Configuration
@ComponentScan(basePackages = {"springIocTest/pojo"})
public class SpringIocConfig {

    @Bean(initMethod = "init", destroyMethod = "destory")
    public UserService getUserService(){
        return new UserService();
    }
}

Component注解
@Component注解表明一个类会作为组件类,并告知 Spring 要为这个类创建 bean。

其通常是通过类路径扫描(Spring会默认扫描启动类当前包和其子包下面的所有文件,也可以通过@ComponentScan来指定额外的包)来自动侦测以及自动装配到 Spring 容器中。

@Repository@Service@Controller是【@Component】用于更具体用例的注解(分别在持久性、服务和表示层中)。这些注解对于我们后期对特定bean进行批量处理时是有帮助的

// Service源码
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {
    @AliasFor(
        annotation = Component.class
    )
    String value() default "";
}

这个注解就包含了@Component注解


Transactional注解

@Tranasctional注解是Spring 框架提供的声明式注解事务解决方案,我们在开发中使用事务保证方法对数据库操作的原子性,要么全部成功,要么全部失败。 就是事务操作嘛,容易理解~

ps:

  1. @Transactional 注解只能用在public 方法上,如果用在protected或者private的方法上,不会报错,但是该注解不会生效。
  2. @Transactional注解只能回滚非检查型异常,具体为RuntimeException及其子类和Error子类,可以从Spring源码的DefaultTransactionAttribute类里找到判断方法rollbackOn。
  3. 使用rollbackFor 属性来定义回滚的异常类型,使用 propagation 属性定义事务的传播行为。如: 回滚Exception类的异常,事务的传播行为支持当前事务,当前如果没有事务,那么会创建一个事务。
  4. @Transactional注解不能回滚被try{}catch() 捕获的异常
  5. @Transactional注解只能对在被Spring 容器扫描到的类下的方法生效。