Detail the spring applicationContext.xml configuration file
- 2020-06-01 09:52:43
- OfStack
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">
<!-- Automatic scanning web package , Class that will be annotated Included in the spring Container management -->
<context:component-scan base-package="com.eduoinfo.finances.bank.web"></context:component-scan>
<!-- The introduction of jdbc The configuration file -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:jdbc.properties</value>
</list>
</property>
</bean>
<!-- dataSource configuration -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- Basic attributes url , user , password -->
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- Configure the initial size, minimum, and maximum -->
<property name="initialSize" value="1" />
<property name="minIdle" value="1" />
<property name="maxActive" value="20" />
<!-- Configure to get the time for the connection wait timeout -->
<property name="maxWait" value="60000" />
<!-- How long is the configuration interval 1 A secondary detection that detects idle connections that need to be closed in milliseconds -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- configuration 1 The minimum time a connection can live in a pool, in milliseconds -->
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="validationQuery" value="SELECT 'x'" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<!-- Open the PSCache , and specify each connection PSCache The size of the -->
<property name="poolPreparedStatements" value="false" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
<!-- Configure monitoring statistics intercept filters -->
<property name="filters" value="stat" />
</bean>
<!-- mybatis File configuration, scan all mapper file -->
<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" />
<!-- spring with mybatis Integrate the configuration and scan all dao -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" p:basePackage="com.eduoinfo.finances.bank.web.dao" p:sqlSessionFactoryBeanName="sqlSessionFactory" />
<!-- right dataSource Data source for transaction management -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource" />
<!-- Configuration to make Spring using CGLIB The agent -->
<aop:aspectj-autoproxy proxy-target-class="true" />
<!-- Enable support for transaction annotations -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Cache configuration -->
<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
>
Initialization function Spring container, will scan com. eduoinfo. finances. bank. Under web marked (@ Component, @ Service @ Controller, @ Repository) annotation class into spring container management
On the class, implement the bean declaration using the following annotations
Component refers to components in general, and we can use this annotation to annotate components when they are not easy to categorize.
@Service is used to annotate business-tier components
@Controller is used to annotate control layer components (e.g. controller of srping mvc, action of struts)
@Repository is used to annotate the data access component, the DAO component
Example:
@Controller
@RequestMapping(value = "/test")
public class TestController {
}
On the member variables of the class, use the following annotations to implement the automatic assembly of properties
Autowired: assemble by type of class
Resource (recommended) :
1 if both name and type are specified, bean with only 1 matching is found in the context of spring for assembly, and an exception is thrown if it is not found
2. If name is specified, bean with a matching name (id) is searched from the context for assembly, and an exception is thrown if it is not found
3. If type is specified, only 1bean with a type matching is found in the context for assembly. If it is not found or more than one is found, an exception will be thrown
4. If neither name nor type is specified, the assembly will be automatically carried out in the way of byName; If there is no match, it will be matched back to 1 original type. If there is a match, it will be automatically assembled.
The @Resource annotation is on the field, so you don't have to write the setter method, and the annotation belongs to J2EE, reducing the coupling to spring.
Example:
@Resource
private TestServiceImpl testServiceImpl;