<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
xsi:schemalocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<import resource="config/customer.xml"/>
<import resource="config/scheduler.xml"/>
</beans>
package com.h3.config;
import org.springframework.context.annotation.configuration;
import org.springframework.context.annotation.import;
@configuration
@import({ customerconfig.class, schedulerconfig.class })
public class appconfig {
}
file : customerbo.java
package com.h3.core;
public class customerbo {
public void printmsg(string msg) {
system.out.println("customerbo : " + msg);
}
}
file : schedulerbo.java
package com.h3.core;
public class schedulerbo {
public void printmsg(string msg) {
system.out.println("schedulerbo : " + msg);
}
}
file : customerconfig.java
package com.h3.config;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import com.h3.core.customerbo;
@configuration
public class customerconfig {
@bean(name="customer")
public customerbo customerbo(){
return new customerbo();
}
}
file : schedulerconfig.java
package com.h3.config;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import com.h3.core.schedulerbo;
@configuration
public class schedulerconfig {
@bean(name="scheduler")
public schedulerbo suchedulerbo(){
return new schedulerbo();
}
}
file : appconfig.java
package com.h3.config;
import org.springframework.context.annotation.configuration;
import org.springframework.context.annotation.import;
@configuration
@import({ customerconfig.class, schedulerconfig.class })
public class appconfig {
}
package com.h3.core;
import org.springframework.context.applicationcontext;
import org.springframework.context.annotation.annotationconfigapplicationcontext;
import com.h3.config.appconfig;
public class app {
public static void main(string[] args) {
applicationcontext context = new annotationconfigapplicationcontext(
appconfig.class);
customerbo customer = (customerbo) context.getbean("customer");
customer.printmsg("hello 11");
schedulerbo scheduler = (schedulerbo) context.getbean("scheduler");
scheduler.printmsg("hello 22");
}
}
输出结果
customerbo : hello 11 schedulerbo : hello 22