Spring创建对象和常用API测试,对Spring框架的解析-----Spring框架

package com.powernode.spring6.test;

import com.powernode.Spring6.bean.User;
import com.powernode.Spring6.dao.UserDaoImplForMySQL;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import java.text.SimpleDateFormat;
import java.util.Date;

public class firstSpringTest
{
    @Test
    public void TestSpringCode()
    {
        //获取Spring容器对象,根据bean的ID获取这个类对象,ApplicationContext翻译为应用上下文,其实就是Spring容器
        //ApplicationContext是一个接口,这个接口下有很多实现类,其中有一个实现类叫做ClassPathXmlApplicationContext专门从类路径加载Spring配置文件的Spring上下文对象
        //只要该代码执行,就启动了Spring容器,并且会解析XML文件,并且将对象创建出来,放置在Spring容器中
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring6.xml","bean.xml","xml/beans.xml");
        //第二步,根据bean的ID从Spring容器中获取该对象
        Object userBean = applicationContext.getBean("userBean");
        System.out.println(userBean);
        //如果该Bean类没有可调用的无参数构造方法就会报错
        Object userDao = applicationContext.getBean("userDaoBean");
        UserDaoImplForMySQL userDaoBean = applicationContext.getBean("userDaoBean",UserDaoImplForMySQL.class);
        userDaoBean.insert();
        System.out.println(applicationContext.getBean("vip"));
        System.out.println(userDao);
        //Spring将创建好的对象存到一个Map中
        //key(ID),"userBean",value(bean)User对象
        Object obj = userBean.getClass();
        obj = com.powernode.Spring6.bean.User.class;
        //Spring配置文件的名字可以自拟
        System.out.println(applicationContext.getBean("userBean2"));
        obj = applicationContext.getBean("nowTime");
        System.out.println(obj);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
        String nowTime = simpleDateFormat.format((Date)obj);
        //不强制类型转换方法如下
        Date date = applicationContext.getBean("nowTime",Date.class);
        //通过第二个参数指定返回类型
        nowTime = simpleDateFormat.format(date);
        //如果bean的ID不存在,不会返回Null,而是直接报异常信息
//        obj = applicationContext.getBean("nowTime2");
        System.out.println(nowTime);
    }
    @Test
    public void TestXMLPath()
    {
        //FileSystemXmlApplicationContext不是从类路径中加载xml
        ApplicationContext applicationContext = new FileSystemXmlApplicationContext("D:\\File\\Spring6\\Spring6-002-first\\target\\classes\\spring6.xml");
        User u = applicationContext.getBean("userBean", User.class);
        System.out.println(u);
    }
    @Test
    public void TestXMLBeanFactory()
    {
        //ApplicationContext接口的超级父接口是BeanFactory,即Bean工厂,能够生产bean对象的一个工厂对象,他是IOC容器的超级接口
        //Spring容器IOC底层实际上使用了工厂模式和XML解析加反射机制
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("spring6.xml");
        User u = beanFactory.getBean("userBean",User.class);
        System.out.println(u);
    }
    @Test
    public void TestBeginInitBean()
    {
        //不是在getBean方法调用时才创建对象,执行解析xml文件时就已经创建对象了
        new ClassPathXmlApplicationContext("spring6.xml");
    }
}

package com.powernode.spring6.test;

import com.powernode.Spring6.bean.User;
import com.powernode.Spring6.dao.UserDaoImplForMySQL;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import java.text.SimpleDateFormat;
import java.util.Date;

public class firstSpringTest
{
@Test
public void TestSpringCode()
{
//获取Spring容器对象,根据bean的ID获取这个类对象,ApplicationContext翻译为应用上下文,其实就是Spring容器
//ApplicationContext是一个接口,这个接口下有很多实现类,其中有一个实现类叫做ClassPathXmlApplicationContext专门从类路径加载Spring配置文件的Spring上下文对象
//只要该代码执行,就启动了Spring容器,并且会解析XML文件,并且将对象创建出来,放置在Spring容器中
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring6.xml","bean.xml","xml/beans.xml");
//第二步,根据bean的ID从Spring容器中获取该对象
Object userBean = applicationContext.getBean("userBean");
System.out.println(userBean);
//如果该Bean类没有可调用的无参数构造方法就会报错
Object userDao = applicationContext.getBean("userDaoBean");
UserDaoImplForMySQL userDaoBean = applicationContext.getBean("userDaoBean",UserDaoImplForMySQL.class);
userDaoBean.insert();
System.out.println(applicationContext.getBean("vip"));
System.out.println(userDao);
//Spring将创建好的对象存到一个Map
//key(ID),"userBean",value(bean)User对象
Object obj = userBean.getClass();
obj = com.powernode.Spring6.bean.User.class;
//Spring配置文件的名字可以自拟
System.out.println(applicationContext.getBean("userBean2"));
obj = applicationContext.getBean("nowTime");
System.out.println(obj);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
String nowTime = simpleDateFormat.format((Date)obj);
//不强制类型转换方法如下
Date date = applicationContext.getBean("nowTime",Date.class);
//通过第二个参数指定返回类型
nowTime = simpleDateFormat.format(date);
//如果bean的ID不存在,不会返回Null,而是直接报异常信息
// obj = applicationContext.getBean("nowTime2");
System.out.println(nowTime);
}
@Test
public void TestXMLPath()
{
//FileSystemXmlApplicationContext不是从类路径中加载xml
ApplicationContext applicationContext = new FileSystemXmlApplicationContext("D:\\File\\Spring6\\Spring6-002-first\\target\\classes\\spring6.xml");
User u = applicationContext.getBean("userBean", User.class);
System.out.println(u);
}
@Test
public void TestXMLBeanFactory()
{
//ApplicationContext接口的超级父接口是BeanFactory,即Bean工厂,能够生产bean对象的一个工厂对象,他是IOC容器的超级接口
//Spring容器IOC底层实际上使用了工厂模式和XML解析加反射机制
BeanFactory beanFactory = new ClassPathXmlApplicationContext("spring6.xml");
User u = beanFactory.getBean("userBean",User.class);
System.out.println(u);
}
@Test
public void TestBeginInitBean()
{
//不是在getBean方法调用时才创建对象,执行解析xml文件时就已经创建对象了
new ClassPathXmlApplicationContext("spring6.xml");
}
}



    
    
    
    
    

    
    


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.xsd">








package com.powernode.Spring6.bean;

public class User
{
    //Spring是通过反射机制,调用类的无参数构造方法来实例化对象
    //原理如下,Class clazz = Class.forName();Object o = clazz.newInstance();
    public User()
    {
        System.out.println("我在这了");
    }
    public User(String s)
    {

    }
    //这是一个Bean,封装了用户的信息,Spring可以帮我们创建对象
}

 package com.powernode.Spring6.bean;

public class User
{
//Spring是通过反射机制,调用类的无参数构造方法来实例化对象
//原理如下,Class clazz = Class.forName();Object o = clazz.newInstance();
public User()
{
System.out.println("我在这了");
}
public User(String s)
{

}
//这是一个Bean,封装了用户的信息,Spring可以帮我们创建对象
}



    


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.xsd">



    


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.xsd">

package com.powernode.Spring6.dao;

public class UserDaoImplForMySQL
{
    public void insert()
    {
        System.out.println("MySQL");
    }
}

package com.powernode.Spring6.dao;

public class UserDaoImplForMySQL
{
public void insert()
{
System.out.println("MySQL");
}
}

你可能感兴趣的:(Spring框架,JAVA随手写,spring,java,开发语言,算法,intellij-idea,jvm)