java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Spring xml配置文件使用

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

作者:石小浪♪

这篇文章主要介绍了Spring中xml配置文件的基础使用方式,主要包括xml配置文件的读取,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

1. xml配置文件的读取

目录结构

applicationContext.xml配置文件

<?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-4.3.xsd">
    
    <!--通过无参构造创建对象-->
    <bean id = "userDao" class="org.example.POI.UserDaoImpl"/>
 
</beans>

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 + '\'' +
                '}';
    }
}
<!-- 调用有参构造,反射创建对象-->
<bean id = "user" class="org.example.pojo.User">
    <constructor-arg name="age" value="18"></constructor-arg>
    <constructor-arg name="userName" value="貂蝉"></constructor-arg>
</bean>
    //创建带参构造对象
    @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!");
    }
}
 <!--调用另一个类中的方法创建对象并将其加入到Spring容器中-->
    <bean id="userDaoService" class="org.example.POI.UserServiceImpl"></bean>
    <!--factory-bean: 另一个类的id  factory-method: 另一个类中创建指定对象的方法名-->
    <bean id="userDaoByService" factory-bean="userDaoService" factory-method="createUserDao"></bean>
    //调用另一个类中的方法创建对象
    @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!");
    }
}
  <!--调用类中的静态方法创建对象存放到Spring容器中-->
    <bean id="userDaoByService1" class="org.example.POI.UserServiceImpl" factory-method="createUserDao1"></bean>
    //调用另一个类中的静态方法创建对象
    @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();
    }
  <!--对象的生命周期-->
  <!--init-method: 创建容器时调用demo01函数-->
  <!--destroy-method: 销毁容器时调用demo02函数-->
    <bean id="lifecycle" init-method="demo01" destroy-method="demo02" class="org.example.POI.UserDaoImpl"></bean>

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);
    }

多例

  <bean id = "userDao" scope="prototype" class="org.example.POI.UserDaoImpl"/>

默认单例

<bean id = "userDao" scope="singleton" class="org.example.POI.UserDaoImpl"/>

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

您可能感兴趣的文章:
阅读全文