Spring in Action Study Notes (1)

Spring basis

IoC Inversion of Control , also called dependency injection DI-

An assembly bean

Recommended order: automatic assembly -> JavaConfig Assembly -> XML assembly

1. Automatic Assembly

  1. @Component Note: This class will show as a component class, and inform the Spring bean to be created for the class. The bean ID for the first letter lowercase class name;
    @ComponentScan Note: Tell Config Enable component scans the configuration class, the default scan base package the same configuration and the following packet classes for sub-packets.
    @Autowired Note: The object bean injection
    @Test Note: testing, unit testing requires junit junit-4.x.jar + hamcrest-core- 1.x.jar

  2. @Component ( "yourName") Note: assign an ID for the bean. @Named ( "yourName") the same effect.

    The following contains the specified base package assembly manually scan:
    • @ComponentScan("yourBasePackage")
    • @ComponentScan(basepackages = "yourBasePackage")
    • @ComponentScan(basepackages = {"yourBasePackage1", "yourBasePackage2"})

    String using the above example to indicate the basic package, the type of insecure (not type-safe).

    Directly specified class or interface package :( labeled empty interface)
    • @ComponentScan(basePackageClasses = {class1.class, class2.class})

    @Autowired also applied to a parameter. @Inject role is basically the same

2. JavaConfig assembly

This method does not require @Component and @ComponentScan

@Configuration notes indicate some sort is a configuration class

@Bean annotation tells Spring, the method returns an object that you want to register for the Spring context bean. The bean is consistent with the default method name and ID.

@Bean (name = "yourBeanName") can specify the bean's ID.

3. XML assembly

  • [ ] ALL

Second, Advanced Assembly

Profile Notes

@Profile ( "dev" / "prod" / "qa") annotation feature configuration bean, show a development environment, production, QA environment created when the corresponding bean.

By setting spring.profiles.active spring.profiles.default property or properties to activate the corresponding profile

  • As the initialization parameter DispatcherServlet
  • As a Web application context parameter
  • As JNDI entries
  • As an environment variable
  • As the JVM system properties
  • In the integrated test class, using @ActiveProfiles annotation settings

Conditioned bean

@Conditional (thisIsACondition.class) notes, thisIsACondition class implements the Condition interface,

public interface Condition{
    boolean matches(ConditionContext ctxt, AnnotatedTypeMetadata metadata);
}

Return true matches, the corresponding annotated bean will be created

Ambiguity automatic assembly

When multiple class @Component annotation have achieved a certain interface, is @Autowired annotation function using this interface as a parameter, there is a discrepancy:

@Primary comment choice of bean

@Qualifier ( "wantedBean") or @Inject @Autowired annotation used in combination with the set parameters of the bean is to inject qualifier (if not specified other qualifiers, the default qualifiers ID consistent with bean)

@Qualifier ( "xxx"), or annotations @Component @Bean combination with manually set bean qualifier

When you want to use a plurality of narrow Alternatively the bean qualifiers accurately, because it is not used in a plurality @Qualifier entry, a @xxx annotation can customize, add annotations defining @Qualifier, become the custom annotations a qualifier annotation

@Target({ElementType.CONSTRUCTOR, ElementType.FIELD
         ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface xxx(){}

scope of the bean

  • Singleton (Singleton): throughout the application, only one instance of the bean to create, by default.
  • Prototype (Prototype): each injection or when acquired by the Spring application context, creates a new bean instance.
  • Session (Session): In the Web application, create a bean instance for each session.
  • Request (Request): In the Web application, create a bean instance for each request.

@Scope @Component or @Bean combination with:

@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)

@Scope("prototype")

Run-time injection

@PropertySource ( "classpath: /a/b/c/app.properties") annotation configuration class, declare properties of the source. To retrieve the property by Environment.

Environment env;
env.getProperty("some.property");

getProperty (): Get Property Value

getRequiredProperty (): This attribute must be defined otherwise IllegalStateException is thrown

containsProperty (): Check whether there is a property

getPropertyAsClass (): resolved to a class attribute

String [] getActiveProfiles (): Returns an array of names of the activation profile;

String [] getDefaultProfiles (): Returns an array of default profile name;

boolean acceptsProfiles (String ... profiles): If the environment supports a given profile, then it returns true.

  • Placeholder property: "$ {...}"

    • Available in an XML file

    • When automatic assembly using @Value ( "$ {some.property}") annotations, and configure a PropertySourcesPlaceholderConfigurer bean

  • Spring Expression Language (SpEL) : "# {...}"

    For example: "# {T (System) .currentTimeMillis ()}": T () expression will java.lang.System considered type corresponding Java

    @Value("#{some.property}")

    @Value("#{systemProperties['some.property']}")

    • [ ] ALL

Guess you like

Origin www.cnblogs.com/caophoenix/p/11240488.html