Description: Spring IOC is actually defining some of the policy classes in service implementations that are not determined by initialization, Setter, or Factory methods. Instead, it is loaded through a set called context (ApplicationContext). Here we introduce two kinds of context-building component processes
Prerequisites: Introduction of support for the spring framework in the Build.gradle file of the Gradle project
repositories { mavencentral ()}dependencies { ' org.springframework ', Name: ' Spring-context ', Version: ' 4.1.5.RELEASE ' org.springframework ', Name: ' Spring-core ', version: ' 4.1.5.RELEASE ' ' JUnit ', Name: ' JUnit ', Version: ' 4.11 '}
- The first way: the Annotationconfigapplicationcontext class loads the configuration file of the Javaclass file
The configuration file Javaclass is as follows:
@Configuration: Indicates that this is a configuration file
@Bean: This is a Bean (named Method name), in the future he will be used to pair with the @autowired attribute in the service, note that the pairing is based on the return type, that is, all the service has @autowired attribute, They are all taken from this configuration file.
@Configuration Public classapplicationcontextconfiguration {@Bean Publicaccountrepoisitory accountrepoisitory () {return NewAccountrepositoryimp (); } @Bean PublicTransactionservice Transactionservice () {return NewTransactionserviceimp (); } @Bean PublicTransferservice Transferservice () {return NewTransferserviceimp (); }}
Take a look at the service class using @autowired. This autowired will be paired with the @bean in the configuration file above
Public classTransactionserviceimpImplementsTransactionservice {@Autowired Publicaccountrepoisitory accountrepoisitory; @Override Public voidNewtransaction (String accountId1, String accountId2,DoubleMoney ) {Account Account1=Accountrepoisitory.getaccountbyaccountid (ACCOUNTID1); Account Account2=Accountrepoisitory.getaccountbyaccountid (ACCOUNTID2); Transaction Transaction=NewTransaction () {}; Transaction.fromaccount=Account1; Transaction.toaccount=Account2; Transaction.moneytransfered=Money ; Transaction.transactiondate=calendar.getinstance (). GetTime (); BankFactory.Transactions.add (transaction); }}
Finally, let's see how the main function combines the configuration file with the service file. Very simple
ApplicationContext context= new Annotationconfigapplicationcontext ( Com.ctrip.configuration.ApplicationContextConfiguration. class );
Then we can use any of the methods defined in the service.
Accountrepoisitory Accountrepoisitory=context.getbean ("Accountrepoisitory",
Accountrepositoryimp.class);
Transactionservice Transactionservice=context.getbean ("Transactionservice",
Transactionserviceimp.class);
Transferservice Transferservice=context.getbean ("Transferservice",
Transferserviceimp.class
);
Transferservice.transfer ("1", "2", 234);
- Second way: Load the XML configuration file via Classpathxmlapplicationcontext
The first is to create a new beans definition XML below resources/meta-info/config (note this address so far I do not understand, put in other directories under the old failure.) All right, Mark first.
The property in the property defines the property that is involved in each bean. Note that this place must support setter methods in your service class because it is an assignment to a property
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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.xsd" > <BeanID= "Accountrepoisitory"class= "COM.CTRIP.DEPENDS.ACCOUNTREPOSITORYIMP"></Bean> <BeanID= "Transactionservice"class= "COM.CTRIP.DEPENDS.TRANSACTIONSERVICEIMP"> < Propertyname= "Accountrepoisitory"ref= "Accountrepoisitory"></ Property> </Bean> <BeanID= "Transferservice"class= "COM.CTRIP.DEPENDS.TRANSFERSERVICEIMP"> < Propertyname= "Accountrepoisitory"ref= "Accountrepoisitory"></ Property> < Propertyname= "Transactionservice"ref= "Transactionservice"></ Property> </Bean></Beans>
The use of the main method is actually very simple.
ApplicationContext context=New classpathxmlapplicationcontext ("classpath:/meta-inf/config/ Application-context.xml "); Transfer (context);
Through the above steps we tell the existence of each service and the service they depend on, and then we can use any of the methods in the service.
Accountrepoisitory Accountrepoisitory=context.getbean ("Accountrepoisitory",
Accountrepositoryimp.class);
Transactionservice Transactionservice=context.getbean ("Transactionservice",
Transactionserviceimp.class);
Transferservice Transferservice=context.getbean ("Transferservice",
Transferserviceimp.class
);
Transferservice.transfer ("1", "2", 234);
Spring in IOC-configuration of two applicationcontext loaded beans