ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

spring散记(一)

2020-03-07 13:40:45  阅读:277  来源: 互联网

标签:依赖 spring class framework docs 散记 public


官方文档https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html

springIOC

控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度。其中最常见的方式叫做依赖注入(Dependency Injection,简称DI),还有一种方式叫“依赖查找”(Dependency Lookup)

依赖查找:如JNDI,主动查找

spring实现IOC的思路大致可以拆分成3点

1.应用程序中提供类,提供依赖关系(属性或者构造方法)

2.把需要交给容器管理的对象通过配置信息告诉容器(xml、annotation,javaconfig)

3.把各个类之间的依赖关系通过配置信息告诉容器

注入的三种方法

接口注入

spring4及其之后被取消,不人性化

构造方法

(1)

package x.y;

public class ThingOne {

    public ThingOne(ThingTwo thingTwo, ThingThree thingThree) {
        // ...
    }
}

还有种@ConstructorProperties({"years", "ultimateAnswer"})

(2)

<beans>
    <bean id="beanOne" class="x.y.ThingOne">
        <constructor-arg ref="beanTwo"/>
        <constructor-arg ref="beanThree"/>
    </bean>

    <bean id="beanTwo" class="x.y.ThingTwo"/>

    <bean id="beanThree" class="x.y.ThingThree"/>
</beans>

setter方法

用法:https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-constructor-injection

三种风格

xml

annotation使用注解记得开启注解和扫描(4之后扫描包含注解和扫描)

java Configuration

熟练混合使用

如:java Configuration+annotation+xml

还有一种直接干掉xml。

@Configuration

@ComponentScan("xxx")

+xml:@ImportResource("classpath:xxx")

ClassPathXmlApplicationContext->AnnotationConfigApplicationContext

自动装配(局限看官网)

        IOC的注入有两个地方需要提供依赖关系,一是类的定义中,二是在spring的配置中需要去描述。自动装配则把第二个取消了,即我们仅仅需要在类中提供依赖,继而把对象交给容器管理即可完成注入。在实际开发中,描述类之间的依赖关系通常是大篇幅的,如果使用自动装配则省去了很多配置,并且如果对象的依赖发生更新我们可以不需要去更新配置

byType:private IndexDao dao;

byName:如:setDao()->dao  (也是xml的 bean id=dao)

注;. @Repository@Service, and @Controller can also carry additional semantics in future releases of the Spring Framework. Thus, if you are choosing between using @Component or @Service for your service layer, @Service is clearly the better choice. 

@Component可以注解到任意被spring管理的容器上,是@Service,@Controller,@Repository(一般在dao上)父类

@Autowired默认byType,找不到就byName,但不根据setDao()而是private IndexDao dao,(1)可以加个@Primary (2)使用限定符@Qualifier()

@Resource默认byName,但不根据setDao()而是private IndexDao dao,也可以指定type

记一下:BeanNameGenerator

spring作用域

全局singleton,局部prototype

解决办法:implements ApplicationContextAware

方法一

缺点,与spring耦合太高

方法二:

@lookUp

生命周期的回调

     为了与容器对bean生命周期的管理进行交互

3种方法

1.实现Spring InitializingBeanDisposableBean接口(不好,耦合大啊)

2.配置init-methodand destroy-method属性指定(在XML中)方法名称来覆盖默认值(难配置)

3.使用注解@PostConstruct@PreDestroy (推荐)

<bean id="beanOne" class="ExampleBean" depends-on="manager"/>
<bean id="manager" class="ManagerBean" />

depends-on(也许beanOne不依赖manager,但或许beanOne中的构造方法中用到了manager):就是beanOne实例化之前先实例化manager

过滤器自定义扫描

例子:

@Configuration
@ComponentScan(basePackages = "org.example",
        includeFilters = @Filter(type = FilterType.REGEX, pattern = ".*Stub.*Repository"),
        excludeFilters = @Filter(Repository.class))
public class AppConfig {
    ...
}

Generating an Index of Candidate Components(也可以不用,用java注解见官网也行)

https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-standard-annotations     

      尽管类路径扫描非常快,但是可以通过在编译时创建候选静态列表来提高大型应用程序的启动性能。在这种模式下,作为组件扫描目标的所有模块都必须使用此机制。

要生成索引,请向每个包含组件的模块添加附加依赖关系,这些组件是组件扫描指令的目标。以下示例显示了如何使用Maven进行操作

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-indexer</artifactId>
        <version>5.2.4.RELEASE</version>
        <optional>true</optional>
    </dependency>
</dependencies>

@Bean注释被用于指示一个方法实例(具体见官网)

https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-java

使用此方法在ApplicationContext指定为该方法的返回值的类型内注册Bean定义。缺省情况下,bean名称与方法名称相同

@Configuration
public class AppConfig {

    @Bean
    public TransferServiceImpl transferService() {
        return new TransferServiceImpl();
    }
}

@Bean注释的方法可以具有任意数量的参数,这些参数描述构建该bean所需的依赖关系

@Configuration
public class AppConfig {

    @Bean
    public TransferService transferService(AccountRepository accountRepository) {
        return new TransferServiceImpl(accountRepository);
    }
}

@Profile

@Profile 注解让你指示组件有资格登记在一个或多个指定的配置文件是活动的

 也可以在方法级别声明为仅包含配置类的一个特定bean 

AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.getEnvironment().setActiveProfiles("development");
ctx.register(SomeConfig.class, StandaloneDataConfig.class, JndiDataConfig.class,Appconfig.class);
ctx.refresh();

测试可以用spring-jdbc

标签:依赖,spring,class,framework,docs,散记,public
来源: https://blog.csdn.net/a_higher/article/details/104709436

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有