0%

Spring Framework

"概览"

Ioc Container(DI)

reference:

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

def:objects define their dependencies only through constructor arguments

  • org.springframework.beans
  • org.springframework.context

A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container.

Container Overview

configuring metadata

  • xml based

    this bean definition corresponds to the actual objects

    • service layer
    • data access objects
  • annotation based

  • java-based

instantiating a Container

1
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");

service.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- services -->

<bean id="petStore" class="org.springframework.samples.jpetstore.services.PetStoreServiceImpl">
<property name="accountDao" ref="accountDao"/>
<property name="itemDao" ref="itemDao"/>
<!-- additional collaborators and configuration for this bean go here -->
</bean>

<!-- more bean definitions for services go here -->

</beans>

Dependency

  • constructor-based Dependency
  • setter-based

Annotation-based

@Required for setter method | @Autowired for constructor

1
2
3
4
5
6
7
8
9
10
11
public class MovieRecommender {

private final CustomerPreferenceDao customerPreferenceDao;

@Autowired
public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
this.customerPreferenceDao = customerPreferenceDao;
}

// ...
}

As of Spring Framework 4.3, an @Autowired annotation on such a constructor is no longer necessary if the target bean defines only one constructor to begin with. However, if several constructors are available, at least one must be annotated to teach the container which one to use.

  1. traditional constructor

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public class MovieRecommender {

    private final CustomerPreferenceDao customerPreferenceDao;

    @Autowired
    public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
    this.customerPreferenceDao = customerPreferenceDao;
    }

    // ...
    }
  2. setter

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public class SimpleMovieLister {

    private MovieFinder movieFinder;

    @Autowired
    public void setMovieFinder(MovieFinder movieFinder) {
    this.movieFinder = movieFinder;
    }

    // ...
    }
  3. arbitrary names and multiple arguments

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    public class MovieRecommender {

    private MovieCatalog movieCatalog;

    private CustomerPreferenceDao customerPreferenceDao;

    @Autowired
    public void prepare(MovieCatalog movieCatalog,
    CustomerPreferenceDao customerPreferenceDao) {
    this.movieCatalog = movieCatalog;
    this.customerPreferenceDao = customerPreferenceDao;
    }

    // ...
    }
  4. fileds

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    public class MovieRecommender {

    private final CustomerPreferenceDao customerPreferenceDao;

    @Autowired
    private MovieCatalog movieCatalog;

    @Autowired
    public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
    this.customerPreferenceDao = customerPreferenceDao;
    }

    // ...
    }
  5. arrays and collections

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public class MovieRecommender {

    private Set<MovieCatalog> movieCatalogs;

    @Autowired
    public void setMovieCatalogs(Set<MovieCatalog> movieCatalogs) {
    this.movieCatalogs = movieCatalogs;
    }

    // ...
    }
  6. map

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public class MovieRecommender {

    private Map<String, MovieCatalog> movieCatalogs;

    @Autowired
    public void setMovieCatalogs(Map<String, MovieCatalog> movieCatalogs) {
    this.movieCatalogs = movieCatalogs;
    }

    // ...
    }

autowiring fails when no matching candiate beans are available

AOP

Aspect Oriented Programming