Spring注入,xml方式和注解@Bean


思路:搭建环境,测试xml 和 @configuration的效果,比较写法的不同。

1.新建一个spring空项目。

 2.测试目录下新建测试的资源文件和配置文件。

 3.创建bean类

package com.example.springbean_stu01.bean;

public class UserBean {
private String Username;
private String password;
public UserBean(){
System.out.println("无参构造");
};
public UserBean(String username, String password) {
System.out.println("有参构造");
Username = username;
this.password = password;
}

public String getUsername() {
System.out.println("getUsername");
return Username;
}

public void setUsername(String username) {
System.out.println("setUsername");
Username = username;
}

public String getPassword() {
System.out.println("getPassword");
return password;
}

public void setPassword(String password) {
System.out.println("setPassword");
this.password = password;
}
}

 可以从@Bean的源码中看到,name<=>value,默认值就是方法名,这里如果不写name = "userBean",等价于name ="user"。

4.2写测试类

package com.example.springbean_stu01;

import com.example.springbean_stu01.bean.UserBean;
import com.example.springbean_stu01.configurations.TestConfig;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

@SpringBootTest
class SpringBeanStu01ApplicationTests {

    @Test
    void contextLoadsByAnnotation() {
        ApplicationContext context = new AnnotationConfigApplicationContext(TestConfig.class);
        UserBean ub = (UserBean) context.getBean("userBean");
        System.out.println(ub.getUsername());
    }

    @Test
    void contextLoadsByXml() {
        ApplicationContext context = new AnnotationConfigApplicationContext(TestConfig.class);
        UserBean ub = (UserBean) context.getBean("user");
        System.out.println(ub.getUsername());
    }
}

运行contextLoadsByAnnotation效果如下

 可以看到,bean被正确的注入了。

-----------------------------------------------------------------------------------分割线------------------------------------------------------------------------------------------

接下来测xml

5.1写配置bean的xml,//testbean.xml

<?xml version="1.0" encoding="UTF-8"?>


    
        User-xml
        123
    

5.2写测试类

package com.example.springbean_stu01;

import com.example.springbean_stu01.bean.UserBean;
import com.example.springbean_stu01.configurations.TestConfig;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

@SpringBootTest
class SpringBeanStu01ApplicationTests {

    @Test
    void contextLoadsByAnnotation() {
        ApplicationContext context = new AnnotationConfigApplicationContext(TestConfig.class);
        UserBean ub = (UserBean) context.getBean("userBean");
        System.out.println(ub.getUsername());
    }

    @Test
    void contextLoadsByXml() {
        ApplicationContext context = new ClassPathXmlApplicationContext("testbean.xml");
        UserBean ub = (UserBean) context.getBean("userBean");
        System.out.println(ub.getUsername());
    }
}

运行contextLoadsByXml()方法,得到预期的输出如下

 6.总结一波

a.@Configuration配合@Bean,实现无xml自动注入。

b.xml方式写

<bean id="必须唯一,默认类名" class="类的全路径,类必须有无参的构造方法,不然编译报错。">

     <property name="bean实体的属性名,通过调用setter方法赋值" value="属性值">property> bean>

拓展:

xml中,bean标签id改成user,Test时报错;在bean中添加name="userBean" ,可以成功运行。

那么,bean标签中,id和name的关系可以这么理解,id和name作用等同,在同一个bean标签中可以重复可以不同;一旦id或name被使用过,在其他的bean标签中,无论是id还是name都不能在使用。例如

正确案例1:

正确案例2:

错误案例1: 

相关