spring解析占位符分析,如@Value注解,xml文件中配置

示例:

配置文件:

<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:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="
	http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"
    default-lazy-init="false">

  <bean id="student" class="com.spring.bean.Student">
    <property name="name" value="${test}"/>
  </bean>
  <context:component-scan base-package="com.spring"/>
  <context:property-placeholder location="classpath:test2.properties,classpath:test.properties"/>
</beans>

 test.properties:

test:study

test2.properties:

test:play

实例: 

package com.spring.bean;

import lombok.Data;

import javax.annotation.PostConstruct;

@Data
public class Student extends Person  {

    private String name;

    private  int age;

    @Override
    public String show(String str) {
        System.out.println("Studeng:test()");
        return str;
    }

}

测试:

 @org.junit.Test
    public void test7() throws InterruptedException {

        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        Student bean =(Student)context.getBean("student");

        System.out.println("Student:"+bean.getName());

    }

结果:

 源码分析:自定义标签解析PropertyPlaceholderBeanDefinitionParser

解析配置文件中的值并赋值:PropertySourcesPlaceholderConfigurer实现了BeanFactoryPostProcessor接口

 在这儿加入到embeddedValueResolvers容器中:

 以解析string为例:

解析@value注解,通过AutowiredAnnotationBeanPostProcessor

 这里从embeddedValueResolvers容器中获取解析:

注意:如果多个配置文件中的key值相同,value值会取最后配置的,因为会覆盖掉前面的值,例如: <context:property-placeholder location="classpath:test2.properties,classpath:test.properties"/>最后name的值为study;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring使用`@Mapper`注解XML之间实现绑定是通过MyBatis来实现的。MyBatis是一个开源的持久层框架,与Spring集成后可以方便地进行数据库操作。 首先,确保你的项目已经引入了MyBatis和MyBatis-Spring的相关依赖。然后,在你的Mapper接口上使用`@Mapper`注解,标识该接口为MyBatis的Mapper接口,示例代码如下: ```java @Mapper public interface UserMapper { User getUserById(Long id); } ``` 接下来,创建一个与Mapper接口同名的XML文件,在XML文件定义对应的SQL语句。XML文件需要放置在与Mapper接口相同的包路径下,并且以Mapper接口的全限定名为命名空间,示例代码如下: ```xml <!-- UserMapper.xml --> <mapper namespace="com.example.mapper.UserMapper"> <select id="getUserById" resultType="com.example.model.User"> SELECT * FROM user WHERE id = #{id} </select> </mapper> ``` 在XML文件,你可以定义各种SQL语句,例如查询、插入、更新等。注意,SQL语句的参数需要使用`#{}`占位符来引用Mapper接口的方法参数。 最后,在Spring配置文件配置MyBatis的相关信息,例如数据源、事务管理等。示例代码如下: ```xml <!-- applicationContext.xml --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <!-- 配置数据源信息 --> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath*:com/example/mapper/*.xml"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.example.mapper"/> </bean> ``` 通过上述配置Spring会自动扫描指定包路径下的Mapper接口,并将其与对应的XML文件进行绑定。这样,你就可以在业务代码直接注入Mapper接口,使用MyBatis进行数据库操作了。 希望以上信息能对你有所帮助!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值