目录
- jdbc.driver=com.mysql.jdbc.Driver
- jdbc.url=jdbc:mysql://localhost:3306/db02
- jdbc.username=root
- jdbc.password=123456
然后在配置文件中加载创建的配置文件,同时使用其中的配置信息
- "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"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
-
- <context:property-placeholder location="classpath:jdbc.properties"/>
-
- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
- <property name="driverClassName" value="${jdbc.driver}"/>
- <property name="url" value="${jdbc.url}"/>
- <property name="username" value="${jdbc.username}"/>
- <property name="password" value="${jdbc.password}"/>
- bean>
-
- <bean class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource"/>
- bean>
-
-
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.example.Mapper"/>
- bean>
-
- <bean id="userService" class="com.example.Service.Impl.UserServiceImpl">
- <property name="empMapper" ref="empMapper">property>
- <property name="userDAO" ref="userDAO"/>
- <property name="name" value="hhhh"/>
- bean>
-
- <bean name="userDAO" class="com.example.DAO.Impl.UserDAOImpl"/>
- beans>
- "1.0" encoding="UTF-8"?>
-
- <xsd:schema xmlns="http://www.example.com/haohao"
- xmlns:xsd="http://www.w3.org/2001/XMLSchema"
- targetNamespace="http://www.example.com/haohao">
-
- <xsd:element name="annotation-driven">xsd:element>
-
- xsd:schema>
http\://www.example.com/haohao/haohao-annotation.xsd=com/example/haohao/configure/haohao-annotation.xsd
spring.handlers
http\://www.example.com/haohao=com.example.handlers.HaoHaoNamespaceHandler
- package com.example.handlers;
-
-
- import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
-
- public class HaoHaoNamespaceHandler extends NamespaceHandlerSupport {
- @Override
- public void init() {
- // 初始化,一般情况下,一个命名空间下有多个标签,会在当前init方法中为每一个标签注册一个标签解析器
- this.registerBeanDefinitionParser("annotation-driven", new HaoHaoBeanDefinitionParser());
- }
- }
- package com.example.handlers;
-
- import org.springframework.beans.factory.config.BeanDefinition;
- import org.springframework.beans.factory.support.RootBeanDefinition;
- import org.springframework.beans.factory.xml.ParserContext;
- import org.w3c.dom.Element;
-
- public class HaoHaoBeanDefinitionParser implements org.springframework.beans.factory.xml.BeanDefinitionParser {
- @Override
- public BeanDefinition parse(Element element, ParserContext parserContext) {
- // 注入一个BeanPostProcessor
- BeanDefinition beanDefinition = new RootBeanDefinition();
- beanDefinition.setBeanClassName("com.example.PostProcessor.HaoHaoBeanPostProcessor");
- parserContext.getRegistry().registerBeanDefinition("HaoHaoBeanPostProcessor", beanDefinition);
- return beanDefinition;
- }
- }
- package com.example.PostProcessor;
-
- import org.springframework.beans.BeansException;
- import org.springframework.beans.factory.config.BeanPostProcessor;
-
- public class HaoHaoBeanPostProcessor implements BeanPostProcessor {
-
- @Override
- public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
- System.out.println("HaoHaoBeanPostProcessor执行......");
- return bean;
- }
- }