The main purpose of this section is to configure Spring-service.xml, which is to configure spring and test whether the service layer is configured successfully
Using IntelliJ idea to develop a spring+springmvc+mybatis framework step-up two: Configure MyBatis and Test (2 configuration Spring-dao and testing)
Continue with spring configuration on this basis.
Review above we have successfully tested the configuration through the MyBatis. The directory structure at this time is:
One: Below we continue to add directory structure, create service, DTO and Util folder in Com.peakfortake file directory project, create spring-service.xml under Spring file
File. and copy Spring-dao.xml's head into the spring-service.xml.
Two: Configuration Spring-service.xml This configuration is relatively simple
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context" xmlns:tx= "Http://www.springframework.org/schema/tx"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdHttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><!--Spring Service configuration-<!--scan the service pack for all types of annotations that are used--<context:component-scan base- Package= "Peakfortake.service"/> <!--configuration transaction Manager--<bean id= "TransactionManager"class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" > <!-- Because this spring-service is not defined in DataSource but is already defined in Spring-dao, it is automatically found at the time of execution and <property name= "DataSource" ref= "DataS Ource "/> </bean> <!--configuration based on annotation declaration transactions--<tx:annotation-driven transaction-manager=" Transactionmanag ER "/></beans>
Three: Implementing service and adding annotations The main thing is the annotations @service and @transactional , especially transactions.
@Service Public classTestuserserviceimplImplementsTestuserservice {//Inject Dependency@AutowiredPrivateTestuserdao Testuserdao; @Transactional/*** Advantages of using annotations to control transaction methods * 1: The development team agreed to define the code specification for the transaction method * 2: Ensure that the transaction method execution time is as short as possible, do not interspersed with other network operations such as HTTP operations or stripping transactions external methods * 3: Not All methods require transactions, and if there is a modification, the read-only operation does not require transaction control*/ PublicList<string>Getallusername () {List<TestUser> T =Testuserdao.getuser (); List<String> Allname =NewArraylist<string>(); for(TestUser tt:t) {Allname.add (Tt.getusername ()); } returnAllname; } PublicList<testuser>userlist () {returnTestuserdao.getuser (); }}
Four: Build Logback logs and test
1. Build Logback.xml File under Resource
and http://logback.qos.ch/manual/configuration.html the profile on LOGBAKC's official web site.
<?xml version= "1.0" encoding= "UTF-8"?><configuration> class= " Ch.qos.logback.core.ConsoleAppender "> <!-- Encoders is assigned the type default -- > <encoder> <pattern>%d{hh:mm:ss. SSS} [%thread]%-5level%logger{36}-%msg%n</pattern> </encoder> </appender> < Root level= "Debug" > <appender-ref ref= "STDOUT"/> </root></configuration>
2: The test class is
@RunWith (Springjunit4classrunner.class) @ContextConfiguration ({"Classpath:spring/spring-dao.xml", "Classpath:spring/spring-service.xml"}) Public classTestuservice {Private FinalLogger Logger = Loggerfactory.getlogger ( This. GetClass ()); @AutowiredPrivateTestuserservice Testuserservice; @Test Public voidGettn () {List<String> mm=Testuserservice.getallusername (); Logger.info ("Allname={}", mm.tostring ()); }}
Test Pass: Description Spring configuration complete
This time our file structure is:
And then the last step is to do
Configuration of the Spring-web:
development of Spring+springmvc+mybatis framework with IntelliJ idea step-up four: Configuration Springmvc
Using IntelliJ idea to develop a spring+springmvc+mybatis framework step-up build three: Configure Spring and test