Spring learn note

本文深入探讨了Spring框架的核心功能,包括依赖注入(DI)和面向切面编程(AOP),并详细介绍了Spring容器如何创建、配置和管理应用程序对象的整个生命周期。通过XML配置和Java配置的示例,展示了如何实现组件之间的关联。

Core Spring

Spring module and portfolio have a lot of function and feature, but its primary features are dependency injection (DI) and aspect-oriented programming (AOP).

Spring aims at Simplifying Java development, Spring employs four key strategies:

  • Lightweight and minimally invasive development with POJOs
  • Loose coupling through DI and interface orientation
  • Declarative programming through aspects and common conventions
  • Eliminating boilerplate code with aspects and templates

Dependency injection (DI)

The act of creating associations between application components is commonly referred to as wiring. In Spring, there are many ways to wire components together, but a common approach has always been via XML, Spring also allows you to express configuration using Java.

e.g, XML
<beans ...>
  <bean id="person" class="com.gordon.Person">
    <constructor-arg ref="eat" />   
  </bean>

  <bean id="eat" class="com.gordon.Eat">    
    <constructor-arg value="#{T(System).out}" /><!-- SEL -->
  </bean>
</beans>
e.g, Java code
Omit relevant package and import.

@Configuration
public class PersonConfig{

  @Bean
  public Person doSomething() {
    return new Person(eat());
  }

  @Bean
  public Eat eat() {
    return new Eat(System.out);
  }

}

aspect-oriented programming (AOP)

It may help to think of aspects as blankets that cover many components of an application. At its core, an application consists of modules that implement business functionality. With AOP, you can then cover your core application with layers of functionality. These layers can be applied declaratively throughout your application in a flexible manner without your core application even knowing they exist. This is a powerful concept, because it keeps the security, transaction, and logging concerns from littering the application’s core business logic.
e.g XML

<beans ...>

  <bean id="person" class="com.gordon.Person">
    <constructor-arg ref="eat" />   
  </bean>

  <bean id="eat" class="com.gordon.Eat">    
    <constructor-arg value="#{T(System).out}" />
  </bean>

  <bean id="washHand" class="com.gordon.washHand">  
    <constructor-arg value="#{T(System).out}" />
  </bean>

  <aop:config>
    <aop:aspect ref="washHand">
      <!-- AspectJ -->
      <aop:pointcut id="doing"
          expression="execution(* *.doSomething(..))"/> 
      <!-- before advice -->
      <aop:before pointcut-ref="doing"  
          method="washBeforeDoing"/>
      <!-- after advice -->
      <aop:after pointcut-ref="embark"  
          method="washAfterDoing"/>
    </aop:aspect>
  </aop:config>

</beans>

Spring container

In a Spring-based application, your application objects live in the Spring container. The container creates the objects, wires them together, configures them, and manages their complete lifecycle from cradle to grave (or new to finalize(), as the case may be).

Spring container can be categorized into two distinct types.

  • Bean factories (defined by the org.springframework.beans.factory.BeanFactory interface) are the simplest of containers, providing basic support for DI
  • Application contexts (defined by the org.springframework.context.ApplicationContext interface) build on the notion of a bean factory by providing application-framework services(Recommend)
Application Context

In a Spring application, an application context loads bean definitions and wires them together. The Spring application context is fully responsible for the creation of and wiring of the objects that make up the application.

Here are a few that you’ll most likely encounter:

  • AnnotationConfigApplicationContext
  • AnnotationConfigWebApplicationContext
  • ClassPathXmlApplicationContext
  • FileSystemXmlApplicationContext
  • XmlWebApplicationContext
How it works

public class PersonMain {

  public static void main(String[] args) throws Exception {
    ClassPathXmlApplicationContext context =    
        new ClassPathXmlApplicationContext(
            "META-INF/spring/Persons.xml");
    Person person= context.getBean(Person.class);   
    person.doSomething();   
     context.close();
  }

}

You can find that it’s similar to annotation. three phase:

  1. Definition
  2. Declaration, use definition
  3. Process and resolution(likes apt)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值