Spring3 注解@ComponentScan、@Configuration、@Bean、@Scope

1. @ComponentScan annotation

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
 
    <context:component-scan base-package=“com.mengma”/>
 
</beans>

As I learned in Spring 2.5, <context: component-scan> will traverse all type definitions under the com.mengma path, find the classes marked with corresponding annotations, and add them to the IoC container. <context: component-scan> Scan annotation types are @Component and refined @Contoller, @Service and @Repository, and <context: component-scan>, will also register AutowiredAnnotationBeanPostProcessor and CommonAnnotationBeanPostProcessor to the container, so, @Autowired and @Resource can also satisfy dependency injection. 

  1. @ComponentScan ("net.quickcodes. *") Is similar to this line in the configuration file: <context: component-scan base-package = "com.mengma" />
  2. What is @ComponentScan annotation? @ComponentScan is mainly to define the scanning path, and find out from it that the class that needs to be assembled is automatically assembled into the spring bean container;
  3. Students who have done web development must have used @Controller , @Service , @Repository annotations . Check their source code and you will find that they have a common annotation @Component . Yes, @ComponentScan annotations will be equipped with a logo @Controller by default. , @Service, @Repository, @Autowired, @Resource, @Component annotated classes into the spring container;
  4. Parameters annotated by @ComponentScan:
  • value: scan path;
  • basePackages: same as value attribute;
  • basePackageClasses: add multiple classes in the specified array to the spring container;
  • includeFilters: 
  • excludeFilters:

   usage:   

  • Under the automatic scanning path, @Controller, @Service, @Repository, @Component annotations are added to the spring container;

  • Add the class that does not have the above annotations in the scan path to the spring container through includeFilters;

  • Use excludeFilters to filter out classes that do not need to be added to spring containers;

  • Customize the annotation method of adding @Component annotation;

Two, @Configuration annotation 

       

  1. From the definition point of view,  @Configuration annotations are essentially the same  @Component, so  <context:component-scan/> or  @ComponentScan they can handle @Configuration annotation classes.
  2. @Configuration The marked class must meet the following requirements:
  •           The configuration class must be provided in the form of a class (cannot be an instance returned by a factory method), which allows enhancement at runtime by generating subclasses (cglib dynamic proxy);
  •           The configuration class cannot be a  final class (no dynamic proxy);
  •           @Configuration cannot be an anonymous class;
  •           Configuration annotations are usually used to @Bean generate Spring container-managed classes through  annotations;
  •           The configuration class must be non-local (that is, it cannot be declared in a method and cannot be private);
  •           Any nested configuration class must be declared asstatic;
  •     @Bean The method may not in turn create further configuration classes (that is, if the returned bean comes with it  @Configuration, it will not be treated specially, it will only be used as an ordinary bean);

   3. @ Configuration is marked on the class, which is equivalent to using this class as the xml configuration file of spring. Its <beans>role is to configure the spring container (application context) 

package com.dxz.demo.configuration;
 
import org.springframework.context.annotation.Configuration;
 
@Configuration
public class TestConfiguration {
    public TestConfiguration() {
        System.out.println("TestConfiguration容器启动初始化。。。");
    }
}

Equivalent to: 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd" default-lazy-init="false">
 
 
</beans>

Generally speaking, in the configuration class, @Configuration and @Bean are used at the same time; 

           

In addition:

(1) Introduce @ComponentScan in the scan path in @configuration 

 

(2) Introduce spring's xml configuration file in @configuration 

    

 (3) Introduce spring's properties configuration file in @configuration

     

(4) Introduce other annotation configuration in @configuration 

       

(5) @configuration nesting (nested Configuration must be a static class)

       By nesting configuration classes of configuration classes, the purpose of combining multiple configuration classes is achieved. But note that the inner class must be a static class.

        

Three, @Bean annotation 

   In the previous way of using XML configuration, the bean is like this: 

<bean id="person" class="com.mengma.assembly.Person" />

The configuration form based on JavaConfig is this: 

@Configuration
public class Appconfig{
    @Bean
    public Person person(){
        return new Person();
    }
}

In xml, the dependency relationship between bean and bean is configured like this: 

<!--DAO配置-->
	<bean id="userDAO" class="com.hisu.daoImpl.UserDAOImpl" scope="singleton">
		...
	</bean>
	<!--service配置-->
	<bean id="userService" class="com.hisu.serviceImpl.UserServiceImpl">
		<property name="userDAO">
			<ref bean="userDAO"/>
		</property>
	</bean>

Now, the configuration form based on JavaConfig is like this: 

@Configuration
public class Appconfig{
    @Bean
    public UserDAO userDAO(){
        return new UserDAO();
    }

    @Bean
    public UserService userService(){
        return new UserService(userDAO());
    }
}

Note: I believe most people  will think that the  method returned here  may not be the same object as the  above method  when they  first see the  mockService() call  above  , so this method may be used instead of dependencyService()DependencyService@BeanDependencyService

@Autowired
private DependencyService  dependencyService;

In fact, it is not necessary to do so, the direct call  dependencyService() method returns the same instance.

  1. Scope of use: @Bean is a method-level annotation (method that returns an instance), which is equivalent to the xml configuration file in spring <bean>, and its role is: to register a bean object, mainly used in @Configuration annotated classes, also Can be used in @Component annotated classes.
  2. @Bean without parameters: the id of the added bean is the method name, and the type is the method return value type;
  3. @Bean (name = “testBean”) takes parameters: the id of the added bean is testBean, and the type is the method return value type;
  4. The default scope of @Bean annotation is singleton scope, which can be set as prototype scope by @Scope ("prototype"); 
  5. If the definition of a bean depends on other beans, then directly call the creation method of the dependent bean in the corresponding JavaConfig class.

Fourth, @Scope: @Bean attribute support

@Scope sets how the Spring container creates a new Bean instance (method, there must be @Bean). 
Its setting types include:

Singleton (singleton, there is only one bean instance in a Spring container, default mode), 
Protetype (each call creates a new bean)

Published 203 original articles · won praise 6 · views 4494

Guess you like

Origin blog.csdn.net/weixin_42073629/article/details/105400517