當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
Spring context:component-scan代替context:annotation-config
生活随笔
收集整理的這篇文章主要介紹了
Spring context:component-scan代替context:annotation-config
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Spring context:component-scan代替context:annotation-config
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.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"><context:component-scan base-package="com.stono.sprtest" /> </beans>?
AppBean:
package com.stono.sprtest;import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class AppBeans6 {@SuppressWarnings("resource")public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("appbeans6.xml");Singer2 singer2 = (Singer2) context.getBean("singer");System.out.println(singer2);} }?
POJO:
package com.stono.sprtest;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;@Component("singer") // 這里可以指定Bean的ID public class Singer2 {@Autowired// @Component標(biāo)注的類,其Id是camel-casing類名;兩個(gè)Intrument接口實(shí)現(xiàn)類,必須指定其中的一個(gè);@Qualifier("saxophone")private InstrumentI instrument;@Value("justValue")private String name;@Overridepublic String toString() {return "Singer2 [instrument=" + instrument + ", name=" + name + "]";} } package com.stono.sprtest;import org.springframework.stereotype.Component;@Component public class Saxophone implements InstrumentI {private Integer age;private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;} } package com.stono.sprtest;import org.springframework.stereotype.Component;@Component public class Cymbal implements InstrumentI { }?
總結(jié)
以上是生活随笔為你收集整理的Spring context:component-scan代替context:annotation-config的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: jsp:include和%@includ
- 下一篇: Android百分比布局初探