Spring Java-based Bean declaration
Use @configuration to set up;
Xml:
<?XML version= "1.0" encoding= "UTF-8"?><BeansXmlns: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 "> <Context:component-scanBase-package= "Com.stono.sprtest"></Context:component-scan></Beans>
Javaconfig:
Packagecom.stono.sprtest;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;ImportOrg.springframework.context.annotation.Scope; @Configuration Public classsingerconfig {@Bean @Scope ("Prototype") //If this is added, it will result in two cymbal objects; PublicInstrumenti Cymbal () {return Newcymbal (); } @Bean PublicSinger3 Singer3 () {//the cymbal () in the parameter does not create a new bean or the previous singleton entity; return NewSinger3 (Cymbal (), "Newstr"); }}
Appbean:
Packagecom.stono.sprtest;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classAppBeans8 {@SuppressWarnings ("Resource") Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Appbeans8.xml"); Singer3 Singer3= (Singer3) context.getbean ("Singer3"); System.out.println (SINGER3); Object Bean= Context.getbean ("Cymbal"); System.out.println (Bean); }}
Pojo:
Packagecom.stono.sprtest;Importorg.springframework.beans.factory.annotation.Autowired; Public classSinger3 {@AutowiredPrivateInstrumenti instrument; PrivateString name; PublicSinger3 (instrumenti instrument, String name) { This. Instrument =instrument; This. Name =name; } @Override PublicString toString () {return"Singer2 [instrument=" + instrument + ", name=" + name + "]"; }}
Package com.stono.sprtest; Public class Implements Instrumenti {}
Spring Java-based Bean declaration