Spring Applicationcontext.xml Detailed and template

Source: Internet
Author: User

Applicationcontext.xml file
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: context= "Http://www.springframework.org/schema/context" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns: p= "http://www.springframework.org/schema/p" xmlns:util= "Http://www.springframework.org/schema/util" xmlns:jdbc= " Http://www.springframework.org/schema/jdbc "xmlns:cache=" Http://www.springframework.org/schema/cache "xsi: schemalocation= "Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context.xsd Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/    Spring-beans.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd Http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/Spring-jdbc-3.1.xsd Http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/ Spring-cache-3.1.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop.xsd Http://www.springframework.org/schema/util http://www.springframework.org/schema/util/ Spring-util.xsd "> <<span style=" color: #ff0000; " >!--automatically scan Web packages, incorporating annotated classes into Spring container management--></span> <context:component-scan base-package= " Com.eduoinfo.finances.bank.web "></context:component-scan> <!--introducing the JDBC configuration file--<bean id=" Propertyc Onfigurer "class=" Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer "> <property name=" Locations "> <list> <value>classpath*:jdbc.properties</value> &lt ;/list> </property> </bean> <!--dataSource configuration-<bean id= "DataSource" class= "Co M.alibaba.druid.pool.Druiddatasource "init-method=" Init "destroy-method=" Close "> <!--basic property URL, user, password--<pro Perty name= "url" value= "${jdbc.url}"/> <property name= "username" value= "${jdbc.username}"/> <p Roperty name= "Password" value= "${jdbc.password}"/> <!--configuration initialization size, MIN, max--and <property name= "Initi  Alsize "value=" 1 "/> <property name=" Minidle "value=" 1 "/> <property name=" maxactive "value=" 20 " /> <!--configuration Get connection Wait timeout time-<property name= "maxwait" value= "60000"/> <!--configuration interval One detection, detects idle connections that need to be closed, in milliseconds-<property name= "Timebetweenevictionrunsmillis" value= "60000"/> <!-- Configures the minimum lifetime of a connection in a pool, in milliseconds-<property name= "Minevictableidletimemillis" value= "300000"/> <proper Ty name= "validationquery" value= "select ' X '"/> <property name= "Testwhileidle" value= "true"/> < Property Name= "TestonborRow "value=" false "/> <property name=" Testonreturn "value=" false "/> <!--open Pscache and specify PSC on each connection Ache Size--<property name= "Poolpreparedstatements" value= "false"/> <property name= "Maxpoolprep  Aredstatementperconnectionsize "value="/> <!--Configuration Monitoring statistics interception filters--<property name= "Filters" value= "stat"/> </bean> <!--mybatis file configuration, scan all mapper files--<bean id= "Sqlsessionfactory" class= " Org.mybatis.spring.SqlSessionFactoryBean "p:datasource-ref=" DataSource "p:configlocation=" classpath: Mybatis-config.xml "p:mapperlocations=" Classpath:com/eduoinfo/finances/bank/web/dao/*.xml "/> <span style=" Color: #ff0000; " ><!--Spring and mybatis integrated configuration, scanning all DAO--></span> <bean class= " Org.mybatis.spring.mapper.MapperScannerConfigurer "p:basepackage=" Com.eduoinfo.finances.bank.web.dao "P: Sqlsessionfactorybeanname= "Sqlsessionfactory"/> <!--transaction management of the DataSource data source-<Bean id= "TransactionManager" class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" P: datasource-ref= "DataSource"/> <span style= "color: #ff0000;" ><!--Configuration makes spring adopt Cglib proxy--></span> <aop:aspectj-autoproxy proxy-target-class= "true"/> <spa n style= "color: #ff0000;" > <!--enable support for transaction annotations--></span> <tx:annotation-driven transaction-manager= "TransactionManager"/>& Lt;span style= "color: #ff0000;" > <!--cache configuration--></span> <cache:annotation-driven cache-manager= "CacheManager"/> <bean Id= "Ehcachemanagerfactory" class= "Org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configlocation= "Classpath:ehcache.xml"/> <bean id= "CacheManager" class= " Org.springframework.cache.ehcache.EhCacheCacheManager "p:cachemanager-ref=" Ehcachemanagerfactory "/></ Beans>

  


1, <context:component-scan base-package= "Com.eduoinfo.finances.bank.web" ></context:component-scan> When the spring container is initialized, it scans the com.eduoinfo.finances.bank.web labeled (@Component, @Service, @Controller, @Repository) Annotated class Incorporating spring container management

On the class, implement the Bean declaration using the following annotations

@Component refer to components, we can use this annotation when the component is poorly categorized.

@Service for labeling business layer components

@Controller for labeling Control-layer components (such as action in Controller,struts of srping MVC)

@Repository for labeling data Access Components, DAO components

Example:

@Controller
@RequestMapping (value = "/test")
public class TestController {

}

--------------------------------------------------------------------------------------------------------------- ---

On the member variables of the class, use the following annotations to implement automatic assembly of properties

@Autowired: assembly by type of class

@Resource (Recommended): 1 if both name and type are specified, the uniquely matched bean is found from the spring context to assemble, and an exception is thrown if it is not found

2. If name is specified, a bean matching the name (ID) from the context is assembled, and an exception is thrown if it is not found

3. If the type is specified, an exception is thrown when the unique bean matching the match is found in the context, cannot be found, or multiple occurrences are found

4. If neither name is specified and type is not specified, the assembly is automatically byname; if there is no match, the fallback is a match for the original type, and if the match is automatically assembled;

@Resource annotation is on the field, so there is no need to write setter methods, and this annotation belongs to the Java EE, which reduces the coupling with spring.

Example:

@Resource
Private Testserviceimpl Testserviceimpl;

Applicationcontext.xml templates:

<?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:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= " Http://www.springframework.org/schema/tx "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/tx        http://www.springframework.org/ Schema/tx/spring-tx-3.0.xsd       http://www.springframework.org/schema/context/       http Www.springframework.org/schema/context/spring-context-3.0.xsd       HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "></beans>

  

Spring Applicationcontext.xml Detailed and template

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.