龙空技术网

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

IT凡三岁 51

前言:

今天咱们对“xml转为java”都比较讲究,姐妹们都需要了解一些“xml转为java”的相关内容。那么小编也在网络上网罗了一些对于“xml转为java””的相关文章,希望兄弟们能喜欢,姐妹们快快来了解一下吧!

为了深入学习主流的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 = ";       xmlns:xsi = ";       xmlns:aop = ";       xsi:schemaLocation = "                         ">    <!--配置数据源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方法。

@Configurationpublic 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@Datapublic 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@Datapublic 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包下的所有componentpublic 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

@Configurationpublic class MyConfig {    @Component@Datapublic 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配置类。

标签: #xml转为java #xsd生成java类 #xml转换java