`

Spring

 
阅读更多
Inversion of Control (IoC) is also known as dependency injection (DI). It is a process whereby objects define their dependencies only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method. The container then injects those dependencies when it creates the bean. This process is fundamentally the inverse of the bean itself controlling the instantiation

1. version history
1) Traditional XML based configuration
2) Annotation-based configuration: Spring 2.5 introduced support for annotation-based configuration metadata.
3) Java-based configuration: Starting with Spring 3.0, many features provided by the Spring JavaConfig project became part of the core Spring Framework. Thus, you can define beans external to your application classes by using Java rather than XML files. See @Configuration, @Bean, @Import, @DependsOn annotations.

2. ApplicationContext is a sub-interface of BeanFactory. It adds:
+ Easier integration with Spring’s AOP features
+ Message resource handling (for use in internationalization)
+ Event publication
+ Application-layer specific contexts such as the WebApplicationContext for use in web applications.

3. Annotation
1) @Configuration & @Bean
@Configuration is a class-level annotation indicating that an object is a source of bean definitions.
Annotating a class with @Configuration indicates that its primary purpose is as a source of bean definitions. Furthermore, @Configuration classes let inter-bean dependencies be defined by calling other @Bean methods in the same class.
@Bean is a method-level annotation and a direct analog of the XML <bean/> element. @Bean annotation is used to indicate that a method instantiates, configures, and initializes a new object to be managed by the Spring IoC container.
You can use the @Bean annotation in a @Configuration-annotated or in a @Component-annotated class.
Any classes defined with the @Bean annotation support the regular lifecycle callbacks and can use the @PostConstruct and @PreDestroy annotations from JSR-250.
2) Autowire
Spring framework enables automatic dependency injection. In other words, by declaring all the bean dependencies in a Spring configuration file, Spring container can autowire relationships between collaborating beans. This is called Spring bean autowiring.
To autodetect these classes and register the corresponding beans, you need to add @ComponentScan to your @Configuration class, where the basePackages attribute is a common parent package for the two classes.
@Configuration
@ComponentScan(basePackages = "org.example")
public class AppConfig  {
    // ...
}

Same to below XML:
<beans ...>
    <context:component-scan base-package="org.example"/>
</beans>

After enabling annotation injection, we can use autowiring on properties, setters, and constructors.
3) AnnotationConfigApplicationContext
When @Configuration classes are provided as input, the @Configuration class itself is registered as a bean definition and all declared @Bean methods within the class are also registered as bean definitions.
When @Component and JSR-330 classes are provided, they are registered as bean definitions, and it is assumed that DI metadata such as @Autowired or @Inject are used within those classes where necessary.

3) @Inject & @Name
Starting with Spring 3.0, Spring offers support for JSR-330 standard annotations (Dependency Injection). Those annotations are scanned in the same way as the Spring annotations.
Instead of @Component, you can use @javax.inject.Named or javax.annotation.ManagedBean.

4) @Component & @Repository, @Service, @Controller
@Component is a generic stereotype for any Spring-managed component. @Repository, @Service, and @Controller are specializations of @Component for more specific use cases

5) Other
@Resource (since JSR-250) annotation (javax.annotation.Resource) on fields or bean property setter methods.
@Value is typically used to inject externalized properties:
@Import annotation allows for loading @Bean definitions from another configuration class
@Profile annotation lets you indicate that a component is eligible for registration when one or more specified profiles are active
@PropertySource annotation provides a convenient and declarative mechanism for adding a PropertySource to Spring’s Environment.

4. AOP
Within your Spring configurations, all aspect and advisor elements must be placed within an <aop:config> element. An <aop:config> element can contain pointcut, advisor, and aspect elements (note that these must be declared in that order)
A pointcut that represents the execution of any business service
Each advice is a Spring bean. An Advisor is an aspect that contains only a single advice object associated with a pointcut expression.

E.g.
<aop:config>
    <aop:advisor
        pointcut="com.xyz.myapp.CommonPointcuts.businessService()"
        advice-ref="tx-advice"/>
</aop:config>

<tx:advice id="tx-advice">
    <tx:attributes>
        <tx:method name="*" propagation="REQUIRED"/>
    </tx:attributes>
</tx:advice>

The basic way to create an AOP proxy in Spring is to use the org.springframework.aop.framework.ProxyFactoryBean.
One of the most important benefits of using a ProxyFactoryBean or another IoC-aware class to create AOP proxies is that advices and pointcuts can also be managed by IoC.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics