Loading

Spring 使用context:annotation-config的设置

Spring 使用context:annotation-config的设置;

还是需要声明Bean的,并且还可能自己定义Annotation;

xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
    <!-- 通知使用Annotation的方式进行注入 -->
    <context:annotation-config />
    <bean id="name" class="java.lang.String">
        <constructor-arg>
            <value>beanString</value>
        </constructor-arg>
    </bean>
    <!-- 如果将下面的bean注释掉,会因为找不到autowired对象报错; -->
    <bean id="cymbal" class="com.stono.sprtest.Cymbal"></bean>
    <!-- 如果有两个Instrument接口实现类,必须指定一个的autowire-candidate为false -->
    <bean id="harmonica2" class="com.stono.sprtest.Harmonica" autowire-candidate="false"></bean>
    <!-- 如果在@Autowired下面增加@Qualifier指定bean的id,就可以使用两个Instrument接口实现类 -->
    <bean id="harmonica" class="com.stono.sprtest.Harmonica"></bean>
    <!-- @Autowired默认使用byType方式进行注入,可以注入String类型 -->
    <bean id="singer" class="com.stono.sprtest.Singer"></bean>
</beans>

 

AppBean:

package com.stono.sprtest;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AppBeans5 {

    @SuppressWarnings("resource")
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("appbeans5.xml");
        Singer singer = (Singer) context.getBean("singer");
        System.out.println(singer);
    }

}

 

Annotation:

package com.stono.sprtest;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.beans.factory.annotation.Qualifier;

@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface HarmonicaInstrument {
}

 

POJO:

package com.stono.sprtest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

public class Singer {
    // 可以直接在private属性上面进行设置,也可以在setter方法上面设置;
    @Autowired
    // @Qualifier("harmonica")
    // 使用自定义annotation可以替换@Qualifier,但是在POJO上面会有Annotation
    @HarmonicaInstrument
    private InstrumentI instrument;
    // 增加上required=false,没有string类型的也是可以为空的;
    // @Autowired(required = false)
    // 可以使用 @Value直接进行注入;
    // @Value("annotaionValue")
    // 可以使用@Value进行SpEL表达式值的获取;
    @Value("#{systemProperties['os.name']}")
    private String name;
    public Singer() {
    }
    public Singer(InstrumentI instrument, String name) {
        this.instrument = instrument;
        this.name = name;
    }
    @Override
    public String toString() {
        return "Singer [instrument=" + instrument + ", name=" + name + "]";
    }
}
package com.stono.sprtest;

@HarmonicaInstrument
public class Harmonica implements InstrumentI {
    
}

 

posted @ 2015-09-28 11:28  stono  阅读(248)  评论(0编辑  收藏  举报