计算机系统应用教程网站

网站首页 > 技术文章 正文

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

btikc 2024-09-16 12:55:37 技术文章 26 ℃ 0 评论

为了深入学习主流的Java配置类写法,我们要学会把xml文件转化为配置类。

今天就由三岁带大家学习一下XML和配置类间的联系和转化~

XML

java类

备注

<beans></beans>标签

@Configuration

通常一个XML文件对应一个@Configuration

<bean></bean>标签

@Bean或者@Component("id名")

方法用@Bean,类用@Component

id

方法名


class

方法的返回值类型


<property>

类定义的属性


<constructor-arg name="name" value="张三"></constructor-arg>

类构造器的参数


new ClassPathXmlApplicationContext("beans.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"
       xmlns:aop = "http://www.springframework.org/schema/aop"
       xsi:schemaLocation = "http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-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&useUnicode=true&characterEncoding=utf-8&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("a13434157245");
        return driverManagerDataSource;
    }
}
  • XML文件中的<beans></beans>标签,对应类上面使用的@Configuration注解。
  • XML文件中的<bean></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配置类。

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表