Spring Learning (xxii) - @ Configuration and @Bean

1. @ Configuration annotation

This annotation is used on a class of notes, equivalent to the XML configuration beans, equivalent Ioc container, the head of one of its methods if they are registered @Bean, as will the Spring container Bean, and the xml configuration means the same as bean.

 

2. @ Bean is a comment on a method level, mainly used in @Configuration annotated class, it can also be used in @Component annotated class. @Bean be annotated return value of the method as a class, the method may be returned by the spring to the class registration @Bean the Context.

 

1. Write a class Wanzi.class

package beans;

import interfaces.WanziInterfece;

public class Wanzi{

    String wanzi = "wanzi";
        
    public Wanzi(){
        System.out.println("Wanzi.class 的构造方法");
    }
    
    public String getWanzi(){
        return wanzi;
    }
}

 

2, write a WanziConfig.java

springAnnotions Package; 

Import org.springframework.context.annotation.Bean; 
Import org.springframework.context.annotation.Configuration; 
Import org.springframework.context.annotation.Description; 
Import org.springframework.context.annotation.Scope; 

Import Beans. Wanzi; 

/ ** 
 * @Configuration notes equivalent Ioc container, the head of one of its methods if they are registered @Bean, as will the Spring container bean, like the bean meaning in xml configuration. 
 * @Configuration annotated class must be used in the spring.xml <context: component-scanbase-package = "XXX" /> package to scan the file resides 
 * @author qiaozhong 
 * / 
@Configuration 
public class WanziConfig { 

    @Bean (name = "wanzi") 
    the @Scope ( "prototype") 
    @Description ( "this is a test of @Bean annotated"
    }
}

 

3, spring-annotion.xml scanning configuration package is located WanziConfig.java

<?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd ">

        <!- Support @Value get the value of properties ->
        <bean id="appProperty" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <array>
                    <value>classpath:application.properties</value>
                </array>
            </property>
        </bean>
    
    <!-- 配置自动扫描的包 -->
    <context:component-scan base-package="springAnnotions"></context:component-scan>

</beans>

 

4, write test classes WanziConfigTest.java

package springAnnotions;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;

import beans.Wanzi;

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration("/springConfig/spring-all.xml") 
@TransactionConfiguration
public class WanziConfigTest {
    
    @Autowired
    private Wanzi wanzi;
    
    @Test
    public void testConfigurationAnnotion(){
        System.out.println(wanzi.getWanzi());
    }
}

 

Test Results:

Wanzi.class constructor 
wanzi

It can be seen in the implementation of the test, printed Wanzi.java constructor, and returns the wanzi.getWanzi () value.

DESCRIPTION Wanzi.class been injected into the context of the spring and by @configuration @Bean.

@configuration and the spring effect achieved @Bean <bean> tag

Guess you like

Origin www.cnblogs.com/gllegolas/p/11809373.html