Spring中xml配置文件的基础使用方式详解

1. xml配置文件的读取

目录结构

Spring中xml配置文件的基础使用方式详解_第1张图片

applicationContext.xml配置文件



    
    
    
 

1.1 通过类路径读取配置文件

package org.example.POI;
 
public interface UserDao {
    public void say();
}
package org.example.POI;
 
public class UserDaoImpl implements UserDao{
    @Override
    public void say() {
        System.out.println("userDao say Hello World!");
    }
}
    @Test
    public void shouldAnswerWithTrue()
    {
        //读取配置文件,创建Spring容器, 根据类路径读取
        ApplicationContext context =
                new ClassPathXmlApplicationContext("applicationContext.xml");
        //根据id获取对象
        UserDao userDao = (UserDao) context.getBean("userDao");
        UserDao userDao2 = (UserDao) context.getBean("userDao");
        //使用对象调用方法
        userDao.say();
        //默认单例
        System.out.println(userDao == userDao2);
 
    }

1.2 通过文件系统绝对路径读取配置文件

    @Test
    public void Test01(){
        //根据文件系统的绝对路径读取配置文件
        ApplicationContext context1 =
                new FileSystemXmlApplicationContext("E:\\绝对路径 "+          
        "\\src\\main\\resource\\applicationContext.xml");
        UserDao userDao1 = (UserDao) context1.getBean("userDao");
        UserDao userDao2 = (UserDao) context1.getBean("userDao");
 
        userDao1.say();
        //默认单例
        System.out.println(userDao1 == userDao2);
    }

1.3使用BeanFactory接口读取配置文件

    @Test
    public void diffrentTest(){
        //将配置文件信息封装到Resource对象中
        ClassPathResource resource = new ClassPathResource("applicationContext.xml");
        //通过Resource对象创建容器
        BeanFactory beanFactory = new XmlBeanFactory(resource);
        //通过id获取对应的对象
        UserDao userDao = (UserDao) beanFactory.getBean("userDao");
        UserDao userDao1 = (UserDao) beanFactory.getBean("userDao");
        userDao.say();
        System.out.println(userDao == userDao1);
    }

BeanFactory 创建对象时才调用构造函数创建对象 延迟加载

ApplicationContext 在创建容器时就调用构造创建对象(读取配置文件后马上创建对象) 立即加载

2.带参构造对象的创建(constructor-arg标签)

public class User {
    private Integer age;
    private String userName;
 
    public User() {
    }
 
    public User(Integer age, String userName) {
        this.age = age;
        this.userName = userName;
    }
 
    @Override
    public String toString() {
        return "User{" +
                "age=" + age +
                ", userName='" + userName + '\'' +
                '}';
    }
}


    
    
    //创建带参构造对象
    @Test
    public void haveArgStruct(){
        ApplicationContext context =
                new ClassPathXmlApplicationContext("applicationContext.xml");
        User user = (User) context.getBean("user");
        System.out.println(user);
    }

3.使用另一个类中的方法创建对象,并放到Spring容器中

package org.example.POI;
 
public interface UserService {
    public void say();
}
public class UserServiceImpl implements UserService{
    private UserDao userDao;
    public  void setUserDao(UserDao userDao){
        this.userDao = userDao;
    }
 
    public UserDao createUserDao(){
        System.out.println("我是UserServiceImpl,我创建了UserDao----");
        UserDao userDao = new UserDaoImpl();
        return userDao;
    }
 
    @Override
    public void say() {
        this.userDao.say();
        System.out.println("userService say hello World!");
    }
}
 
    
    
    
    //调用另一个类中的方法创建对象
    @Test
    public void userDaoByUserService(){
        ApplicationContext context =
                new ClassPathXmlApplicationContext("applicationContext.xml");
        UserDao userDao = (UserDao) context.getBean("userDaoByService");
        userDao.say();
    }

4.调用另一个类中的静态方法创建对象,并放到Spring容器中

public class UserServiceImpl implements UserService{
    private UserDao userDao;
    public  void setUserDao(UserDao userDao){
        this.userDao = userDao;
    }
 
    public static UserDao createUserDao1(){
        System.out.println("我是static createUserDao1,我创建了UserDao----");
        return new UserDaoImpl();
    }
 
    @Override
    public void say() {
        this.userDao.say();
        System.out.println("userService say hello World!");
    }
}
  
    
    //调用另一个类中的静态方法创建对象
    @Test
    public void userDaoByUserService1(){
        ApplicationContext context =
                new ClassPathXmlApplicationContext("applicationContext.xml");
        UserDao userDao = (UserDao) context.getBean("userDaoByService1");
        userDao.say();
    }

5.对象的生命周期

package org.example.POI;
 
public class UserDaoImpl implements UserDao{
    public UserDaoImpl(){
        System.out.println("UserDaoImpl无参构造调用--------");
    }
    @Override
    public void say() {
        System.out.println("userDao say Hello World!");
    }
    public void demo01(){
        System.out.println("init obj!");
    }
    public void demo02(){
        System.out.println("destroy obj!");
    }
}
    //对象的生命周期
    @Test
    public void lifecycle(){
        //创建容器
        ApplicationContext context = 
                new ClassPathXmlApplicationContext("applicationContext.xml");
        //销毁容器
        ((ClassPathXmlApplicationContext)context).close();
    }
  
  
  
    

Spring中xml配置文件的基础使用方式详解_第2张图片

6.单例多例的测试

package org.example.POI;
 
public class UserDaoImpl implements UserDao{
    public UserDaoImpl(){
        System.out.println("UserDaoImpl无参构造调用--------");
    }
    @Override
    public void say() {
        System.out.println("userDao say Hello World!");
    }
}
    @Test
    public void shouldAnswerWithTrue()
    {
        //读取配置文件,读取Spring容器, 根据类路径读取
        ApplicationContext context =
                new ClassPathXmlApplicationContext("applicationContext.xml");
        //根据id获取对象
        UserDao userDao = (UserDao) context.getBean("userDao");
        UserDao userDao2 = (UserDao) context.getBean("userDao");
        //使用对象调用方法
        userDao.say();
        //默认单例
        System.out.println(userDao == userDao2);
    }

多例

  

Spring中xml配置文件的基础使用方式详解_第3张图片

默认单例

Spring中xml配置文件的基础使用方式详解_第4张图片

到此这篇关于Spring中xml配置文件的基础使用方式的文章就介绍到这了,更多相关Spring xml配置文件使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(Spring中xml配置文件的基础使用方式详解)