如何用注解把XML配置文件转成Java配置类

如何用注解把XML配置文件转化成@Configuration类

XMLjava类备注
beans标签@Configuration通常一个XML文件对应一个@Configuration
bean标签@Bean或者@Component(“id名”)方法用@Bean,类用@Component
id方法名
class方法的返回值类型
property 标签类定义的属性
constructor-arg 标签类构造器的参数
new ClassPathXmlApplicationContext(“applicationContext.xml”)new AnnotationConfigApplicationContext(MyConfig.class)获取应用上下文context

1、看以下例子

<?xml version = "1.0" encoding = "UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
       xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation = "http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd>
    <!--配置数据源DataSource -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/houkydb?useSSL=false&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;serverTimezone=Asia/Shanghai"/>
        <property name="username" value="root" />
        <property name="password" value="a13434157245" />
    </bean>
</beans>

以上是xml文件中的一个bean,把他转化为一个方法

我们先新建一个类:MyConfig,然后在里面定义一个dataSource方法。

@Configuration
public class MyConfig {
    @Bean
    public DriverManagerDataSource dataSource() {
        DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
        driverManagerDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        driverManagerDataSource.setUrl("jdbc:mysql://127.0.0.1:3306/houkydb?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai");
        driverManagerDataSource.setUsername("root");
        driverManagerDataSource.setPassword("123456");
        return driverManagerDataSource;
    }
}
  • XML文件中的<beans 标签,对应类上面使用的@Configuration注解。
  • XML文件中的<bean 标签,对应方法dataSource()上面使用的@Bean注解。
  • id=“dataSource” 对应 方法名dataSource ;
  • class=“org.springframework.jdbc.datasource.DriverManagerDataSource” 对应 返回值的类型DriverManagerDataSource;
  • <property 标签对应类里面的属性,name对应属性名,value对应属性值。

测试是否能拿到bean类:

public class MyTest {
    @Test
    public void test01(){
        //通过xml文件配置bean,用ClassPathXmlApplicationContext
//        ApplicationContext context=new ClassPathXmlApplicationContext("spring-dao.xml");
//        通过java配置类配置bean,用AnnotationConfigApplicationContext
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
        DriverManagerDataSource dataSource = context.getBean("dataSource", DriverManagerDataSource.class);
        System.out.println(dataSource);
    }
}

2、再看第二个例子

pojo包下的实体类Teacher

@Component
@Data
public class Teacher {
    private String name;
    private int id;

    public Teacher(String name, int id) {
        this.name = name;
        this.id = id;
    }

    public Teacher() {
    }
}

XML文件中

<bean id="teacher" class="com.houky.pojo.Teacher">
    <constructor-arg name="name" value="陈老师"/>
    <constructor-arg name="id" value="1"/>
</bean>

转成java类:①加上@Component注解 ②用@Value赋值(不赋值也可以,默认为null)

@Component
@Data
public class Teacher {
    @Value("陈老师")
    private String name;
    @Value("1")
    private int id;

    public Teacher(String name, int id) {
        this.name = name;
        this.id = id;
    }
    public Teacher() {
    }
}

再看MyConfig类:加上@ComponentScan(“com.houky.pojo”),作用是扫描pojo包下的所有component

@Configuration
@ComponentScan("com.houky.pojo")//扫描pojo包下的所有component
public class MyConfig {
}

测试是否能拿到Bean类:

    @Test
    public void test02(){
//        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
        Teacher teacher = context.getBean("teacher", Teacher.class);
        System.out.println(teacher);
    }

测试成功!

另外:如果实体类定义在MyConfig内部,则不需要用扫包注解@ComponentScan

@Configuration
public class MyConfig {
    
@Component
@Data
public class Teacher {//内部类,可以自动被MyConfig扫描到
    @Value("陈老师")
    private String name;
    @Value("1")
    private int id;
    public Teacher(String name, int id) {
        this.name = name;
        this.id = id;
    }
    public Teacher() {
    }
}
}

总结:

以上就是如何把XML文件转化成Java配置类的教程了,纯手打~

如果要掌握springboot,则必须学会把spring中的XML配置文件都转化成Java配置类。

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值